Pandas version checks

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

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

  • [X] 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 : 2e218d10984e9919f0296931d92ea851c6a6faf5 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

Comment From: topper-123

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?

Comment From: mb-hz

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

Comment From: topper-123

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.

Comment From: rhshadrach

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

Yes, it is!

Comment From: topper-123

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?

Comment From: rhshadrach

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.

Comment From: mroeschke

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

Comment From: Meadiocre

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!

Comment From: Jruth44

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.

Comment From: iangainey

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

Comment From: rhshadrach

@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.

Comment From: Jruth44

@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.

Comment From: michael2015tse

It is hands down the most popular library, but those f*ckers keep breaking their APIs with every update and never care about backward compatibility. Total bullshit!