Home > Software design >  Changing Pandas datetime index frequency
Changing Pandas datetime index frequency

Time:01-24

I am working with a datetime index in pandas. the index is as :

df.index

DatetimeIndex(['2009-01-01 00:10:00', '2009-01-01 00:20:00', '2009-01-01 00:30:00', '2009-01-01 00:40:00', '2009-01-01 00:50:00', '2009-01-01 01:00:00', '2009-01-01 01:10:00', '2009-01-01 01:20:00', '2009-01-01 01:30:00', '2009-01-01 01:40:00', ... '2016-12-31 22:30:00', '2016-12-31 22:40:00', '2016-12-31 22:50:00', '2016-12-31 23:00:00', '2016-12-31 23:10:00', '2016-12-31 23:20:00', '2016-12-31 23:30:00', '2016-12-31 23:40:00', '2016-12-31 23:50:00', '2017-01-01 00:00:00'], dtype='datetime64[ns]', name='Date Time', length=420551, freq=None)

I need the frequency of the index to be hourly. like
2009-01-01 00:10:00 ,
2009-01-01 01:10:00 ,
2009-01-01 02:10:00 ,
2009-01-01 03:10:00
....

can someone help me with this

CodePudding user response:

You could probably try the resample method: pandas.DataFrame.resample. You can build the new DataFrame (NewDf) from your current DataFrame (let's say df)

NewDf = df.resample('H').mean()

'H' allows to resample by hour

You need obviously to define the proper values, in this example I used the mean() method assuming you have more the one instance for each new resampled frequency value.

For your reference: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.resample.html

CodePudding user response:

You can create one like this.

pd.date_range(start='2009-01-01 00:10:00', periods = 420551, freq='H')
  •  Tags:  
  • Related