为什么 offsets.QuarterBegin 日期是 12-01、03-01、06-01、09-01?

why the offsets.QuarterBegin date is 12-01, 03-01, 06-01, 09-01?

t = pd.Timestamp('2020-02-05')
print(pd.offsets.QuarterBegin().rollback(t))
print(pd.offsets.QuarterEnd().rollback(t))

输出:

2019-12-01 00:00:00
2019-12-31 00:00:00

为什么结果是2019-12-01?不应该是 2020-01-01 吗?

docs specify that there is a startingMonth; e.g. "startingMonth = 3 corresponds to dates like 3/01/2007, 6/01/2007". To find out what the default of this keyword argument is, you can have a look at the src and observe that _default_starting_month = 3 (link).

因此,在不提供 startingMonth 的情况下,您的季度从月 = 3、6、9、12 开始 - 这就是为什么如果您“回滚”pd.Timestamp('2020-02-05'),您将获得 2019-12-01 00:00:00

如果你想让你的宿舍开始,例如on month = 1, 4, ... and end on month = 3, 6, ...(含),您可以将关键字设置为 1(对于 QuarterEnd 分别设置为 3):

t = pd.Timestamp('2020-02-05')
print(pd.offsets.QuarterBegin(startingMonth=1).rollback(t))
print(pd.offsets.QuarterEnd(startingMonth=3).rollback(t))

2020-01-01 00:00:00
2019-12-31 00:00:00