0

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 !

skillsmuggler
  • 1,862
  • 1
  • 11
  • 16
pomseb
  • 11
  • 3

2 Answers2

1

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]')
skillsmuggler
  • 1,862
  • 1
  • 11
  • 16
  • This is really good, thank you but we have a numpy object in both cases. Any idea how we can stay in the Pandas world ? – pomseb Nov 16 '22 at 14:36
  • The first case is a`list`. You can convert the numpy object to a list by using the `.tolist()` method. You will get the same result as the first solution. – skillsmuggler Nov 16 '22 at 14:40
  • Works fine if it is a date, but not if it is a period for instance. Here is an example: >>> g.resample('M', kind='period').sum().index PeriodIndex(['2022-01', '2022-02', '2022-03', '2022-04'], dtype='period[M]') – pomseb Nov 16 '22 at 16:25
0

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

pomseb
  • 11
  • 3