Skip to content

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

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
3 tasks done
shuimei opened this issue Sep 20, 2022 · 2 comments
Closed
3 tasks done
Labels
Duplicate Report Duplicate issue or pull request Window rolling, ewma, expanding

Comments

@shuimei
Copy link

shuimei commented Sep 20, 2022

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

@shuimei shuimei added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 20, 2022
@phofl
Copy link
Member

phofl commented Sep 29, 2022

cc @mroeschke

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

@phofl phofl added Window rolling, ewma, expanding and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 29, 2022
@mroeschke
Copy link
Member

Thanks for the report.

All rolling ops (cython and numba) are only written for float64 data, so this is an expected limitation. I think this is a duplicate of #23002 so closing as a duplicate

@mroeschke mroeschke added Duplicate Report Duplicate issue or pull request and removed Bug labels Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate Report Duplicate issue or pull request Window rolling, ewma, expanding
Projects
None yet
Development

No branches or pull requests

3 participants