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: rolling with axis=1, win_type=, and center=True raises ValueError #46135

Closed
3 tasks done
mvashishtha opened this issue Feb 23, 2022 · 0 comments · Fixed by #46265
Closed
3 tasks done

BUG: rolling with axis=1, win_type=, and center=True raises ValueError #46135

mvashishtha opened this issue Feb 23, 2022 · 0 comments · Fixed by #46265
Labels
Bug Window rolling, ewma, expanding

Comments

@mvashishtha
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([[0]])
# this works:
print(df.rolling(window=1, axis=0, win_type="boxcar", center=True).sum())
# This raises ValueError:
print(df.rolling(window=1, axis=1, win_type="boxcar", center=True).sum())

Issue Description

I start with a dataframe with a single value, 0. Rolling over columns with the above parameters gives:

     0
0  3.0

Rolling over rows with axis=1 instead gives ValueError: Requested axis is larger then no. of argument dimensions:

Show stack trace
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [48], in <module>
----> 1 df.rolling(window=1, axis=1, win_type="boxcar", center=True).sum()

File /usr/local/lib/python3.9/site-packages/pandas/core/window/rolling.py:1216, in Window.sum(self, *args, **kwargs)
   1212 window_func = window_aggregations.roll_weighted_sum
   1213 # error: Argument 1 to "_apply" of "Window" has incompatible type
   1214 # "Callable[[ndarray, ndarray, int], ndarray]"; expected
   1215 # "Callable[[ndarray, int, int], ndarray]"
-> 1216 return self._apply(window_func, name="sum", **kwargs)

File /usr/local/lib/python3.9/site-packages/pandas/core/window/rolling.py:1154, in Window._apply(self, func, name, numba_cache_key, numba_args, **kwargs)
   1150         result = self._center_window(result, offset)
   1152     return result
-> 1154 return self._apply_blockwise(homogeneous_func, name)

File /usr/local/lib/python3.9/site-packages/pandas/core/window/rolling.py:462, in BaseWindow._apply_blockwise(self, homogeneous_func, name)
    459 for i, arr in enumerate(obj._iter_column_arrays()):
    460     # GH#42736 operate column-wise instead of block-wise
    461     try:
--> 462         res = hfunc(arr)
    463     except (TypeError, NotImplementedError):
    464         pass

File /usr/local/lib/python3.9/site-packages/pandas/core/window/rolling.py:452, in BaseWindow._apply_blockwise.<locals>.hfunc(values)
    450 def hfunc(values: ArrayLike) -> ArrayLike:
    451     values = self._prep_values(values)
--> 452     return homogeneous_func(values)

File /usr/local/lib/python3.9/site-packages/pandas/core/window/rolling.py:1150, in Window._apply.<locals>.homogeneous_func(values)
   1147     result = np.asarray(calc(values))
   1149 if self.center:
-> 1150     result = self._center_window(result, offset)
   1152 return result

File /usr/local/lib/python3.9/site-packages/pandas/core/window/rolling.py:1092, in Window._center_window(self, result, offset)
   1088 """
   1089 Center the result in the window for weighted rolling aggregations.
   1090 """
   1091 if self.axis > result.ndim - 1:
-> 1092     raise ValueError("Requested axis is larger then no. of argument dimensions")
   1094 if offset > 0:
   1095     lead_indexer = [slice(None)] * result.ndim

ValueError: Requested axis is larger then no. of argument dimensions

Expected Behavior

I expect rolling over axis 1 to give the same result as rolling over axis 0 for this single-element dataframe.

Installed Versions

INSTALLED VERSIONS

commit : b4e578d
python : 3.9.10.final.0
python-bits : 64
OS : Darwin
OS-release : 21.3.0
Version : Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.0.dev0+122.gb4e578db85
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 22.0.3
setuptools : 60.8.2
Cython : 0.29.27
pytest : 7.0.0
hypothesis : None
sphinx : 4.4.0
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : 2.9.3
jinja2 : 3.0.3
IPython : 8.0.0
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : 2022.01.0
gcsfs : None
matplotlib : 3.5.1
numba : None
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.9
pandas_gbq : 0.16.0
pyarrow : 6.0.1
pyreadstat : None
pyxlsb : None
s3fs : 2022.01.0
scipy : 1.7.3
sqlalchemy : 1.4.31
tables : 3.7.0
tabulate : None
xarray : 0.20.2
xlrd : 2.0.1
xlwt : None
zstandard : None

@mvashishtha mvashishtha added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 23, 2022
@mroeschke mroeschke added Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 24, 2022
mroeschke pushed a commit that referenced this issue May 6, 2022
* TST: added test for rolling with centering=True and axis=1 (#46135)

* BUG: fixed bug of rolling with centering=True and axis=1 (#46135)

* DOC: fixed bug of rolling with centering=True and axis=1 (#46135)

* Fixes from pre-commit [automated commit]

* TST: adapted test_rolling_center_axis_1 to support check_dtype=True

* Fixes from pre-commit [automated commit]

* TST: move test_rolling_center_axis_1 from test_rolling.py to test_win_type.py

* Fixes from pre-commit [automated commit]

* DOC: moved bug mention to rolling doc section and specify conditions (#46135)

Co-authored-by: Yuval <yuval,jacoby1@mail.huji.ac.il>
yehoshuadimarsky pushed a commit to yehoshuadimarsky/pandas that referenced this issue Jul 13, 2022
…-dev#46265)

* TST: added test for rolling with centering=True and axis=1 (pandas-dev#46135)

* BUG: fixed bug of rolling with centering=True and axis=1 (pandas-dev#46135)

* DOC: fixed bug of rolling with centering=True and axis=1 (pandas-dev#46135)

* Fixes from pre-commit [automated commit]

* TST: adapted test_rolling_center_axis_1 to support check_dtype=True

* Fixes from pre-commit [automated commit]

* TST: move test_rolling_center_axis_1 from test_rolling.py to test_win_type.py

* Fixes from pre-commit [automated commit]

* DOC: moved bug mention to rolling doc section and specify conditions (pandas-dev#46135)

Co-authored-by: Yuval <yuval,jacoby1@mail.huji.ac.il>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Window rolling, ewma, expanding
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants