У меня есть DataFrame, содержащий атрибуты [ключ, дата, получатель, оценка].Я хочу изменить данные по дате и получателю в течение 5 минут.приращения.Мой подход ниже.Сначала я делаю datetime для соответствующих типов date и time.Затем я группирую «дата» и «получатель» и использую примененную функцию для повторной выборки каждой группы.
Если я удаляю «resample» и просто возвращаю «x», я вижу, что данные правильно сгруппированы и переданы в функцию «process» (см. Ниже).
key datetime receiver score date time
0 9IIWNCEZD 2017-01-03 08:36:09 A -2.013896e+08 2017-01-03 08:36:09
5 ZEU7GZP47 2017-01-03 08:36:23 A -2.013668e+08 2017-01-03 08:36:23
6 ZEYSUQEI1 2017-01-03 08:36:27 A -2.013640e+08 2017-01-03 08:36:27
10 KW5FYJPIT 2017-01-03 08:36:38 A -2.013632e+08 2017-01-03 08:36:38
17 CE9RZFN5S 2017-01-03 08:36:49 A -2.013631e+08 2017-01-03 08:36:49
21 YQ7KSTNSC 2017-01-03 09:09:32 A -2.029635e+08 2017-01-03 09:09:32
key datetime receiver score date time
1 10E1WQXUI 2017-01-03 08:36:11 B -50020185.32 2017-01-03 08:36:11
key datetime receiver score date time
2 EHB0FM863 2017-01-03 08:36:12 C -1.008293e+08 2017-01-03 08:36:12
8 KW0UKT04Y 2017-01-03 08:36:35 C -1.007854e+08 2017-01-03 08:36:35
key datetime receiver score date time
3 EHFLTCXJX 2017-01-03 08:36:14 D -90002925.25 2017-01-03 08:36:14
12 YD2EHEZUE 2017-01-03 08:36:39 D -90001925.25 2017-01-03 08:36:39
18 KWJ83RTOH 2017-01-03 08:36:50 D -90001725.25 2017-01-03 08:36:50
key datetime receiver score date time
4 VHYI21ALA 2017-01-03 08:36:15 E -1.006858e+08 2017-01-03 08:36:15
9 YCXT3OAGJ 2017-01-03 08:36:36 E -1.006308e+08 2017-01-03 08:36:36
11 PUSYD2TBQ 2017-01-03 08:36:38 E -1.006268e+08 2017-01-03 08:36:38
13 3VR53M1VB 2017-01-03 08:36:40 E -1.006264e+08 2017-01-03 08:36:40
16 PV254K83I 2017-01-03 08:36:47 E -1.006258e+08 2017-01-03 08:36:47
19 3W4X8U610 2017-01-03 08:36:53 E -1.005406e+08 2017-01-03 08:36:53
20 DS1EUQNUE 2017-01-03 09:07:34 E -1.005189e+08 2017-01-03 09:07:34
25 T5ZOVXHGW 2017-01-03 10:17:53 E -1.005244e+08 2017-01-03 10:17:53
key datetime receiver score date time
7 IRBW5Z94D 2017-01-03 08:36:31 F -1.001900e+08 2017-01-03 08:36:31
14 CE0L7Y8E0 2017-01-03 08:36:40 F -1.001320e+08 2017-01-03 08:36:40
15 YD6ZV5P8A 2017-01-03 08:36:43 F -1.001270e+08 2017-01-03 08:36:43
29 PUXJQTNW2 2017-01-03 10:28:35 F -1.012220e+08 2017-01-03 10:28:35
key datetime receiver score date time
24 L0VF2ZUFX 2017-01-04 09:14:37 A -2.026835e+08 2017-01-04 09:14:37
30 OCTPWAQOH 2017-01-04 10:51:29 A -2.025107e+08 2017-01-04 10:51:29
key datetime receiver score date time
23 FBJRWFDKB 2017-01-04 09:12:43 B -44649416.6 2017-01-04 09:12:43
key datetime receiver score date time
22 JVEE0WOVC 2017-01-04 09:10:32 D -88645751.82 2017-01-04 09:10:32
key datetime receiver score date time
28 KWA1CAK36 2017-01-04 10:28:35 E -1.005225e+08 2017-01-04 10:28:35
key datetime receiver score date time
26 8IO0DWFDA 2017-01-04 10:22:38 F -1.012222e+08 2017-01-04 10:22:38
27 RK21L5E69 2017-01-04 10:27:46 F -1.012221e+08 2017-01-04 10:27:46
НО, если я включу повторную выборку, поведение будет странным (см. Распечатку внизу).Похоже, что столбцы постепенно удаляются, пока пустой DataFrame не будет передан в функцию 'process', которая затем выдаст ошибку.Я понимаю, что «apply» фактически вызывает функцию дважды в первой строке / столбце, но, поскольку я не думаю, что я изменяю какие-либо данные, я не понимаю, что происходит.ПРИМЕЧАНИЕ: я не ищу просто решение, я также пытаюсь понять поведение.
s = pd.DataFrame([["9IIWNCEZD","2017-01-03 08:36:09","A",-201389609],["10E1WQXUI","2017-01-03 08:36:11","B",-50020185.32],["EHB0FM863","2017-01-03 08:36:12","C",-100829267.43],["EHFLTCXJX","2017-01-03 08:36:14","D",-90002925.25],["VHYI21ALA","2017-01-03 08:36:15","E",-100685818.41],["ZEU7GZP47","2017-01-03 08:36:23","A",-201366792.15],["ZEYSUQEI1","2017-01-03 08:36:27","A",-201363981.95999998],["IRBW5Z94D","2017-01-03 08:36:31","F",-100190030.42],["KW0UKT04Y","2017-01-03 08:36:35","C",-100785367.43],["YCXT3OAGJ","2017-01-03 08:36:36","E",-100630818.41],["KW5FYJPIT","2017-01-03 08:36:38","A",-201363181.95999998],["PUSYD2TBQ","2017-01-03 08:36:38","E",-100626818.41],["YD2EHEZUE","2017-01-03 08:36:39","D",-90001925.25],["3VR53M1VB","2017-01-03 08:36:40","E",-100626418.41],["CE0L7Y8E0","2017-01-03 08:36:40","F",-100132011.16],["YD6ZV5P8A","2017-01-03 08:36:43","F",-100127011.16],["PV254K83I","2017-01-03 08:36:47","E",-100625778.41],["CE9RZFN5S","2017-01-03 08:36:49","A",-201363081.95999998],["KWJ83RTOH","2017-01-03 08:36:50","D",-90001725.25],["3W4X8U610","2017-01-03 08:36:53","E",-100540645.57],["DS1EUQNUE","2017-01-03 09:07:34","E",-100518856.89999999],["YQ7KSTNSC","2017-01-03 09:09:32","A",-202963512.17000002],["JVEE0WOVC","2017-01-03 09:10:32","D",-88645751.82],["FBJRWFDKB","2017-01-03 09:12:43","B",-44649416.6],["L0VF2ZUFX","2017-01-03 09:14:37","A",-202683512.17000002],["T5ZOVXHGW","2017-01-03 10:17:53","E",-100524437.18999998],["8IO0DWFDA","2017-01-03 10:22:38","F",-101222150.92999999],["RK21L5E69","2017-01-03 10:27:46","F",-101222144.03999999],["KWA1CAK36","2017-01-03 10:28:35","E",-100522494.62],["PUXJQTNW2","2017-01-03 10:28:35","F",-101221964.32],["OCTPWAQOH","2017-01-03 10:51:29","A",-202510655.58]],columns=["key","datetime","receiver","score"])
s["date"] = pd.to_datetime(s["datetime"]).dt.date
s["time"] = pd.to_datetime(s["datetime"]).dt.time
data_YMD = s.copy()
i=0
def process(x):
global i
if i<=6:
print(x)
y = x.resample("5T", on="time").max()
return y
data15 = data_YMD.groupby(by=["date","receiver"]).apply(lambda x: process(x))
, которое приводит к следующему выводу (из оператора 'print') и ошибке:
key datetime receiver score date time
0 9IIWNCEZD 2017-01-03 08:36:09 A -2.013896e+08 2017-01-03 08:36:09
5 ZEU7GZP47 2017-01-03 08:36:23 A -2.013668e+08 2017-01-03 08:36:23
6 ZEYSUQEI1 2017-01-03 08:36:27 A -2.013640e+08 2017-01-03 08:36:27
10 KW5FYJPIT 2017-01-03 08:36:38 A -2.013632e+08 2017-01-03 08:36:38
17 CE9RZFN5S 2017-01-03 08:36:49 A -2.013631e+08 2017-01-03 08:36:49
21 YQ7KSTNSC 2017-01-03 09:09:32 A -2.029635e+08 2017-01-03 09:09:32
key datetime receiver score date time
0 9IIWNCEZD 2017-01-03 08:36:09 A -2.013896e+08 2017-01-03 08:36:09
5 ZEU7GZP47 2017-01-03 08:36:23 A -2.013668e+08 2017-01-03 08:36:23
6 ZEYSUQEI1 2017-01-03 08:36:27 A -2.013640e+08 2017-01-03 08:36:27
10 KW5FYJPIT 2017-01-03 08:36:38 A -2.013632e+08 2017-01-03 08:36:38
17 CE9RZFN5S 2017-01-03 08:36:49 A -2.013631e+08 2017-01-03 08:36:49
21 YQ7KSTNSC 2017-01-03 09:09:32 A -2.029635e+08 2017-01-03 09:09:32
key datetime score time
0 9IIWNCEZD 2017-01-03 08:36:09 -2.013896e+08 08:36:09
5 ZEU7GZP47 2017-01-03 08:36:23 -2.013668e+08 08:36:23
6 ZEYSUQEI1 2017-01-03 08:36:27 -2.013640e+08 08:36:27
10 KW5FYJPIT 2017-01-03 08:36:38 -2.013632e+08 08:36:38
17 CE9RZFN5S 2017-01-03 08:36:49 -2.013631e+08 08:36:49
21 YQ7KSTNSC 2017-01-03 09:09:32 -2.029635e+08 09:09:32
key datetime score time
0 9IIWNCEZD 2017-01-03 08:36:09 -2.013896e+08 08:36:09
5 ZEU7GZP47 2017-01-03 08:36:23 -2.013668e+08 08:36:23
6 ZEYSUQEI1 2017-01-03 08:36:27 -2.013640e+08 08:36:27
10 KW5FYJPIT 2017-01-03 08:36:38 -2.013632e+08 08:36:38
17 CE9RZFN5S 2017-01-03 08:36:49 -2.013631e+08 08:36:49
21 YQ7KSTNSC 2017-01-03 09:09:32 -2.029635e+08 09:09:32
Traceback (most recent call last):
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 725, in apply
result = self._python_apply_general(f)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 742, in _python_apply_general
keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/groupby/ops.py", line 237, in apply
res = f(group)
File "main.py", line 112, in <lambda>
data15 = data_YMD.groupby(by=["date","receiver"]).apply(lambda x: process(x))
File "main.py", line 109, in process
y = x.resample("5T", on="time").max()
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/generic.py", line 8449, in resample
level=level,
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/resample.py", line 1306, in resample
return tg._get_resampler(obj, kind=kind)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/resample.py", line 1443, in _get_resampler
"but got an instance of %r" % type(ax).__name__
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 112, in <module>
data15 = data_YMD.groupby(by=["date","receiver"]).apply(lambda x: process(x))
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 737, in apply
return self._python_apply_general(f)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/groupby/groupby.py", line 742, in _python_apply_general
keys, values, mutated = self.grouper.apply(f, self._selected_obj, self.axis)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/groupby/ops.py", line 237, in apply
res = f(group)
File "main.py", line 112, in <lambda>
data15 = data_YMD.groupby(by=["date","receiver"]).apply(lambda x: process(x))
File "main.py", line 109, in process
y = x.resample("5T", on="time").max()
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/generic.py", line 8449, in resample
level=level,
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/resample.py", line 1306, in resample
return tg._get_resampler(obj, kind=kind)
File "/home/runner/.local/share/virtualenvs/python3/lib/python3.7/site-packages/pandas/core/resample.py", line 1443, in _get_resampler
"but got an instance of %r" % type(ax).__name__
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
СЛЕДУЮЩИЕ ЗАМЕЧАНИЯ:
1) проблема может быть связана с «groupby».Это работает, если вместо вызова «groupby (« дата »,« получатель »)» я делаю «groupby» («дата»), а затем вызывать функцию, которая выполняет групповую обработку («получатель») и т. Д., Как показано ниже.Но это выглядит очень глупо, и мне интересно почему это работает?
def process(x):
def scoop(y):
return y.set_index(pd.DatetimeIndex(y["datetime"])) \
.resample("5T").max()
x = x.groupby("receiver").apply(lambda y: scoop(y))
return x
data15 = data_YMD.groupby(by=["date"]).apply(lambda x: process(x))