Skip to content

Commit ed966dd

Browse files
REGR: where not copying on no-op (pandas-dev#40592) (pandas-dev#40634)
Co-authored-by: Matthew Zeitlin <[email protected]>
1 parent 97f9a1e commit ed966dd

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

doc/source/whatsnew/v1.2.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Fixed regressions
1717

1818
- Fixed regression in :meth:`DataFrame.sum` when ``min_count`` greater than the :class:`DataFrame` shape was passed resulted in a ``ValueError`` (:issue:`39738`)
1919
- Fixed regression in :meth:`DataFrame.to_json` raising ``AttributeError`` when run on PyPy (:issue:`39837`)
20+
- Fixed regression in :meth:`DataFrame.where` not returning a copy in the case of an all True condition (:issue:`39595`)
2021
-
2122

2223
.. ---------------------------------------------------------------------------

pandas/core/internals/blocks.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1458,7 +1458,7 @@ def where(
14581458
raise ValueError("where must have a condition that is ndarray like")
14591459

14601460
if cond.ravel("K").all():
1461-
result = values
1461+
result = values.copy()
14621462
else:
14631463
# see if we can operate on the entire block, or need item-by-item
14641464
# or if we are a single block (ndim == 1)

pandas/tests/frame/indexing/test_where.py

+17
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,20 @@ def test_where_categorical_filtering(self):
653653
expected.loc[0, :] = np.nan
654654

655655
tm.assert_equal(result, expected)
656+
657+
658+
def test_where_copies_with_noop(frame_or_series):
659+
# GH-39595
660+
result = frame_or_series([1, 2, 3, 4])
661+
expected = result.copy()
662+
col = result[0] if frame_or_series is DataFrame else result
663+
664+
where_res = result.where(col < 5)
665+
where_res *= 2
666+
667+
tm.assert_equal(result, expected)
668+
669+
where_res = result.where(col > 5, [1, 2, 3, 4])
670+
where_res *= 2
671+
672+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)