Difference between pandas resample 'M' and 'MS'

4.3k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.