Skip to content

Commit 6b7225a

Browse files
BUG: df constructor not copying ea backed series (#53744)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent ee16656 commit 6b7225a

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ Sparse
516516

517517
ExtensionArray
518518
^^^^^^^^^^^^^^
519+
- Bug in :class:`DataFrame` constructor not copying :class:`Series` with extension dtype when given in dict (:issue:`53744`)
519520
- Bug in :class:`~arrays.ArrowExtensionArray` converting pandas non-nanosecond temporal objects from non-zero values to zero values (:issue:`53171`)
520521
- Bug in :meth:`Series.quantile` for pyarrow temporal types raising ArrowInvalid (:issue:`52678`)
521522
- Bug in :meth:`Series.rank` returning wrong order for small values with ``Float64`` dtype (:issue:`52471`)

pandas/core/internals/construction.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,11 @@ def dict_to_mgr(
469469
x.copy()
470470
if isinstance(x, ExtensionArray)
471471
else x.copy(deep=True)
472-
if isinstance(x, Index)
472+
if (
473+
isinstance(x, Index)
474+
or isinstance(x, ABCSeries)
475+
and is_1d_only_ea_dtype(x.dtype)
476+
)
473477
else x
474478
for x in arrays
475479
]

pandas/tests/frame/test_constructors.py

+7
Original file line numberDiff line numberDiff line change
@@ -2576,6 +2576,13 @@ def check_views(c_only: bool = False):
25762576
# TODO: we can check b[0] == 0 if we stop consolidating in
25772577
# setitem_with_indexer (except for datetimelike?)
25782578

2579+
def test_construct_from_dict_ea_series(self):
2580+
# GH#53744 - default of copy=True should also apply for Series with
2581+
# extension dtype
2582+
ser = Series([1, 2, 3], dtype="Int64")
2583+
df = DataFrame({"a": ser})
2584+
assert not np.shares_memory(ser.values._data, df["a"].values._data)
2585+
25792586
def test_from_series_with_name_with_columns(self):
25802587
# GH 7893
25812588
result = DataFrame(Series(1, name="foo"), columns=["bar"])

0 commit comments

Comments
 (0)