Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does my monthly frequency date range use the last day of the month rather than the first?

I am creating a date range as follows:

contract_start_date = pd.to_datetime('2022-11-01')
contract_length_months = 3 * 12 # three years

pd.date_range(start=contract_start_date, periods=contract_length_months, freq='M')
>> DatetimeIndex(['2022-11-30', '2022-12-31', '2023-01-31', '2023-02-28',
                  '2023-03-31', '2023-04-30', '2023-05-31', '2023-06-30',
                  '2023-07-31', '2023-08-31', '2023-09-30', '2023-10-31',
                  '2023-11-30', '2023-12-31', '2024-01-31', '2024-02-29',
                  '2024-03-31', '2024-04-30', '2024-05-31', '2024-06-30',
                  '2024-07-31', '2024-08-31', '2024-09-30', '2024-10-31',
                  '2024-11-30', '2024-12-31', '2025-01-31', '2025-02-28',
                  '2025-03-31', '2025-04-30', '2025-05-31', '2025-06-30',
                  '2025-07-31', '2025-08-31', '2025-09-30', '2025-10-31'],
                 dtype='datetime64[ns]', freq='M')

My expectation was that the first date in the DatetimeIndex would be 2022-11-01 – why is it instead 2022-11-30?

Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

It’s because M means (see Offset aliases)

M month end frequency

You can change M to MS,

MS month start frequency

pd.date_range(start=contract_start_date, periods=contract_length_months, freq='MS')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading