Python数据帧重新采样TypeError

时间:2017-06-29 09:03:48

标签: python dataframe typeerror

我有一个pd.DataFrame,我想每十分钟重新采样一次。 对于重新采样,我将我的数据框索引为:

re_in = Re.set_index(pd.DatetimeIndex(Re['time'])) # index Re

导致re_in看起来像这样:

time                 time                   T   WR    WS    WG   U    R   RC                                                                           
2017-06-22 00:00:00  2017-06-22 00:00:00  21.8  159   5.8   7.9  66  0.0  0.0   
2017-06-22 00:05:00  2017-06-22 00:05:00  21.9  173   9.0  11.9  65  0.0  0.0   
2017-06-22 00:10:00  2017-06-22 00:10:00  21.9  145   4.0   4.0  66  0.0  0.0   
2017-06-22 00:15:00  2017-06-22 00:15:00  21.9  158   6.8   7.9  67  0.0  0.0   
2017-06-22 00:20:00  2017-06-22 00:20:00  21.9  149   6.4   7.9  68  0.0  0.0   
2017-06-22 00:25:00  2017-06-22 00:25:00  21.9  156   4.7   7.9  68  0.0  0.0   
2017-06-22 00:30:00  2017-06-22 00:30:00  21.8  153   3.2   4.0  69  0.0  0.0 

我试图用这段代码取样:

av_temp['RE_mean'] = re_in.T.resample("10T").mean() # average Re

但我收到错误:

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 880, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "M:/Python/Testveld/Software/selecting_date_temperature_av.py", line 234, in <module>
    av_temp['WH_mean'] = re_in.T.resample(SAMPLE).mean() # average renkforce

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 4212, in resample
    base=base, key=on, level=level)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\tseries\resample.py", line 944, in resample
    return tg._get_resampler(obj, kind=kind)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\tseries\resample.py", line 1057, 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' 

当我检查re_in.index时,它说它是一个DatetimeIndex。

print(re_in.index)
DatetimeIndex(['2017-06-22 00:00:00', '2017-06-22 00:05:00',
               '2017-06-22 00:10:00', '2017-06-22 00:15:00',
               '2017-06-22 00:20:00', '2017-06-22 00:25:00',
               '2017-06-22 00:30:00', '2017-06-22 00:35:00',
               '2017-06-22 00:37:00', '2017-06-22 00:42:00',
               ...
               '2017-06-22 23:15:00', '2017-06-22 23:20:00',
               '2017-06-22 23:25:00', '2017-06-22 23:30:00',
               '2017-06-22 23:35:00', '2017-06-22 23:40:00',
               '2017-06-22 23:45:00', '2017-06-22 23:46:00',
               '2017-06-22 23:51:00', '2017-06-22 23:56:00'],
              dtype='datetime64[ns]', name='time', length=288, freq=None)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

Tpandas.DataFrame的属性transpose您的数据框,您需要使用['T']

av_temp['RE_mean'] = re_in['T'].resample("10T").mean() # average Re
相关问题