AttributeError: module ‘pandas‘ has no attribute ‘TimeGrouper‘

要为每个’key’的值进行相同的重新采样,我们可以使用pandas.TimeGrouper对象:报错

import pandas as pd
import numpy as np

N = 15
times = pd.date_range('2017-05-20 00:00', freq='1min', periods=N)
df2 = pd.DataFrame({'time': times.repeat(3), 'key': np.tile(['a', 'b', 'c'], N), 'value': np.arange(N * 3.)})
time_key = pd.TimeGrouper('5min')
resampled = (df2.set_index('time').groupby(['key', time_key]).sum())

输出:

AttributeError: module 'pandas' has no attribute 'TimeGrouper'

修改后:
之后我们可以设置时间索引,按’key’和time_key进行分组,再聚合:

from pandas.core import resample as rp
time_key = rp.TimeGrouper('5min')
resampled = (df2.set_index('time')
             .groupby(['key', time_key])
             .sum())
resampled

输出:

Out[12]: 
                         value
key time                      
a   2017-05-20 00:00:00   30.0
    2017-05-20 00:05:00  105.0
    2017-05-20 00:10:00  180.0
b   2017-05-20 00:00:00   35.0
    2017-05-20 00:05:00  110.0
    2017-05-20 00:10:00  185.0
c   2017-05-20 00:00:00   40.0
    2017-05-20 00:05:00  115.0
    2017-05-20 00:10:00  190.0

你可能感兴趣的:(利用Python进行数据分析,pandas,python,数据分析)