Skip to content
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

BUG: Fix regression in DataFrame.apply causing RecursionError #25230

Merged
merged 4 commits into from
Feb 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Fixed Regressions
^^^^^^^^^^^^^^^^^

- Fixed regression in :meth:`DataFrame.all` and :meth:`DataFrame.any` where ``bool_only=True`` was ignored (:issue:`25101`)

- Fixed issue in ``DataFrame`` construction with passing a mixed list of mixed types could segfault. (:issue:`25075`)
- Fixed regression in :meth:`DataFrame.apply` causing ``RecursionError`` when ``dict``-like classes were passed as argument. (:issue:`25196`)

.. _whatsnew_0242.enhancements:

Expand Down
13 changes: 8 additions & 5 deletions pandas/core/dtypes/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,15 @@ def is_dict_like(obj):
True
>>> is_dict_like([1, 2, 3])
False
>>> is_dict_like(dict)
False
>>> is_dict_like(dict())
True
"""
for attr in ("__getitem__", "keys", "__contains__"):
if not hasattr(obj, attr):
return False

return True
dict_like_attrs = ("__getitem__", "keys", "__contains__")
return (all(hasattr(obj, attr) for attr in dict_like_attrs)
# [GH 25196] exclude classes
and not isinstance(obj, type))


def is_named_tuple(obj):
Expand Down
8 changes: 5 additions & 3 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,13 +159,15 @@ def test_is_nested_list_like_fails(obj):


@pytest.mark.parametrize(
"ll", [{}, {'A': 1}, Series([1])])
"ll", [{}, {'A': 1}, Series([1]), collections.defaultdict()])
def test_is_dict_like_passes(ll):
assert inference.is_dict_like(ll)


@pytest.mark.parametrize(
"ll", ['1', 1, [1, 2], (1, 2), range(2), Index([1])])
@pytest.mark.parametrize("ll", [
'1', 1, [1, 2], (1, 2), range(2), Index([1]),
dict, collections.defaultdict, Series
])
def test_is_dict_like_fails(ll):
assert not inference.is_dict_like(ll)

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ def test_apply_reduce_Series(self, float_frame):
result = float_frame.apply(np.mean, axis=1)
assert_series_equal(result, expected)

def test_apply_reduce_rows_to_dict(self):
# GH 25196
data = pd.DataFrame([[1, 2], [3, 4]])
jschendel marked this conversation as resolved.
Show resolved Hide resolved
expected = pd.Series([{0: 1, 1: 3}, {0: 2, 1: 4}])
result = data.apply(dict)
assert_series_equal(result, expected)

def test_apply_differently_indexed(self):
df = DataFrame(np.random.randn(20, 10))

Expand Down