I have a pandas dataframe in Python with a column such as this:
df.Timestamp
... .................. 129 2018-09-12 21:40:00 130 2018-09-12 21:50:00 131 2018-09-12 22:00:00 132 2018-09-12 22:10:00 133 2018-09-12 22:20:00 134 2018-09-12 22:30:00 135 2018-09-12 22:40:00 136 2018-09-12 22:50:00 137 2018-09-12 23:00:00 138 2018-09-12 23:10:00 139 2018-09-12 23:20:00 140 2018-09-12 23:30:00 141 2018-09-12 23:40:00 142 2018-09-12 23:50:00 Name: Timestamp, dtype: datetime64[ns]
Type of individual element is:
type(df.Timestamp[0])
datetime.datetime
When I try resampling, following error occurs:
df.Timestamp.resample('2H')
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex
I have tried pd_todatetime
but it does not work.
As far as I know, my datatype is right as it is datetime.datetime
None of the solutions I viewed had an answer for this type of problem.
What can be the solution?
There are 2 possible solutions - parameter on
in resample
or create DatetimeIndex
by set_index
.
Last add some aggregate function like sum
, mean
...:
df.resample('2H', on='Timestamp').sum()
df.set_index('Timestamp').resample('2H').sum()