熊猫groupby-> resample删除列

时间:2019-09-23 17:38:00

标签: python-3.x pandas pandas-groupby

我有一个包含[key,datetime,receiver,score]属性的DataFrame。我想在5分钟内按日期和接收者重新采样数据。增量。我的方法如下。首先,我将“ datetime”设置为适当的“ date”和“ time”类型。然后,我对“日期”和“接收者”进行分组,并使用一个应用函数对每个组进行重采样。

如果删除“重新采样”并仅返回“ x”,则可以看到数据已正确分组并传递给“处理”功能(请参见下文)。

       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'函数,然后该函数出错。我知道“应用”实际上在第一行/列上调用了两次函数,但是由于我不认为自己对任何数据进行了变异,所以我不了解发生了什么。注意:我不仅在寻找解决方案,还试图了解其行为。

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('date')而不是调用“ groupby('date','receiver')”,然后调用应用执行groupby('receiver')等的函数,则如下所示。但这似乎很混乱,我想知道为什么有效吗?

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))

2 个答案:

答案 0 :(得分:1)

您得到的错误是您需要对非datatime dtype列进行重新采样。

将s ['datetime']转换为datetime dtype并在s ['datetime']上重新采样,如下所示:

s["date"] = pd.to_datetime(s["datetime"]).dt.date
s["time"] = pd.to_datetime(s["datetime"]).dt.time
s['datetime'] = pd.to_datetime(s['datetime'])
data_YMD = s.copy()
i=0
def process(x):
    global i 
    if i<=6:
        print(x)
    y = x.resample("5T", on="datetime").max()
    return y

data15 = data_YMD.groupby(by=["date","receiver"]).apply(lambda x: process(x))

答案 1 :(得分:1)

您的代码有两个独立的问题:

  1. resample需要一个日期时间列,而您的'date'不是。这就是为什么在跟踪结束时出现错误TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'的原因。看这个:

    s["date"] = pd.to_datetime(s["datetime"]).dt.date
    s["time"] = pd.to_datetime(s["datetime"]).dt.time
    

    dt.datedt.time正在提取表示日期和时间的字符串。但是是字符串,而不是日期时间对象。因此,您无法在任何这些列上resample

  2. 组合groupby().apply()试图将使用apply生成的结果数据帧合并到多索引数据帧中。但是apply使用的函数正在使用resample,看来这很难重新加入数据帧。如果仅固定点1,则将得到一个KeyError,如下所示:ValueError: Key 2017-01-03 00:00:00 not in level Index([2017-01-03], dtype='object', name='date')

如何解决所有这些问题?首先,将"datetime"设置为实际的日期时间列(如果尚未创建):

s["datetime"] = pd.to_datetime(s["datetime"])

要解决问题2,可以使用pandas Grouper,它提供了有用的重采样操作,因此无需使用resample

data_YMD = s.copy()
data15 = data_YMD.groupby(by=[pd.Grouper(key="receiver"), pd.Grouper(key="datetime", freq="5T")]).max()
使用示例数据的

data15输出为:

                                    key         score        date
receiver datetime                                                
A        2017-01-03 08:35:00  ZEYSUQEI1 -2.013631e+08  2017-01-03
         2017-01-03 09:05:00  YQ7KSTNSC -2.029635e+08  2017-01-03
         2017-01-03 09:10:00  L0VF2ZUFX -2.026835e+08  2017-01-03
         2017-01-03 10:50:00  OCTPWAQOH -2.025107e+08  2017-01-03
B        2017-01-03 08:35:00  10E1WQXUI -5.002019e+07  2017-01-03
         2017-01-03 09:10:00  FBJRWFDKB -4.464942e+07  2017-01-03
C        2017-01-03 08:35:00  KW0UKT04Y -1.007854e+08  2017-01-03
D        2017-01-03 08:35:00  YD2EHEZUE -9.000173e+07  2017-01-03
         2017-01-03 09:10:00  JVEE0WOVC -8.864575e+07  2017-01-03
E        2017-01-03 08:35:00  YCXT3OAGJ -1.005406e+08  2017-01-03
         2017-01-03 09:05:00  DS1EUQNUE -1.005189e+08  2017-01-03
         2017-01-03 10:15:00  T5ZOVXHGW -1.005244e+08  2017-01-03
         2017-01-03 10:25:00  KWA1CAK36 -1.005225e+08  2017-01-03
F        2017-01-03 08:35:00  YD6ZV5P8A -1.001270e+08  2017-01-03
         2017-01-03 10:20:00  8IO0DWFDA -1.012222e+08  2017-01-03
         2017-01-03 10:25:00  RK21L5E69 -1.012220e+08  2017-01-03