Home > Blockchain >  Capturing the Timestamp values from resampled DataFrame
Capturing the Timestamp values from resampled DataFrame

Time:11-17

Using the .resample() method yields a DataFrame with a DatetimeIndex and a frequency.

Does anyone have an idea on how to iterate through the values of that DatetimeIndex ?

df = pd.DataFrame(
    data=np.random.randint(0, 10, 100),
    index=pd.date_range('20220101', periods=100),
    columns=['a'],
)

df.resample('M').mean()

If you iterate, you get individual entries taking the Timestamp(‘2022-11-XX…’, freq=‘M’) form but I did not manage to get the date only.

g.resample('M').mean().index[0]
Timestamp('2022-01-31 00:00:00', freq='M')

I am aiming at feeding all the dates in a list for instance.

Thanks for your help !

CodePudding user response:

You an convert each entry in the index into a Datetime object using .date and to a list using .tolist() as below

>>> df.resample('M').mean().index.date.tolist()
[datetime.date(2022, 1, 31), datetime.date(2022, 2, 28), datetime.date(2022, 3, 31), datetime.date(2022, 4, 30)]

You can also truncate the timestamp as follows (reference solution)

>>> df.resample('M').mean().index.values.astype('<M8[D]')
array(['2022-01-31', '2022-02-28', '2022-03-31', '2022-04-30'],
      dtype='datetime64[D]')

CodePudding user response:

This solution seems to work fine both for dates and periods:
I = [k.strftime('%Y-%m') for k in g.resample('M').groups]

  • Related