Skip to content

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

Closed
@mvashishtha

Description

@mvashishtha

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugWindowrolling, ewma, expanding

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions