From 1390f74bfe74ab914de3840c05307a36f972e9b9 Mon Sep 17 00:00:00 2001 From: heoh Date: Mon, 9 Jun 2025 18:48:42 +0000 Subject: [PATCH] BUG: Fix RecursionError when apply native container types as a func (#61565) --- doc/source/whatsnew/v3.0.0.rst | 1 + pandas/_libs/lib.pyx | 3 ++- pandas/tests/dtypes/test_inference.py | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 03a386708323d..b76d722be4e6d 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -884,6 +884,7 @@ Other - Bug in :func:`eval` with ``engine="numexpr"`` returning unexpected result for float division. (:issue:`59736`) - Bug in :func:`to_numeric` raising ``TypeError`` when ``arg`` is a :class:`Timedelta` or :class:`Timestamp` scalar. (:issue:`59944`) - Bug in :func:`unique` on :class:`Index` not always returning :class:`Index` (:issue:`57043`) +- Bug in :meth:`DataFrame.apply` raising ``RecursionError`` when passing ``func=list[int]``. (:issue:`61565`) - Bug in :meth:`DataFrame.apply` where passing ``engine="numba"`` ignored ``args`` passed to the applied function (:issue:`58712`) - Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which caused an exception when using NumPy attributes via ``@`` notation, e.g., ``df.eval("@np.floor(a)")``. (:issue:`58041`) - Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which did not allow to use ``tan`` function. (:issue:`55091`) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index cded299f77876..3b7d659c2150e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -2,6 +2,7 @@ from collections import abc from decimal import Decimal from enum import Enum from sys import getsizeof +from types import GenericAlias from typing import ( Literal, _GenericAlias, @@ -1298,7 +1299,7 @@ cdef bint c_is_list_like(object obj, bint allow_sets) except -1: getattr(obj, "__iter__", None) is not None and not isinstance(obj, type) # we do not count strings/unicode/bytes as list-like # exclude Generic types that have __iter__ - and not isinstance(obj, (str, bytes, _GenericAlias)) + and not isinstance(obj, (str, bytes, _GenericAlias, GenericAlias)) # exclude zero-dimensional duck-arrays, effectively scalars and not (hasattr(obj, "ndim") and obj.ndim == 0) # exclude sets if allow_sets is False diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index db98751324ebc..7fd0395009adb 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -250,6 +250,15 @@ class MyDataFrame(DataFrame, Generic[T]): ... assert inference.is_list_like(tst) +def test_is_list_like_native_container_types(): + # GH 61565 + # is_list_like was yielding false positives for native container types + assert not inference.is_list_like(list[int]) + assert not inference.is_list_like(list[str]) + assert not inference.is_list_like(tuple[int]) + assert not inference.is_list_like(tuple[str]) + + def test_is_sequence(): is_seq = inference.is_sequence assert is_seq((1, 2))