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

Better support for UserDict on constructor #55109

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enhancement2
Other enhancements
^^^^^^^^^^^^^^^^^^
- DataFrame.apply now allows the usage of numba (via ``engine="numba"``) to JIT compile the passed function, allowing for potential speedups (:issue:`54666`)
-
- It is now possible to pass an instance of a `collections.UserDict` subclass to initialize a DataFrame. The expected behaviour is the same as if you pass a `dict` (:issue:`55109`)

.. ---------------------------------------------------------------------------
.. _whatsnew_220.notable_bug_fixes:
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ def __init__(
raise ValueError("columns cannot be a set")

if copy is None:
if isinstance(data, dict):
if isinstance(data, abc.MutableMapping):
gabrielcnr marked this conversation as resolved.
Show resolved Hide resolved
# retain pre-GH#38939 default behavior
copy = True
elif (
Expand Down Expand Up @@ -732,7 +732,7 @@ def __init__(
data, axes={"index": index, "columns": columns}, dtype=dtype, copy=copy
)

elif isinstance(data, dict):
elif isinstance(data, abc.MutableMapping):
gabrielcnr marked this conversation as resolved.
Show resolved Hide resolved
# GH#38939 de facto copy defaults to False only in non-dict cases
mgr = dict_to_mgr(data, index, columns, dtype=dtype, copy=copy, typ=manager)
elif isinstance(data, ma.MaskedArray):
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ def _check_values_indices_shape_match(


def dict_to_mgr(
data: dict,
data: abc.MutableMapping,
index,
columns,
*,
Expand Down
17 changes: 16 additions & 1 deletion pandas/tests/frame/constructors/test_from_dict.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from collections import OrderedDict
from collections import (
OrderedDict,
UserDict,
)

import numpy as np
import pytest
Expand Down Expand Up @@ -200,3 +203,15 @@ def test_from_dict_orient_invalid(self):
)
with pytest.raises(ValueError, match=msg):
DataFrame.from_dict({"foo": 1, "baz": 3, "bar": 2}, orient="abc")

def test_constructor_user_dict(self):
class CustomUserDict(UserDict):
pass

d_1 = CustomUserDict(foo=1, bar=2)
df_1 = DataFrame(d_1, index=["a"])

d_2 = {"foo": 1, "bar": 2}
df_2 = DataFrame(d_2, index=["a"])

tm.assert_frame_equal(df_1, df_2)