Skip to content

BUG: df constructor not copying ea backed series #53744

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

Merged
merged 4 commits into from
Jun 28, 2023
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ Sparse

ExtensionArray
^^^^^^^^^^^^^^
- Bug in :class:`DataFrame` constructor not copying :class:`Series` with extension dtype when given in dict (:issue:`53744`)
- Bug in :class:`~arrays.ArrowExtensionArray` converting pandas non-nanosecond temporal objects from non-zero values to zero values (:issue:`53171`)
- Bug in :meth:`Series.quantile` for pyarrow temporal types raising ArrowInvalid (:issue:`52678`)
- Bug in :meth:`Series.rank` returning wrong order for small values with ``Float64`` dtype (:issue:`52471`)
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,11 @@ def dict_to_mgr(
x.copy()
if isinstance(x, ExtensionArray)
else x.copy(deep=True)
if isinstance(x, Index)
if (
isinstance(x, Index)
or isinstance(x, ABCSeries)
and is_1d_only_ea_dtype(x.dtype)
)
else x
for x in arrays
]
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2576,6 +2576,13 @@ def check_views(c_only: bool = False):
# TODO: we can check b[0] == 0 if we stop consolidating in
# setitem_with_indexer (except for datetimelike?)

def test_construct_from_dict_ea_series(self):
# GH#53744 - default of copy=True should also apply for Series with
# extension dtype
ser = Series([1, 2, 3], dtype="Int64")
df = DataFrame({"a": ser})
assert not np.shares_memory(ser.values._data, df["a"].values._data)

def test_from_series_with_name_with_columns(self):
# GH 7893
result = DataFrame(Series(1, name="foo"), columns=["bar"])
Expand Down