Skip to content

Commit f12e925

Browse files
mzeitlin11vladu
authored andcommitted
REGR: where not copying on no-op (pandas-dev#40592)
1 parent 5dc783d commit f12e925

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,8 @@ def where(self, other, cond, errors="raise") -> List[Block]:
12861286

12871287
if noop:
12881288
# TODO: avoid the downcasting at the end in this case?
1289-
result = values
1289+
# GH-39595: Always return a copy
1290+
result = values.copy()
12901291
else:
12911292
# see if we can operate on the entire block, or need item-by-item
12921293
# 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
@@ -692,3 +692,20 @@ def test_where_try_cast_deprecated(frame_or_series):
692692
with tm.assert_produces_warning(FutureWarning):
693693
# try_cast keyword deprecated
694694
obj.where(mask, -1, try_cast=False)
695+
696+
697+
def test_where_copies_with_noop(frame_or_series):
698+
# GH-39595
699+
result = frame_or_series([1, 2, 3, 4])
700+
expected = result.copy()
701+
col = result[0] if frame_or_series is DataFrame else result
702+
703+
where_res = result.where(col < 5)
704+
where_res *= 2
705+
706+
tm.assert_equal(result, expected)
707+
708+
where_res = result.where(col > 5, [1, 2, 3, 4])
709+
where_res *= 2
710+
711+
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)