我正在创建一个日期范围如下:
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')我的期望是,DatetimeIndex的第一次约会是2022-11-01 --为什么是2022-11-30?
谢谢!
发布于 2022-09-25 20:39:17
因为M的意思是(参见偏移别名)
月终频率
您可以将M改为MS,
月启动频率
pd.date_range(start=contract_start_date, periods=contract_length_months, freq='MS')https://stackoverflow.com/questions/73847656
复制相似问题