Sanne
Sanne

Reputation: 57

Python dataframe resample TypeError

I have a pd.DataFrame that I want to resample for every ten minutes. For the resampling I indexed my dataframe like this:

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

Resulting to have re_in looks like this:

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 

I tried to take samples with this code:

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

But I get the error:

  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' 

When I checked the re_in.index it said that it is an 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)

How can I solve this problem?

Upvotes: 0

Views: 297

Answers (1)

Holt
Holt

Reputation: 37616

T is a property of pandas.DataFrame that will transpose your dataframe, you need to use ['T']:

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

Upvotes: 1

Related Questions