2

I'm using the function resample to change the daily data to be a monthly data of a pandas dataframe. Reading the documentation I found that I could define the rule='M' or rule='MS'. The first is "calendar month end" and the second is "calendar month begin". What is the difference between the two?

Arrigo
  • 21
  • 1
  • 3
  • Possible duplicate. https://stackoverflow.com/a/17001474/2956135 in short, Month end (M) vs Month start (MS). – Emma Apr 08 '22 at 21:07

1 Answers1

1

It doesn't set the same date as index of the resampled groups.

Here is an example:

date = pd.Series([0,1,2], 
                 index=pd.to_datetime(['2022-01-15', 
                                       '2022-01-20', 
                                       '2022-02-15']))

2022-01-15    0
2022-01-20    1
2022-02-15    2
dtype: int64

# resampling MS:
date.resample('MS').mean()

2022-01-01    0.5
2022-02-01    2.0
Freq: MS, dtype: float64

# resampling M:
date.resample('M').mean()
2022-01-31    0.5
2022-02-28    2.0
Freq: M, dtype: float64

Note the difference in the dates of the index. For 'MS' the dates of the groups are always the first of the month, for 'M' the last day.

mozway
  • 194,879
  • 13
  • 39
  • 75