BUG: the rolling.Rolling subclass cannot handle string when using apply function

This issue has been tracked since 2022-09-20.

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({"aqi_level": ["优"] * 20 + ["良"] * 20})
df.rolling(4).apply(lambda a: len(list(a)))

Issue Description

the example code raise a DataError
DataError: No numeric types to aggregate

---------------------------------------------------------------------------
DataError                                 Traceback (most recent call last)
Input In [6], in <cell line: 1>()
----> 1 df.rolling(4).apply(lambda a: len(a))

File /home/software/conda/lib/python3.9/site-packages/pandas/core/window/rolling.py:2000, in Rolling.apply(self, func, raw, engine, engine_kwargs, args, kwargs)
   1979 @doc(
   1980     template_header,
   1981     create_section_header("Parameters"),
   (...)
   1998     kwargs: dict[str, Any] | None = None,
   1999 ):
-> 2000     return super().apply(
   2001         func,
   2002         raw=raw,
   2003         engine=engine,
   2004         engine_kwargs=engine_kwargs,
   2005         args=args,
   2006         kwargs=kwargs,
   2007     )

File /home/software/conda/lib/python3.9/site-packages/pandas/core/window/rolling.py:1422, in RollingAndExpandingMixin.apply(self, func, raw, engine, engine_kwargs, args, kwargs)
   1419 else:
   1420     raise ValueError("engine must be either 'numba' or 'cython'")
-> 1422 return self._apply(
   1423     apply_func,
   1424     name="apply",
   1425     numba_args=numba_args,
   1426 )

File /home/software/conda/lib/python3.9/site-packages/pandas/core/window/rolling.py:663, in BaseWindow._apply(self, func, name, numeric_only, numba_args, **kwargs)
    660     return result
    662 if self.method == "single":
--> 663     return self._apply_blockwise(homogeneous_func, name, numeric_only)
    664 else:
    665     return self._apply_tablewise(homogeneous_func, name, numeric_only)

File /home/software/conda/lib/python3.9/site-packages/pandas/core/window/rolling.py:555, in BaseWindow._apply_blockwise(self, homogeneous_func, name, numeric_only)
    545     dropped = obj.columns.difference(obj.columns.take(taker))
    546     warnings.warn(
    547         "Dropping of nuisance columns in rolling operations "
    548         "is deprecated; in a future version this will raise TypeError. "
   (...)
    552         stacklevel=find_stack_level(inspect.currentframe()),
    553     )
--> 555 return self._resolve_output(df, obj)

File /home/software/conda/lib/python3.9/site-packages/pandas/core/window/rolling.py:450, in BaseWindow._resolve_output(self, out, obj)
    448 """Validate and finalize result."""
    449 if out.shape[1] == 0 and obj.shape[1] > 0:
--> 450     raise DataError("No numeric types to aggregate")
    451 elif out.shape[1] == 0:
    452     return obj.astype("float64")

DataError: No numeric types to aggregate

Expected Behavior

just like a single groupby apply function, output a length of every group/rolling

Installed Versions

INSTALLED VERSIONS

commit : 224458e
python : 3.9.7.final.0
python-bits : 64
OS : Linux
OS-release : 3.10.0-327.el7.x86_64
Version : #1 SMP Thu Nov 19 22:10:57 UTC 2015
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8

pandas : 1.5.0rc0
numpy : 1.21.2
pytz : 2021.3
dateutil : 2.8.2
setuptools : 58.0.4
pip : 21.2.4
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.8.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.0.3
IPython : 8.1.1
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.4
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.1
numba : 0.55.1
numexpr : 2.8.1
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.8.0
snappy : None
sqlalchemy : 1.4.13
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
zstandard : None
tzdata : None

phofl wrote this answer on 2022-09-29

cc @mroeschke

This looks odd, I think this should work in apply?

More Details About Repo
Owner Name pandas-dev
Repo Name pandas
Full Name pandas-dev/pandas
Language Python
Created Date 2010-08-24
Updated Date 2022-10-04
Star Count 35430
Watcher Count 1120
Fork Count 15089
Issue Count 3589

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date