英文:
Pandas date_range unexpected behaviour
问题
如果我运行:
pd.date_range('2022-01-01 15:00', '2023-02-02 00:00', freq='YS')
我会收到:
DatetimeIndex(['2022-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
我期望在返回中也会得到'2023-01-01 15:00:00',即:
DatetimeIndex(['2022-01-01 15:00:00', '2023-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
我觉得这很奇怪。我是否遗漏了什么,还是这是一个错误?如何实现预期的行为?
谢谢
编辑:同意s_pike的观点,这种行为看起来相当反直觉,我认为这是一个错误。
英文:
If I run:
pd.date_range('2022-01-01 15:00', '2023-02-02 00:00', freq='YS')
I receive:
DatetimeIndex(['2022-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
I would expect to get 2023-01-01 15:00:00 in the return as well, i.e.
DatetimeIndex(['2022-01-01 15:00:00', '2023-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
Feel this is really strange. Am I missing anything or is it a bug? How may I achieve the expected behaviour?
Thanks
Edit: agree with s_pike, this behaviour looks quite counter-intuitive and I would say it's a bug.
答案1
得分: 0
你可以尝试这样做,这可能会提供您期望的结果。
date_range = pd.date_range('2022-01-01 15:00', '2023-02-02 00:00', freq='AS')
date_range = date_range.union(pd.DatetimeIndex(['2023-01-01 15:00']))
这个pd.DatetimeIndex()
构造函数将创建一个新的"datetimeindex"。
英文:
You can try like this, this might provide your expected result.
date_range = pd.date_range('2022-01-01 15:00', '2023-02-02 00:00', freq='AS')
date_range = date_range.union(pd.DatetimeIndex(['2023-01-01 15:00']))
This pd.DatetimeIndex()
constructor will create a new "datetimeindex".
答案2
得分: 0
以下是翻译好的内容:
你可以通过以下方式获取你想要的输出:
pd.date_range('2022-01-01 15:00', '2023-01-01 15:00', freq='YS')
# DatetimeIndex(['2022-01-01 15:00:00', '2023-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
如果你将日期更改为二月,你必须将时间更改为大于或等于开始日期中给定的时间。
pd.date_range('2022-01-01 15:00', '2023-02-02 15:00', freq='YS')
# DatetimeIndex(['2022-01-01 15:00:00', '2023-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
如果你的时间少于开始日期中指定的时间,那么你将无法获得结果:
pd.date_range('2022-01-01 15:00', '2023-02-02 14:59', freq='YS')
# DatetimeIndex(['2022-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
以下是相关文档链接:
英文:
You can get your desired output by:
pd.date_range('2022-01-01 15:00', '2023-01-01 15:00', freq='YS')
#DatetimeIndex(['2022-01-01 15:00:00', '2023-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
If you change your date to February you must change time to greater than or equal time given in start date.
pd.date_range('2022-01-01 15:00', '2023-02-02 15:00', freq='YS')
#DatetimeIndex(['2022-01-01 15:00:00', '2023-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
If you less time than the time specified in the start date then you will not get the result:
pd.date_range('2022-01-01 15:00', '2023-02-02 14:59', freq='YS')
#DatetimeIndex(['2022-01-01 15:00:00'], dtype='datetime64[ns]', freq='AS-JAN')
here are the links to documents:
https://pandas.pydata.org/docs/reference/api/pandas.date_range.html
https://pandas.pydata.org/docs/user_guide/timeseries.html#timeseries-offset-aliases
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论