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

ENH: rolling apply multiple columns or whole dataframe #53235

Open
3 tasks done
mb-hz opened this issue May 15, 2023 · 12 comments · May be fixed by #58501
Open
3 tasks done

ENH: rolling apply multiple columns or whole dataframe #53235

mb-hz opened this issue May 15, 2023 · 12 comments · May be fixed by #58501
Labels
Enhancement Window rolling, ewma, expanding

Comments

@mb-hz
Copy link

mb-hz commented May 15, 2023

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

value4 = 10
weight3 = 2
weight2 = 3
data = {'value': [np.nan, np.nan, np.nan, value4],
        'weight': [np.nan, np.nan, weight3, weight2]}
df = pd.DataFrame(data)

df.rolling(window=2, min_periods=1).apply(lambda x: x['value'][0] * x['weight'][0], raw=False)

Issue Description

If you do for i in df.rolling(window=2, min_periods=1): print(i), you can see that i includes both columns. However, when you use df.rolling with df.apply function, the function can not recognise both columns.

Expected Behavior

I expect the rolling function can return multiple columns as it shows in for loop print, into apply function after it, when we use dataframe instead of series or array as the input.

Installed Versions

INSTALLED VERSIONS

commit : 2e218d1
python : 3.11.1.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 154 Stepping 4, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United Kingdom.1252

pandas : 1.5.3
numpy : 1.23.5
pytz : 2022.7.1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 23.0.1
Cython : 0.29.33
pytest : 7.2.2
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.12.0
pandas_datareader: None
bs4 : 4.12.0
bottleneck : None
brotli : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.7.1
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

@mb-hz mb-hz added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 15, 2023
@mb-hz mb-hz changed the title BUG: BUG: rolling apply multiple columns or whole dataframe May 15, 2023
@topper-123
Copy link
Contributor

That's what the parameter method="table" is for. It is however only implemented when given engine="numba" in the method, so is a bit of a hit & miss for custom functions given to apply.

So, this is currently more of an limitation of the current API. and changing it,so method="table" also works for non-numba functions, could be worth an investigation but would be an enhancement proposal, not a bug fix IMO.

Is rolling.apply part of your work on fixing groupby.apply, @rhshadrach?

@topper-123 topper-123 added the Window rolling, ewma, expanding label May 16, 2023
@mb-hz mb-hz closed this as completed May 19, 2023
@mb-hz mb-hz reopened this May 19, 2023
@mb-hz
Copy link
Author

mb-hz commented May 19, 2023

Hi topper-123, thank you so much for your reply.

The info for reference of numba is limited. I suppose this code should work, replacing the last row of code in the question.
df.rolling(window=2, min_periods=1, method="table").apply(lambda x: x, raw=True, engine='numba'), which returns an error. ValueError: cannot broadcast source array for assignment

@topper-123
Copy link
Contributor

I am not familiar with the restrictions on when numba works with rolling.apply, but yeah I tried that too and it didn't work, I haven't checked why...

I looked through the issue list, and there is already #40374, which started out as a enhancement proposal, but ended up. as a doc issue, but is still open, though the related doc issue has been resolved. So IDK, can you rephrase this to be an enhancement proposal, requesting to method="table" in rolling work outside of numba also? Then I'll close the other as completed.

@rhshadrach
Copy link
Member

Is rolling.apply part of your work on fixing groupby.apply, @rhshadrach?

Yes, it is!

@topper-123
Copy link
Contributor

Is rolling.apply part of your work on fixing groupby.apply, @rhshadrach?

Yes, it is!

Nice. Would we then be able to roll over a DataFrame like requested here?

@rhshadrach
Copy link
Member

Yes - I am aiming to add user-control as to whether the columns or the entire frame is passed to the UDF across the board, so in particular in rolling.

@mroeschke
Copy link
Member

I am not familiar with the restrictions on when numba works with rolling.apply, but yeah I tried that too and it didn't work, I haven't checked why...

For context, this was done because the current window aggregations (including apply) in cython were only written to accepts 1D inputs. At the time, it wasn't in my scope to rewrite them to 2D

@topper-123 topper-123 changed the title BUG: rolling apply multiple columns or whole dataframe ENH: rolling apply multiple columns or whole dataframe May 26, 2023
@topper-123 topper-123 added Enhancement and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 26, 2023
@Meadiocre
Copy link

Hello @mb-hz and @mroeschke,

My project team is looking for Pandas enhancement features for our grad school semester long project. We saw this task and would like to contribute if possible!

@Jruth44
Copy link

Jruth44 commented Mar 17, 2024

Hello @mroeschke

Has this issue been fixed on your end with your groupby.apply fix? I am currently working on trying to modify the Rolling class _apply function to do this, but don't want to double work.

@iangainey
Copy link
Contributor

To go with the above comment, @rhshadrach where you mention aiming to add user control on this, would or does this fix this issue?

@rhshadrach
Copy link
Member

@iangainey - adding user control on what is given to the passed callable (column-by-column or the entire frame at once) would close this issue. Currently there is no concrete proposal for this and it is something I plan to take up eventually - but would be happy if someone else takes it up. That being said, I would like such a proposal to encompass agg, apply, transform, filter (in some instances), and map across Series, DataFrame, groupby, rolling, and resample rather than going piece-meal creating a (more) inconsistent API.

@Jruth44
Copy link

Jruth44 commented Mar 19, 2024

@rhshadrach Hi Richard, thanks for the response! I currently have a working fix in the scope of this issue for rolling.apply by adding user control with a multi_column = true parameter. Is it a requirement that the parameter to be all encompassing, or would you accept a solution that would only close this issue that could be incorporated into a bigger multi_column fix later on.

JPL-JUNO added a commit to JPL-JUNO/Python-3 that referenced this issue Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants