Skip to content

Commit ebe4dc7

Browse files
authored
REGR: DataFrame(dict) missing columns should not be views (#45432)
1 parent 91f12c4 commit ebe4dc7

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pandas/core/internals/construction.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,13 @@ def dict_to_mgr(
464464
# GH#1783
465465
nan_dtype = np.dtype("object")
466466
val = construct_1d_arraylike_from_scalar(np.nan, len(index), nan_dtype)
467-
arrays.loc[missing] = [val] * missing.sum()
467+
nmissing = missing.sum()
468+
if copy:
469+
rhs = [val] * nmissing
470+
else:
471+
# GH#45369
472+
rhs = [val.copy() for _ in range(nmissing)]
473+
arrays.loc[missing] = rhs
468474

469475
arrays = list(arrays)
470476
columns = ensure_index(columns)

pandas/tests/frame/test_constructors.py

+17
Original file line numberDiff line numberDiff line change
@@ -2642,6 +2642,23 @@ def test_error_from_2darray(self, col_a, col_b):
26422642
with pytest.raises(ValueError, match=msg):
26432643
DataFrame({"a": col_a, "b": col_b})
26442644

2645+
def test_from_dict_with_missing_copy_false(self):
2646+
# GH#45369 filled columns should not be views of one another
2647+
df = DataFrame(index=[1, 2, 3], columns=["a", "b", "c"], copy=False)
2648+
assert not np.shares_memory(df["a"]._values, df["b"]._values)
2649+
2650+
df.iloc[0, 0] = 0
2651+
expected = DataFrame(
2652+
{
2653+
"a": [0, np.nan, np.nan],
2654+
"b": [np.nan, np.nan, np.nan],
2655+
"c": [np.nan, np.nan, np.nan],
2656+
},
2657+
index=[1, 2, 3],
2658+
dtype=object,
2659+
)
2660+
tm.assert_frame_equal(df, expected)
2661+
26452662

26462663
class TestDataFrameConstructorIndexInference:
26472664
def test_frame_from_dict_of_series_overlapping_monthly_period_indexes(self):

0 commit comments

Comments
 (0)