Skip to content

BUG: RecursionError when apply generic alias as a func #61565

Closed
@MacroBull

Description

@MacroBull

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
pd.DataFrame({'x': [1], 'y': [2]}).apply(list, axis="columns")
pd.DataFrame({'x': [1], 'y': [2]}).apply(list[int], axis="columns")

Issue Description

Traceback:

[... skipping similar frames: Series.apply at line 4935 (593 times), NDFrameApply.agg_or_apply_list_like at line 744 (592 times), SeriesApply.apply at line 1412 (592 times), Apply.apply_list_or_dict_like at line 630 (592 times), Apply.compute_list_like at line 369 (592 times)]

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/apply.py:1412, in SeriesApply.apply(self)
    ...

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/apply.py:630, in Apply.apply_list_or_dict_like(self)
    ...

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/apply.py:744, in NDFrameApply.agg_or_apply_list_like(self, op_name)
    ...

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/apply.py:369, in Apply.compute_list_like(self, op_name, selected_obj, kwargs)
    ...

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/series.py:4935, in Series.apply(self, func, convert_dtype, args, by_row, **kwargs)
    ...

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/apply.py:1407, in SeriesApply.apply(self)
   1404 def apply(self) -> DataFrame | Series:
   1405     obj = self.obj
-> 1407     if len(obj) == 0:
   1408         return self.apply_empty_result()
   1410     # dispatch to handle list-like or dict-like

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/series.py:918, in Series.__len__(self)
    914 def __len__(self) -> int:
    915     """
    916     Return the length of the Series.
    917     """
--> 918     return len(self._mgr)

File /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/internals/base.py:76, in DataManager.__len__(self)
     74 @final
     75 def __len__(self) -> int:
---> 76     return len(self.items)

RecursionError: maximum recursion depth exceeded
> /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/internals/base.py(76)__len__()
     74     @final
     75     def __len__(self) -> int:
---> 76         return len(self.items)
     77 
     78     @property

ipdb>

where self.func is regarded as list of funcs, which lead to the bug

> /opt/homebrew/envs/pandera/lib/python3.12/site-packages/pandas/core/apply.py(1412)apply()
   1410         # dispatch to handle list-like or dict-like
   1411         if is_list_like(self.func):
-> 1412             return self.apply_list_or_dict_like()
   1413 
   1414         if isinstance(self.func, str):

ipdb> p self.func
*list[int]
ipdb> p is_list_like(self.func)
True
ipdb>

Expected Behavior

Expected output:

0 [1, 2]
dtype: object

Installed Versions

INSTALLED VERSIONS

commit : 2cc3762
python : 3.12.9
python-bits : 64
OS : Darwin
OS-release : 24.5.0
Version : Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:33 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T8122
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : zh_CN.UTF-8
LOCALE : zh_CN.UTF-8

pandas : 2.3.0
numpy : 2.2.6
pytz : 2025.2
dateutil : 2.9.0.post0
pip : 25.0.1
Cython : None
sphinx : None
IPython : 8.31.0
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
blosc : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
html5lib : None
hypothesis : None
gcsfs : None
jinja2 : None
lxml.etree : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
psycopg2 : None
pymysql : None
pyarrow : None
pyreadstat : None
pytest : 8.3.4
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlsxwriter : None
zstandard : 0.23.0
tzdata : 2025.2
qtpy : None
pyqt5 : None

Metadata

Metadata

Assignees

Labels

BugNeeds TriageIssue that has not been reviewed by a pandas team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions