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

This issue has been created 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

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-09-29
Star Count 35374
Watcher Count 1122
Fork Count 15034
Issue Count 3579

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Question about pointcloud sampler 0 2021-12-17 2022-07-29
Full text search: Unable to escape certain characters in Sphinx 3 2021-09-23 2022-09-15
No module named 'psbody' 1 2020-12-15 2022-05-16
Torch hub command not working 10 2019-10-29 2022-09-24
Some notable differences with official implementation 9 2019-10-28 2022-09-24
Service NoSuchFileException with Paper 1.16.5 on latest stable CloudNet Build (Build #11 (10.12.2021, 18:55:47) in branch 3.4/ver/1.18 2 2021-12-11 2022-08-26
Suprising mut/const error message 0 2022-05-02 2022-08-29
SSHFP records for root domain name should be created when no login exists 0 2022-05-06 2022-08-11
Design colours unreadable on Android 2 2010-08-09 2022-09-17
Atomics error on 32 bit SBCL 1 2021-02-21 2022-09-17
Background threaded multi-execute module 0 2022-08-23 2022-09-16
Save Settings for each theme automatically 3 2022-05-05 2022-09-01
ACTION REQUIRED: Microsoft needs this private repository to complete compliance info 0 2020-11-20 2022-09-15
KeyError: "Registering two gradient with name 'Gelu'! 3 2021-12-23 2022-09-22
[技术咨询]有计划支持RTSP编码类型为MP4V-ES吗? 3 2022-04-13 2022-09-03
[技术咨询] docker 收流失败,断开,耗时(s):0 2 2022-04-15 2022-09-24
Copied text is lost after window application quits 2 2022-02-15 2022-09-14
Error in training with the setting of "model type" -"Openpose" and "model.data_format"-"Channels_first" 0 2021-02-04 2022-09-18
another GSUB difference between harfbuzz and MS Word 7 2022-04-28 2022-09-14
Installer breaks unattended install on openbsd by returning a non 0 exit code 4 2021-11-15 2022-09-25
error when starting game 1 2022-08-29 2022-09-10
Fix syslink DMA implementation 0 2022-09-01 2022-09-25
Closed as not an issue 2 2022-06-03 2022-09-25
Private key password 1 2022-07-27 2022-09-16
Twitter embed is heavy and is often adblocked 0 2022-04-30 2022-09-18
snapshot_exclude' is deprecated, 'backup_exclude' should be used instead 1 2021-09-13 2022-09-22
Turn results into report card form 1 2016-01-16 2022-08-08
Unable to add recipient with subdomain 3 2021-02-07 2022-09-27
gnet.Server 在aarch64 Ubuntu18上无法监听在“0.0.0.0” 1 2022-08-05 2022-09-25
node_modules symlinking is incompatible with remote execution (bazel buildfarm) 6 2022-04-04 2022-09-25
Add "halt-on-failure" option 0 2022-03-17 2022-08-25
[Feature] Track source code, diff between runs 0 2021-09-01 2022-09-21
Naming Convention for Custom Attributes 1 2022-05-20 2022-09-17
npm install canvas ERROR 2 2022-03-04 2022-09-27
Update to 1.46.10 from 1.46.9 broken NextCloud instance! 2 2022-04-04 2022-09-24
column name display text overflow in shared base 1 2022-05-20 2022-09-25
Add Royalties for NFTs 4 2021-12-15 2022-09-10
ClusterIP for Services using Fedora CoreOS v36.20220618.3.1 0 2022-07-10 2022-09-10
[BUG] Geomap - WMS request includes plugin parameters 0 2021-05-09 2022-09-14
PriorityQueue is not reordered on .clone in scala >= 2.12 8 2021-12-22 2022-09-08
Reduce gdbstub ROM usage 0 2017-06-01 2022-09-20
version 2.9, tuntap will crush sometimes for some reason 2 2021-09-23 2022-09-25
Failure to create an SDL2 GL context on Linux 3 2022-05-15 2022-07-21
Launch binaries failed with "Error 1406: Data too long for column 'data_value' at row 1" 1 2022-06-02 2022-09-19
Stain unmixing with DCGAN 1 2022-03-01 2022-09-19
Indexation of null and default values 0 2022-03-31 2022-09-11
I want to get property layerX and layerY from PointerEvent in lib.dom.d.ts 1 2021-12-04 2022-09-14
Issue with developing Node CLI with Volta 1 2021-11-19 2022-09-29
Lost 12 words of Helium app. How to resolve ? 5 2022-06-30 2022-07-24
IDE0004 Unnecessary cast false positive 0 2022-01-12 2022-09-27