pandas dataframe resample per day without date time index
For pandas version 0.19.0 and newer you can use the on
keyword:
df.resample('H', on='timestamps').mean()
Result:
light
timestamps
2004-02-28 00:00:00 147.20
2004-02-28 01:00:00 150.88
Here is an approach to resample.
You can use the following method to sample at T
interval.
If original data was in every minute
, your new resampled data will be at the 2 min
interval.
You can use 3T, 4T....
any T
value that fits your need.
df_2T = df.resample('2T', on = 'timestamp').mean()
For hourly
df_hourly = df.resample('60T', on = 'timestamp').mean()
For daily
df_daily = df.resample('1440T', on = 'timestamp').mean()
Note: One day has 60*24 = 1440 min
You are right - need DatetimeIndex
, TimedeltaIndex
or PeriodIndex
else error:
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'
So you have to first reset_index
and set_index
if original index
is important:
print (df.reset_index().set_index('timestamps'))
index light
timestamps
2004-02-28 00:58:45 7 150.88
2004-02-28 00:59:45 26 143.52
2004-02-28 01:00:45 34 150.88
2004-02-28 01:01:15 42 150.88
2004-02-28 01:02:15 59 150.88
if not only set_index
:
print (df.set_index('timestamps'))
light
timestamps
2004-02-28 00:58:45 150.88
2004-02-28 00:59:45 143.52
2004-02-28 01:00:45 150.88
2004-02-28 01:01:15 150.88
2004-02-28 01:02:15 150.88
and then resample
:
print (df.reset_index().set_index('timestamps').resample('1D').mean())
index light
timestamps
2004-02-28 33.6 149.408