Skip to content

df.groupby(cols).tshift(0, freq) Drops Index Level When Shift is Zero #22053

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
avrixtor opened this issue Jul 25, 2018 · 3 comments
Closed

df.groupby(cols).tshift(0, freq) Drops Index Level When Shift is Zero #22053

avrixtor opened this issue Jul 25, 2018 · 3 comments
Labels
Groupby MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode

Comments

@avrixtor
Copy link

Code Sample, a copy-pastable example if possible

import pandas as pd

df = pd.DataFrame({"numerical_col": [-1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
                   "date": ["2018-01-01", "2018-01-01", "2018-01-01", "2018-02-01", "2018-02-01",
                               "2018-03-01", "2018-01-01", "2018-01-01", "2018-02-01", "2018-02-01"],
                   "categories": ["a", "a", "a", "a", "a", "a", "b", "b", "b", "b"]})

df["date"] = pd.to_datetime(df["date"])
df.set_index("date").groupby("categories").tshift(0, "M")

Problem description

In short words, tshift after a groupby deletes the groupby columns ("categories" in this case) if the shift used was zero

In [14]: df.set_index("date").groupby("categories").tshift(0, "M")
Out[14]: 
            numerical_col
date                     
2018-01-01             -1
2018-01-01             -2
2018-01-01             -3
2018-02-01             -4
2018-02-01             -5
2018-03-01             -6
2018-01-01             -7
2018-01-01             -8
2018-02-01             -9
2018-02-01            -10

Expected Output

NOTE: This is the output format of any non zero shift (of course with the dates shifted)

In [15]: df.set_index("date").groupby("categories").tshift(0, "M")
Out[15]: 
                       numerical_col
categories date                     
a          2018-01-01             -1
           2018-01-01             -2
           2018-01-01             -3
           2018-02-01             -4
           2018-02-01             -5
           2018-03-01             -6
b          2018-01-01             -7
           2018-01-01             -8
           2018-02-01             -9
           2018-02-01            -10

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 3.6.5.final.0
python-bits: 64
OS: Linux
OS-release: 4.17.3-200.fc28.x86_64
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: en_US.UTF-8

pandas: 0.23.3
pytest: None
pip: 9.0.3
setuptools: 39.2.0
Cython: 0.28.4
numpy: 1.14.5
scipy: 1.1.0
pyarrow: None
xarray: None
IPython: 6.4.0
sphinx: None
patsy: None
dateutil: 2.7.3
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.2.2
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: None
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@WillAyd
Copy link
Member

WillAyd commented Jul 25, 2018

Thanks for the report - investigation and PRs are always welcome

@WillAyd WillAyd added this to the Contributions Welcome milestone Jul 25, 2018
@WillAyd WillAyd changed the title df.groupby(cols).tshift(0, freq) deletes cols when shift is zero df.groupby(cols).tshift(0, freq) Drops Index Level When Shift is Zero Jul 25, 2018
@WillAyd WillAyd added Indexing Related to indexing on series/frames, not to indexes themselves MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode and removed Indexing Related to indexing on series/frames, not to indexes themselves labels Jul 25, 2018
@avrixtor
Copy link
Author

avrixtor commented Jul 25, 2018

Now that I think about it... that groupby is not really necessary for this process, I could just shift the date directly. What concerns me is that when tshifting with shift == 0 is not compatible with shift != 0, but maybe that is something intrinsic to tshift

@WillAyd
Copy link
Member

WillAyd commented Nov 26, 2018

See comment in #23918 . The inconsistencies a problem though I think the intended behavior here is the opposite of what is requested, as the request would break alignment with the original caller.

Closing this in favor of the other change

@WillAyd WillAyd closed this as completed Nov 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Groupby MultiIndex Reshaping Concat, Merge/Join, Stack/Unstack, Explode
Projects
None yet
Development

No branches or pull requests

2 participants