From 4964c39f1c06ed09ac10a8b61dfe623e28a73ce0 Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Sun, 27 Oct 2024 23:21:49 -0400 Subject: [PATCH 1/3] consistent name usage --- pandas/tests/dtypes/test_dtypes.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 33232e8df14e9..ceea6ab183634 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1243,3 +1243,15 @@ def test_loc_setitem_empty_labels_no_dtype_conversion(): assert df.a.dtype == "int64" tm.assert_frame_equal(df, expected) + + +def test_categorical_nan_no_dtype_conversion(): + # GH 43996 + + df = pd.DataFrame({"a": Categorical([np.nan], [1]), "b": [1]}) + expected = pd.DataFrame({"a": Categorical([1], [1]), "b": [1]}) + assert df["a"].dtype == "category" + + df.loc[0, "a"] = 1 + assert df["a"].dtype == "category" + tm.assert_frame_equal(df, expected) From 32e320681502fb6c84142a05a86906f83656e93d Mon Sep 17 00:00:00 2001 From: Michael Hu Date: Wed, 30 Oct 2024 16:02:56 -0400 Subject: [PATCH 2/3] changed to numpy array of integers --- pandas/tests/dtypes/test_dtypes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index ceea6ab183634..64df01f7f9df0 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1252,6 +1252,6 @@ def test_categorical_nan_no_dtype_conversion(): expected = pd.DataFrame({"a": Categorical([1], [1]), "b": [1]}) assert df["a"].dtype == "category" - df.loc[0, "a"] = 1 + df.loc[0, "a"] = np.array([1]) assert df["a"].dtype == "category" tm.assert_frame_equal(df, expected) From 0cf27a73baff9a7dd899c8d0f720733f16b3fff0 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 30 Oct 2024 17:34:20 -0400 Subject: [PATCH 3/3] Remove redundant assert Co-authored-by: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> --- pandas/tests/dtypes/test_dtypes.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 64df01f7f9df0..b7e37ff270e60 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -1250,8 +1250,5 @@ def test_categorical_nan_no_dtype_conversion(): df = pd.DataFrame({"a": Categorical([np.nan], [1]), "b": [1]}) expected = pd.DataFrame({"a": Categorical([1], [1]), "b": [1]}) - assert df["a"].dtype == "category" - df.loc[0, "a"] = np.array([1]) - assert df["a"].dtype == "category" tm.assert_frame_equal(df, expected)