Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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: 2 additions & 0 deletions doc/source/whatsnew/v0.24.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Fixed Regressions

- 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 classes like ``dict`` were passed as argument. (:issue:`25196`)

.. _whatsnew_0242.enhancements:

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
5 changes: 5 additions & 0 deletions pandas/tests/frame/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ 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):
data = pd.DataFrame([[1, 2], [3, 4]])
expected = pd.Series([{0: 1, 1: 3}, {0: 2, 1: 4}])
assert_series_equal(data.apply(dict), expected)

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

Expand Down