简体   繁体   English

Pandas QuarterBegin():计算第一季度时可能出现错误

[英]Pandas QuarterBegin(): Possible Bug when calculating First of quarter

I would like to calculate the "first of quarter" in a pandas dataframe.我想计算熊猫数据框中的“第一季度”。 However I am running into some problems.但是我遇到了一些问题。 My Pandas version is 0.17.1 .我的 Pandas 版本是0.17.1

import pandas as pd
import datetime as dt

test=pd.Timestamp(dt.datetime(2011,1,20))
test=test.tz_localize('Europe/Rome')

previousquarter=test-pd.tseries.offsets.QuarterBegin()
nextquarter=test+pd.tseries.offsets.QuarterBegin()

My expected results would be previousquarter = (2011,1,1) and nextquarter = (2011,4,1) .我的预期结果是previousquarter = (2011,1,1)nextquarter = (2011,4,1) But what I get is previousquarter = (2010,12,1) and nextquarter = (2011,3,1) .但我得到的是previousquarter = (2010,12,1)nextquarter = (2011,3,1)

I have also tried it without tz_localize.我也尝试过不使用 tz_localize。 However, it did not change the result.然而,这并没有改变结果。

Am I doing something wrong here or is this a bug somewhere?我在这里做错了什么还是这是某个地方的错误?

Thanks in advance!提前致谢!

PS I know I could correct it by shifting one month, but this seems to be a rather crude workaround. PS 我知道我可以通过转移一个月来纠正它,但这似乎是一个相当粗略的解决方法。

Yup, looks like a bug: https://github.com/pydata/pandas/issues/8435是的,看起来像一个错误: https ://github.com/pydata/pandas/issues/8435

There is a better workaround than shifting a month though: offsets.QuarterBegin(startingMonth=1)不过,有一个比移动一个月更好的解决方法: offsets.QuarterBegin(startingMonth=1)

The answer given my Marshall worked fine for me except for first days of each year where it was pointing to first date of the last quarter of the previous year.我的马歇尔的答案对我来说很好,除了每年的第一天,它指向上一年最后一个季度的第一个日期。 eg of 2018-01-01 I was getting 2017-10-01例如 2018-01-01 我得到 2017-10-01

I had to do the following to handle that:我必须执行以下操作来处理:

(date + pd.tseries.offsets.DateOffset(days=1)) - pd.tseries.offsets.QuarterBegin(startingMonth=1) 

where date is a datetime.datetime object.其中 date 是 datetime.datetime 对象。

Reproduced @Abhi's bug in Pandas 1.1.3.在 Pandas 1.1.3 中重现了@Abhi 的错误。 In fact, couldn't get consistent results from QuarterEnd or QuarterBegin for all starting dates within the quarter.事实上,对于季度内的所有开始日期,无法从 QuarterEnd 或 QuarterBegin 获得一致的结果。 Instead resorted to going to the end of the prior quarter and adding a day, or to the beginning of the next quarter and subtracting a day.取而代之的是去到上一季度末并增加一天,或者到下一季度开始并减去一天。 Note QuarterEnd(startingMonth= 12 )注意 QuarterEnd(startingMonth= 12 )

import pandas as pd
print("     date           Quarter   Quarter begin      Quarter end   ")
for yr in range(2020, 2021):
  for mo in range(1,13):
    for dy in range(1,4):
        date = pd.Timestamp(yr, mo, dy)
        if dy == 3:
            date = date + pd.tseries.offsets.MonthEnd()
        qbegin = date + pd.offsets.QuarterEnd(-1, startingMonth=12) + pd.offsets.Day(1)
        qend = date + pd.offsets.QuarterBegin(1, startingMonth=1) - pd.offsets.Day(1)
        print("{}   {}   {}   {}".format(date, date.quarter, qbegin, qend))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM