(-) MonthBegin 的 Pandas 日期时间锚定偏移量无法按预期工作 [英] Pandas datetime anchored offset for (-) MonthBegin doesn't work as expected

查看:110
本文介绍了(-) MonthBegin 的 Pandas 日期时间锚定偏移量无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要回到月初,但如果我已经在月初了,我想留在那里.使用 n=0 的 Pandas 锚定偏移量应该完全做到这一点,但它不会在 (-) MonthBegin 的锚定点之间产生预期的结果.

I need to move back to the beginning of the month but if i'm already at the beginning I want to stay there. Pandas anchored offset with n=0 is supposed to do exactly that but it doesn't produce the expected results between the anchored points for the (-) MonthBegin .

例如为此pd.Timestamp('2017-01-06 00:00:00') - pd.tseries.offsets.MonthBegin(n=0)我希望将我移回 Timestamp('2017-01-01 00:00:00')但是我得到 Timestamp('2017-02-01 00:00:00')我究竟做错了什么?或者你认为这是一个错误?

For example for this pd.Timestamp('2017-01-06 00:00:00') - pd.tseries.offsets.MonthBegin(n=0) I expect to move me back to Timestamp('2017-01-01 00:00:00') but instead I get Timestamp('2017-02-01 00:00:00') What am I doing wrong? Or you think it's a bug?

我也可以看到同样的规则适用于 MonthEnd 所以结合下面的 2 pd.Timestamp('2017-01-06 00:00:00')+pd.tseries.offsets.MonthEnd(n=0)-pd.tseries.offsets.MonthBegin(n=1)我得到了 Timestamp('2017-01-01 00:00:00') 的预期效果,但我期望它只与 - pd.tseries.offsets.MonthBegin(n=0)

I can also see that the same rule works fine for the MonthEnd so combining the 2 like below pd.Timestamp('2017-01-06 00:00:00')+pd.tseries.offsets.MonthEnd(n=0)-pd.tseries.offsets.MonthBegin(n=1) I get the desired effect of Timestamp('2017-01-01 00:00:00') but my expectation for it to work with just - pd.tseries.offsets.MonthBegin(n=0)

推荐答案

这确实是目睹的正确行为,这是锚定偏移语义中为支持开始/结束特定频率偏移.

This is indeed the correct behavior that is witnessed which are part of the rules laid out in Anchored Offset Semantics for offsets supporting start/end of a particular frequency offset.

考虑给定的例子:

from pandas.tseries.offsets import MonthBegin

pd.Timestamp('2017-01-02 00:00:00') - MonthBegin(n=0)
Out[18]:
Timestamp('2017-02-01 00:00:00')

注意MonthBegin偏移量对应的锚点设置为每个月的第一天.现在,由于给定的时间戳明显超过这一天,这些将自动被视为下个月的一部分,并且滚动(无论向前或向后)将在此之后发挥作用.

Note that the anchor point corresponding to MonthBegin offset is set as first of every month. Now, since the given timestamp clearly surpasses this day, these would automatically be treated as though it were a part of the next month and rolling (whether forward or backwards) would come into play only after that.

摘自 docs
对于 n=0 的情况,如果在锚点上,则不会移动日期,否则前滚到下一个锚点.

excerpt from docs
For the case when n=0, the date is not moved if on an anchor point, otherwise it is rolled forward to the next anchor point.

<小时>

要获得您想要的结果,您需要提供 n=1 它将时间戳滚动到正确的日期.


To get what you're after, you need to provide n=1 which would roll the timestamp to the correct date.

pd.Timestamp('2017-01-02 00:00:00') - MonthBegin(n=1)
Out[20]:
Timestamp('2017-01-01 00:00:00')

如果您在确切的锚点上设置了日期,那么它也会根据附加文档为您提供所需的结果.

If you had set the date on the exact anchor point, then also it would give you the desired result as per the attached docs.

pd.Timestamp('2017-01-01 00:00:00') - MonthBegin(n=0)
Out[21]:
Timestamp('2017-01-01 00:00:00')

这篇关于(-) MonthBegin 的 Pandas 日期时间锚定偏移量无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆