From 129e5c47f72e3d9dbcfa11a8cb011d69b9e7b093 Mon Sep 17 00:00:00 2001 From: cmmck Date: Tue, 18 Apr 2023 20:51:48 -0400 Subject: [PATCH 1/2] Add test for GH 27781 --- pandas/tests/arrays/sparse/test_array.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 54c8e359b2859..3d204cda668c4 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -478,3 +478,12 @@ def test_drop_duplicates_fill_value(): result = df.drop_duplicates() expected = pd.DataFrame({i: SparseArray([0.0], fill_value=0) for i in range(5)}) tm.assert_frame_equal(result, expected) + + +def test_zero_sparse_column(): + # GH 27781 + df1 = pd.DataFrame({"A": SparseArray([0, 0, 0]), "B": [1, 2, 3]}) + df2 = pd.DataFrame({"A": SparseArray([0, 1, 0]), "B": [1, 2, 3]}) + result = df1.loc[df1["B"] != 2] + expected = df2.loc[df2["B"] != 2] + tm.assert_frame_equal(result, expected) From 0573a7917f7aed5c8bc7ebf721479a49d20eee05 Mon Sep 17 00:00:00 2001 From: cmmck Date: Wed, 19 Apr 2023 13:13:40 -0400 Subject: [PATCH 2/2] Modify test for GH 27781 --- pandas/tests/arrays/sparse/test_array.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pandas/tests/arrays/sparse/test_array.py b/pandas/tests/arrays/sparse/test_array.py index 3d204cda668c4..4a0795137f80b 100644 --- a/pandas/tests/arrays/sparse/test_array.py +++ b/pandas/tests/arrays/sparse/test_array.py @@ -487,3 +487,6 @@ def test_zero_sparse_column(): result = df1.loc[df1["B"] != 2] expected = df2.loc[df2["B"] != 2] tm.assert_frame_equal(result, expected) + + expected = pd.DataFrame({"A": SparseArray([0, 0]), "B": [1, 3]}, index=[0, 2]) + tm.assert_frame_equal(result, expected)