CoW: Add lazy copy mechanism to DataFrame constructor for dict of Index by phofl · Pull Request #52947 · pandas-dev/pandas (original) (raw)
Yeah, this is currently a "bug" for when passing a dict of Series with EA dtype. Because we are passing a dict, it should result in a copy, but it doesn't when it's an EA dtype and does when default numpy dtype:
In [17]: ser = pd.Series([1, 2, 3], dtype="Int64")
In [18]: df = pd.DataFrame({"col": ser})
In [19]: df.iloc[0, 0] = 10
In [20]: ser
Out[20]:
0 10
1 2
2 3
dtype: Int64
In [21]: ser = pd.Series([1, 2, 3], dtype="int64")
In [22]: df = pd.DataFrame({"col": ser})
In [23]: df.iloc[0, 0] = 10
In [24]: ser
Out[24]:
0 1
1 2
2 3
dtype: int64
And also when creating it from an array instead of Series, it is copied correctly:
In [25]: arr = pd.array([1, 2, 3], dtype="Int64")
In [26]: df = pd.DataFrame({"col": arr})
In [27]: df.iloc[0, 0] = 10
In [28]: arr
Out[28]:
<IntegerArray>
[1, 2, 3]
Length: 3, dtype: Int64