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

BUG: Grouper: Origin param has no effect #47653

Open
3 tasks done
houseofai opened this issue Jul 9, 2022 · 10 comments
Open
3 tasks done

BUG: Grouper: Origin param has no effect #47653

houseofai opened this issue Jul 9, 2022 · 10 comments
Assignees
Labels
Bug Frequency DateOffsets Groupby Needs Discussion Requires discussion from core team before further action

Comments

@houseofai
Copy link

Pandas version checks

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

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

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

df = pd.DataFrame([{"A": A, "datadate": datadate} 
                         for A in range(1,3) 
                         for datadate in pd.date_range(start='1/2/2022', end='2/1/2022', freq='D')])

ddg = df.groupby(["A", pd.Grouper(key="datadate", freq='W', origin='start')])

for i, dfg in ddg:
    print(dfg[["A", "datadate"]])
    print("-----------------------")


ddg = df.groupby(["A", pd.Grouper(key="datadate", freq='W', origin='1/5/2022')])

for i, dfg in ddg:
    print(dfg[["A", "datadate"]])
    print("-----------------------")

Issue Description

Whatever I set on the origin parameter of pd.Grouper, whether it is startor a date, it groups by starting on a Monday.
If I remove the origin parameter, I get the same results.

Expected Behavior

The data are grouped weekly starting by the fixed timestamp provided by the origin parameter.

Installed Versions

INSTALLED VERSIONS

commit : e8093ba
python : 3.8.13.final.0
python-bits : 64
OS : Darwin
OS-release : 21.5.0
Version : Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:37 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.4.3
numpy : 1.21.6
pytz : 2022.1
dateutil : 2.8.2
setuptools : 61.2.0
pip : 21.2.4
Cython : 0.29.30
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.0
html5lib : None
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.1
IPython : 8.2.0
pandas_datareader: None
bs4 : 4.9.1
bottleneck : None
brotli : None
fastparquet : None
fsspec : 2022.5.0
gcsfs : None
markupsafe : 2.1.1
matplotlib : 3.5.2
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : 8.0.0
pyreadstat : None
pyxlsb : None
s3fs : 2022.5.0
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.32
tables : None
tabulate : 0.8.10
xarray : None
xlrd : None
xlwt : None
zstandard : None

@houseofai houseofai added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 9, 2022
@hamedgibago
Copy link

take

@hamedgibago
Copy link

@houseofai For the second grouper, first start day which printed should be 1 2022-01-05?

@houseofai
Copy link
Author

@hamedgibago Yes, thanks

@DBCerigo
Copy link

DBCerigo commented Sep 5, 2022

Any news on this? If triage has been done could an update be given on whether it is a confirmed bug? Thanks.

@jreback
Copy link
Contributor

jreback commented Sep 5, 2022

not a bug

W is an anchored offset which starts on monday; you can use W-TUES for example as well

see https://pandas.pydata.org/docs/search.html?q=Anchored

@DBCerigo
Copy link

DBCerigo commented Sep 5, 2022

@jreback thanks a lot for the reply, my apologies for the time used for the correction.

@houseofai
Copy link
Author

@jreback

df.groupby(["A", pd.Grouper(key="datadate", freq='W-TUES', origin='start')])

throws ValueError: Invalid frequency: W-TUES

@DBCerigo
Copy link

DBCerigo commented Sep 5, 2022

@houseofai
Copy link
Author

@DBCerigo Thanks for pointing out, indeed W-TUE doesn't raise an error.

ddg = df.groupby(["A", pd.Grouper(key="datadate", freq='W-TUE')])

for i, dfg in ddg:
    print(dfg[["A", "datadate"]])
    print("-----------------------")

Gives:

   A   datadate
0  1 2022-01-02
1  1 2022-01-03
2  1 2022-01-04
-----------------------
   A   datadate
3  1 2022-01-05
4  1 2022-01-06
5  1 2022-01-07
6  1 2022-01-08
7  1 2022-01-09
8  1 2022-01-10
9  1 2022-01-11
-----------------------

Note that the first date,2022-01-05, is a Wednesday.

The only way to start the week from the first date is to find the day of the first date (here 2022-01-02 - Sunday), and to set it to the day before:

ddg = df.groupby(["A", pd.Grouper(key="datadate", freq='W-SAT')])
 A   datadate
0  1 2022-01-02
1  1 2022-01-03
2  1 2022-01-04
3  1 2022-01-05
4  1 2022-01-06
5  1 2022-01-07
6  1 2022-01-08
-----------------------
    A   datadate
7   1 2022-01-09
8   1 2022-01-10
9   1 2022-01-11
...

Could you please confirm that is the expected behavior with the frequency parameter?

And I insist but the origin parameter doesn't have any effect.

@rhshadrach
Copy link
Member

Looking at #31809, it appears to me origin was specifically added for day and sub-day frequencies. If this is the case and it's intended to have no impact on frequencies coarser than a day, we should raise when provided (after deprecating the current behavior).

@rhshadrach rhshadrach added Groupby Frequency DateOffsets Needs Discussion Requires discussion from core team before further action and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Frequency DateOffsets Groupby Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

5 participants