From 143139c853b46bc77592c43b6cec4d4fe4a31419 Mon Sep 17 00:00:00 2001 From: zachyattack23 Date: Wed, 3 Dec 2025 22:31:45 -0500 Subject: [PATCH 1/3] TST: Add regression test for DataFrame.where inplace consistency - Add test for GH#46512 - Bug was fixed - inplace and non-inplace now behave consistently - Test ensures StringArray with NA-like values works correctly - Both inplace=True and inplace=False now return pd.NA consistently Closes #46512 --- pandas/tests/frame/indexing/test_where.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index f7fd26dbb4303..66ab3a08de56e 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -1073,3 +1073,23 @@ def test_where_other_nullable_dtype(): result = df.where(df > 1, other, axis=0) expected = DataFrame({0: Series([pd.NA, 2, 3], dtype="Int64")}) tm.assert_frame_equal(result, expected) + + +def test_where_inplace_string_array_consistency(): + # GH#46512 - inplace and non-inplace should have consistent behavior + # for StringArray with NA-like values + df = DataFrame({"A": ["1", "", "3"]}, dtype="string") + df_inplace = df.copy() + + # Test non-inplace + result = df.where(df != "", np.nan) + + # Test inplace + df_inplace.where(df_inplace != "", np.nan, inplace=True) + + # Both should produce pd.NA, not float nan + assert isinstance(result["A"]._values[1], type(pd.NA)) + assert isinstance(df_inplace["A"]._values[1], type(pd.NA)) + + # Results should be identical + tm.assert_frame_equal(result, df_inplace) From cd2b41db931fe18e1c2131f18c04540360ddf4cf Mon Sep 17 00:00:00 2001 From: zachyattack23 <74475738+zachyattack23@users.noreply.github.com> Date: Fri, 5 Dec 2025 11:57:17 -0500 Subject: [PATCH 2/3] Update pandas/tests/frame/indexing/test_where.py Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/tests/frame/indexing/test_where.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index 66ab3a08de56e..1909c9d8b2b71 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -1087,9 +1087,6 @@ def test_where_inplace_string_array_consistency(): # Test inplace df_inplace.where(df_inplace != "", np.nan, inplace=True) - # Both should produce pd.NA, not float nan - assert isinstance(result["A"]._values[1], type(pd.NA)) - assert isinstance(df_inplace["A"]._values[1], type(pd.NA)) # Results should be identical tm.assert_frame_equal(result, df_inplace) From 6d72476c65bd2c34f5c9680db310237ad1d85a63 Mon Sep 17 00:00:00 2001 From: zachyattack23 Date: Fri, 5 Dec 2025 12:05:19 -0500 Subject: [PATCH 3/3] Complete review request: remove all verbose comments except GH reference --- pandas/tests/frame/indexing/test_where.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pandas/tests/frame/indexing/test_where.py b/pandas/tests/frame/indexing/test_where.py index 1909c9d8b2b71..2026270ec752c 100644 --- a/pandas/tests/frame/indexing/test_where.py +++ b/pandas/tests/frame/indexing/test_where.py @@ -1076,17 +1076,11 @@ def test_where_other_nullable_dtype(): def test_where_inplace_string_array_consistency(): - # GH#46512 - inplace and non-inplace should have consistent behavior - # for StringArray with NA-like values + # GH#46512 df = DataFrame({"A": ["1", "", "3"]}, dtype="string") df_inplace = df.copy() - # Test non-inplace result = df.where(df != "", np.nan) - - # Test inplace df_inplace.where(df_inplace != "", np.nan, inplace=True) - - # Results should be identical tm.assert_frame_equal(result, df_inplace)