Skip to content

Commit c40c48c

Browse files
Backport PR #48215 on branch 1.4.x (REGR: properly update DataFrame cache in Series.__setitem__) (#48266)
Co-authored-by: Joris Van den Bossche <[email protected]>
1 parent 6fa8a4a commit c40c48c

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v1.4.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Fixed regressions
2626
- Fixed regression in :meth:`DataFrame.loc` setting a length-1 array like value to a single value in the DataFrame (:issue:`46268`)
2727
- Fixed regression when slicing with :meth:`DataFrame.loc` with :class:`DateOffset`-index (:issue:`46671`)
2828
- Fixed regression in setting ``None`` or non-string value into a ``string``-dtype Series using a mask (:issue:`47628`)
29+
- Fixed regression in updating a DataFrame column through Series ``__setitem__`` (using chained assignment) not updating column values inplace and using too much memory (:issue:`47172`)
2930
- Fixed regression in :meth:`DataFrame.select_dtypes` returning a view on the original DataFrame (:issue:`48090`)
3031
- Fixed regression using custom Index subclasses (for example, used in xarray) with :meth:`~DataFrame.reset_index` or :meth:`Index.insert` (:issue:`47071`)
3132
- Fixed regression in :meth:`DatetimeIndex.intersection` when the :class:`DatetimeIndex` has dates crossing daylight savings time (:issue:`46702`)

pandas/core/series.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1140,7 +1140,7 @@ def __setitem__(self, key, value) -> None:
11401140
self._set_with(key, value)
11411141

11421142
if cacher_needs_updating:
1143-
self._maybe_update_cacher()
1143+
self._maybe_update_cacher(inplace=True)
11441144

11451145
def _set_with_engine(self, key, value) -> None:
11461146
loc = self.index.get_loc(key)

pandas/tests/frame/indexing/test_setitem.py

+14
Original file line numberDiff line numberDiff line change
@@ -1174,3 +1174,17 @@ def test_setitem_not_operating_inplace(self, value, set_value, indexer):
11741174
view = df[:]
11751175
df[indexer] = set_value
11761176
tm.assert_frame_equal(view, expected)
1177+
1178+
@td.skip_array_manager_invalid_test
1179+
def test_setitem_column_update_inplace(self):
1180+
# https://github.com/pandas-dev/pandas/issues/47172
1181+
1182+
labels = [f"c{i}" for i in range(10)]
1183+
df = DataFrame({col: np.zeros(len(labels)) for col in labels}, index=labels)
1184+
values = df._mgr.blocks[0].values
1185+
1186+
for label in df.columns:
1187+
df[label][label] = 1
1188+
1189+
# diagonal values all updated
1190+
assert np.all(values[np.arange(10), np.arange(10)] == 1)

0 commit comments

Comments
 (0)