shouldn’t the below example include ‘2022-01-01’?
>>> import pandas as pd
>>> pd.date_range("2022-01-03", "2023-12-31", freq='MS')
DatetimeIndex(['2022-02-01', '2022-03-01', '2022-04-01', '2022-05-01',
'2022-06-01', '2022-07-01', '2022-08-01', '2022-09-01',
'2022-10-01', '2022-11-01', '2022-12-01', '2023-01-01',
'2023-02-01', '2023-03-01', '2023-04-01', '2023-05-01',
'2023-06-01', '2023-07-01', '2023-08-01', '2023-09-01',
'2023-10-01', '2023-11-01', '2023-12-01'],
dtype='datetime64[ns]', freq='MS')
>Solution :
How do we get start of the month in that case? inclusive of 2022-01-01
In this case, use pd.offsets.MonthBegin
:
>>> pd.date_range("2022-01-03", "2023-12-31", freq='M') + pd.offsets.MonthBegin(-1)
DatetimeIndex(['2022-01-01', '2022-02-01', '2022-03-01', '2022-04-01',
'2022-05-01', '2022-06-01', '2022-07-01', '2022-08-01',
'2022-09-01', '2022-10-01', '2022-11-01', '2022-12-01',
'2023-01-01', '2023-02-01', '2023-03-01', '2023-04-01',
'2023-05-01', '2023-06-01', '2023-07-01', '2023-08-01',
'2023-09-01', '2023-10-01', '2023-11-01', '2023-12-01'],
dtype='datetime64[ns]', freq=None)