-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: groupby rolling apply giving strange index #49878
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
Comments
maybe related to #48056 |
seems related to #47494 and #45925 too @mroeschke |
I believe the resulting index is correct here but the values are ordered incorrectly. Investigation and pull requests welcome! |
from what I could debug until now, in https://github.com/pandas-dev/pandas/blob/main/pandas/core/window/rolling.py#L1417, "values" is the original values reordered by groups and self._on is the original index, so the resulting Series has mismatched index/values. |
@mroeschke, adding the code below right after https://github.com/pandas-dev/pandas/blob/main/pandas/core/window/rolling.py#L874 seems to fix my problem
This code (https://github.com/pandas-dev/pandas/blob/main/pandas/core/window/rolling.py#L871) is reordering the values considering groups but later it is used with the index not ordered by groups (https://github.com/pandas-dev/pandas/blob/main/pandas/core/window/rolling.py#L1417). My code above set the index to the ordered by groups index. But I have no idea if this is the right fix. It seems not. |
take |
Hi @mroeschke, I think the problem may be here. pandas/pandas/core/window/rolling.py Lines 1418 to 1425 in e80dc87
When I want to confirm that |
This is a more concise failure case. import pandas as pd
def f(x):
return x.index.values
df = pd.DataFrame([(1, 10, 'A'), (2, 15, 'B'), (3, 20, 'A')],
columns=['idx', 'value', 'group']).set_index('idx')
gb = df.groupby('group')
gbr = gb.rolling(1)
# list(gbr) # in right order
gbr.apply(f) # wrong
# value
# group idx
# A 1 1.0
# 3 2.0
# B 2 3.0 |
I apologize for providing incorrect information earlier. Upon further examination, it appears that the pandas/pandas/core/window/rolling.py Lines 1418 to 1425 in e80dc87
In the I have two ideas for solving this issue. The first is to add some documentation to inform users not to attempt to get some from groupbyObject's index. If they do need, they should create a copy and add a new column. The second idea is to find a way to add some information about the modified groupbyObject index. Please let me know if you have any advice or if I have misunderstood anything. politely cc @mroeschke |
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
Issue Description
I'm trying to apply a custom function using rolling apply and using the date index of the window, but the index of the data passed to the apply function seems incorrect
output of above rolling apply:
Expected Behavior
Installed Versions
INSTALLED VERSIONS
commit : 8dab54d
python : 3.8.10.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-52-generic
Version : #58~20.04.1-Ubuntu SMP Thu Oct 13 13:09:46 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : pt_BR.UTF-8
pandas : 1.5.2
numpy : 1.22.3
pytz : 2021.3
dateutil : 2.8.2
setuptools : 45.2.0
pip : 22.3.1
Cython : None
pytest : 4.6.9
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.1
html5lib : 1.0.1
pymysql : 1.0.2
psycopg2 : None
jinja2 : 3.1.2
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.8.2
bottleneck : None
brotli : 1.0.9
fastparquet : None
fsspec : 2022.11.0
gcsfs : None
matplotlib : 3.6.0
numba : 0.56.4
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : 7.0.0
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.3.3
snappy : None
sqlalchemy : None
tables : 3.6.1
tabulate : 0.8.9
xarray : None
xlrd : 1.1.0
xlwt : 1.3.0
zstandard : None
tzdata : None
The text was updated successfully, but these errors were encountered: