Skip to content

BUG: expanding numba functions being cached with arguments #42287

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
2 of 3 tasks
lucaswebb opened this issue Jun 28, 2021 · 2 comments · Fixed by #42350
Closed
2 of 3 tasks

BUG: expanding numba functions being cached with arguments #42287

lucaswebb opened this issue Jun 28, 2021 · 2 comments · Fixed by #42350
Labels
Bug numba numba-accelerated operations Window rolling, ewma, expanding
Milestone

Comments

@lucaswebb
Copy link

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample

def expanding_multiply(values, x):
    arr = values[-1]
    arr[1] = values[:,0].sum() * x
    return arr

df = pd.DataFrame({'id': [1, 2, 3], 'value': [0, 0, 0]})

def call_expanding(x):
    expanded = df.expanding(method="table").apply(expanding_multiply, raw=True, engine="numba", args=x)
    return expanded

call_expanding(tuple([1]))
#  1.0    1.0
#  2.0    3.0
#  3.0    6.0
call_expanding(tuple([2]))
#  1.0    1.0
#  2.0    3.0
#  3.0    6.0

Problem description

When using expanding.apply with engine='numba' calling the same function with different arguments gives the same results as the first time it was called. I believe this is because the function is being cached with its arguments.

apply_func = generate_numba_table_func(
args, kwargs, func, engine_kwargs, f"{caller_name}_apply"
)
numba_cache_key = (func, f"{caller_name}_apply_table")

Expected Output

call_expanding(tuple([2]))
# 1.0    2.0
# 2.0    6.0
# 3.0    12.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : 2dd9e9b
python : 3.7.7.final.0
python-bits : 64
OS : Darwin
OS-release : 19.5.0
Version : Darwin Kernel Version 19.5.0: Thu Apr 30 18:25:59 PDT 2020; root:xnu-6153.121.1~7/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : en_US.UTF-8
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.3.0rc1
numpy : 1.19.1
pytz : 2019.1
dateutil : 2.8.1
pip : 19.2.3
setuptools : 41.2.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : 2.8.2 (dt dec pq3 ext lo64)
jinja2 : 3.0.1
IPython : 7.18.1
pandas_datareader: None
bs4 : None
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.3.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyxlsb : None
s3fs : None
scipy : 1.6.3
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : 0.53.1

@lucaswebb lucaswebb added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 28, 2021
@lucaswebb
Copy link
Author

lucaswebb commented Jun 28, 2021

This problem affects all windows.rolling and windows.expanding .apply functions. One solution is to pass *args in to the apply functions instead of args: tuple.

def apply(
self,
func: Callable[..., Any],
raw: bool = False,
engine: str | None = None,
engine_kwargs: dict[str, bool] | None = None,
args: tuple[Any, ...] | None = None,
kwargs: dict[str, Any] | None = None,
):

return func(x, start, end, min_periods)

becomes

return func(x, start, end, min_periods, args)

@mzeitlin11
Copy link
Member

Thanks for reporting and investigating this @lucaswebb! This looks similar to #41647, would you be interested in putting up a pr to fix this? cc @mroeschke

@mzeitlin11 mzeitlin11 added numba numba-accelerated operations Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 1, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Jul 1, 2021
@jreback jreback modified the milestones: Contributions Welcome, 1.3.1, 1.4 Jul 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug numba numba-accelerated operations Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants