Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuarterEnd Offset seems to not work with datetimes on the last day #13644

Closed
thequackdaddy opened this issue Jul 13, 2016 · 4 comments
Closed
Labels

Comments

@thequackdaddy
Copy link
Contributor

Hello,

I have a column of datetimes which I'm trying to group by moving them to the last day of the calendar quarter. However, I think when the datetime is on the last day of the quarter, It doesn't work.

Code Sample, a copy-pastable example if possible

In [8]: pd.Series(pd.DatetimeIndex(['2016-06-30 12:32'])).dt.date + pd.offsets.QuarterEnd()
Out[8]: 
0   2016-09-30
dtype: datetime64[ns]

Quarter Begin appears to be even more screwed up. It is moving the date to March 1, which isn't the beginning of a quarter.

pd.Series(pd.DatetimeIndex(['2016-04-01 04:02'])).dt.date - pd.offsets.QuarterBegin()
Out[9]: 
0   2016-03-01
dtype: datetime64[ns]

Expected Output

I expect this to be 2016-06-30.

output of pd.show_versions()

In [25]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Windows
OS-release: 2012ServerR2
machine: AMD64
processor: Intel64 Family 6 Model 63 Stepping 2, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_US

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.24
numpy: 1.11.0
scipy: 0.17.1
statsmodels: 0.8.0.dev0+032a9ab
xarray: None
IPython: 4.2.0
sphinx: 1.3.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.4
blosc: None
bottleneck: 1.0.0
tables: 3.2.2
numexpr: 2.6.0
matplotlib: 1.5.1
openpyxl: 2.3.2
xlrd: 1.0.0
xlwt: 1.1.2
xlsxwriter: 0.9.2
lxml: 3.6.0
bs4: 4.4.1
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
jinja2: 2.8
boto: 2.40.0
pandas_datareader: None

I think a suitable workaround for QuarterEnd is to convert the datetime to a date, then subtract a day, then move to QuartrEnd. (Untested).

@sinhrks
Copy link
Member

sinhrks commented Jul 14, 2016

If I understood properly, this isn't specific for quarter. It's because we're adding the offset.

pd.Series(pd.DatetimeIndex(['2016-06-30 12:32'])).dt.date + pd.offsets.MonthEnd()
# 0   2016-07-31
# dtype: datetime64[ns]

maybe you expect .rollforward, but it doesn't support vectorization.

pd.offsets.QuarterEnd().rollforward(pd.Timestamp('2016-06-30 12:32'))
# Timestamp('2016-06-30 12:32:00')

For the second issue, you can specify startingMonth (see #8435 for discussion to change the default)

pd.Series(pd.DatetimeIndex(['2016-04-01 04:02'])).dt.date - pd.offsets.QuarterBegin(startingMonth=1)
# 0   2016-01-01
# dtype: datetime64[ns]

@jreback
Copy link
Contributor

jreback commented Jul 14, 2016

cc @chris-b1

@chris-b1
Copy link
Contributor

chris-b1 commented Jul 14, 2016

You want an offset with n=0 - that has the semantics of not rolling the date forward if on an anchor (e.g., a QuarterEnd) and rolling forward otherwise. n=1, the default always rolls forward. See docs here

In [13]: pd.Series(pd.DatetimeIndex(['2016-06-30 12:32'])).dt.date + pd.offsets.QuarterEnd(n=0)
Out[13]:
0   2016-06-30
dtype: datetime64[ns]

@jreback
Copy link
Contributor

jreback commented Jul 14, 2016

ok closing this, but if anyone thinks docs are not clear a pr would be great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants