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!
CodePudding user response:
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')