Skip to content

BUG: Unexpected typecast on datetime.date in groupby.key() #38878

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

Closed
2 of 3 tasks
ghost opened this issue Jan 1, 2021 · 4 comments · Fixed by #39014
Closed
2 of 3 tasks

BUG: Unexpected typecast on datetime.date in groupby.key() #38878

ghost opened this issue Jan 1, 2021 · 4 comments · Fixed by #39014
Assignees
Milestone

Comments

@ghost
Copy link

ghost commented Jan 1, 2021

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.

Code Sample, a copy-pastable example

>>> import datetime as dt
>>> import pandas as pd
>>> df = pd.DataFrame([[dt.date(2021,1,1), 1, 'a'], [dt.date(2021,1,2),  2, 'b']])
>>> g = df.groupby([0, 1])
>>> g.groups.key()
... dict_keys([(Timestamp('2021-01-01 00:00:00'), 1), (Timestamp('2021-01-02 00:00:00'), 2)])

Problem description

When multiple columns pass to groupby, the datetime.date type columns cast o the Timestamp type.

Due to the unexpected type casting, following code raises a KeyError.

>>> list(g.get_group(key) for key in g.groups.keys())
... KeyError: (Timestamp('2021-01-01 00:00:00'), 1)

[this should explain why the current behaviour is a problem and why the expected output is a better solution]

This typecast will only be performed if multiple indexes are specified.

>>> g = df.groupby([0])
>>> g.groups.key()
... dict_keys([datetime.date(2021, 1, 1), datetime.date(2021, 1, 2)])

Expected Output

>>> g.groups.key()
... dict_keys([(datetime.date(2021, 1, 1), 1), (datetime.date(2021, 1, 2), 2)])

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 3e89b4c
python : 3.8.7.final.0
python-bits : 64
OS : Darwin
OS-release : 20.2.0
Version : Darwin Kernel Version 20.2.0: Wed Dec 2 20:39:59 PST 2020; root:xnu-7195.60.75~1/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : ja_JP.UTF-8
pandas : 1.2.0
numpy : 1.19.4
pytz : 2020.5
dateutil : 2.8.1
pip : 20.3.3
setuptools : 51.1.1
Cython : 0.29.21
pytest : 6.2.1
hypothesis : None
sphinx : 3.4.1
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.11.2
IPython : 7.19.0
pandas_datareader: None
bs4 : 4.9.3
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.3
numexpr : 2.7.2
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.0
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.52.0

@ghost ghost added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 1, 2021
@ghost ghost changed the title BUG: unexpe BUG: Unexpected typecast on datetime.date in groupby.key() Jan 1, 2021
@simonjayhawkins
Copy link
Member

Thanks @ktrxcmsn for the report

master gives the expected output

>>> pd.__version__
'1.3.0.dev0+392.ge0327f709f'
>>>
>>> df = pd.DataFrame([[dt.date(2021, 1, 1), 1, "a"], [dt.date(2021, 1, 2), 2, "b"]])
>>> g = df.groupby([0, 1])
>>> g.groups.keys()
dict_keys([(datetime.date(2021, 1, 1), 1), (datetime.date(2021, 1, 2), 2)])

not yet checked which commit changed this or checked the release notes whether it was an intentional change

further investigation welcome.

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Jan 1, 2021
@simonjayhawkins
Copy link
Member

fixed in [545a942] BUG: Index([date]).astype("category").astype(object) roundtrip (#38552)

tests were changed in that PR, so I don't think we need any further tests.

However, the release note may need to be changed to better clarify the changes @jbrockmendel wdyt?

@simonjayhawkins simonjayhawkins added Groupby Datetime Datetime data dtype MultiIndex and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 1, 2021
@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Jan 1, 2021
@jbrockmendel
Copy link
Member

could use an additional whatsnew, yah

@vangorade
Copy link
Contributor

take

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

Successfully merging a pull request may close this issue.

4 participants