From e0a29ca0db8c4527266d48ea4aba99afe14464b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoshiki=20V=C3=A1zquez=20Baeza?= Date: Fri, 1 Oct 2021 10:27:39 -0700 Subject: [PATCH 1/4] TST: Test Series' settitem with Interval and NaN Fixes #27937. As described in that issue, assigning a mixture of NaN and Interval values into a slice of a categorical-typed series would raise an exception. This is no longer the case. --- pandas/tests/indexing/test_iloc.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index b04a2c86a79d7..354933d7e8fe6 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -931,6 +931,16 @@ def test_iloc_setitem_td64_values_cast_na(self, value): expected = Series([NaT, 1, 2], dtype="timedelta64[ns]") tm.assert_series_equal(series, expected) + @pytest.mark.parametrize("not_na", [Interval(0, 1), 'a', 1.0]) + @pytest.mark.parametrize("na", [np.nan, NaT]) + def test_setitem_mix_of_nan_and_interval(self, not_na, na): + # GH#27937 + dtype = CategoricalDtype(categories=[not_na]) + ser = Series([na, na, na, na], dtype=dtype) + ser.iloc[:3] = [na, not_na, na] + exp = Series([na, not_na, na, na], dtype=dtype) + tm.assert_series_equal(ser, exp) + def test_iloc_setitem_empty_frame_raises_with_3d_ndarray(self): idx = Index([]) obj = DataFrame(np.random.randn(len(idx), len(idx)), index=idx, columns=idx) From ec4ea260766fadefc9f33ccd2364d19a67b16338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoshiki=20V=C3=A1zquez=20Baeza?= Date: Sun, 3 Oct 2021 23:39:29 -0700 Subject: [PATCH 2/4] Update pandas/tests/indexing/test_iloc.py --- pandas/tests/indexing/test_iloc.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 354933d7e8fe6..259e9f7b7a61c 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -931,7 +931,7 @@ def test_iloc_setitem_td64_values_cast_na(self, value): expected = Series([NaT, 1, 2], dtype="timedelta64[ns]") tm.assert_series_equal(series, expected) - @pytest.mark.parametrize("not_na", [Interval(0, 1), 'a', 1.0]) + @pytest.mark.parametrize("not_na", [Interval(0, 1), "a", 1.0]) @pytest.mark.parametrize("na", [np.nan, NaT]) def test_setitem_mix_of_nan_and_interval(self, not_na, na): # GH#27937 From 3ccecf0b4b0a7c6f81fca7885a528448c9f7ec82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoshiki=20V=C3=A1zquez=20Baeza?= Date: Mon, 4 Oct 2021 08:48:10 -0700 Subject: [PATCH 3/4] Use nulls_fixture instead of homebrewed nulls --- pandas/tests/indexing/test_iloc.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 259e9f7b7a61c..2135a8e9aa3e8 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -932,13 +932,14 @@ def test_iloc_setitem_td64_values_cast_na(self, value): tm.assert_series_equal(series, expected) @pytest.mark.parametrize("not_na", [Interval(0, 1), "a", 1.0]) - @pytest.mark.parametrize("na", [np.nan, NaT]) - def test_setitem_mix_of_nan_and_interval(self, not_na, na): + def test_setitem_mix_of_nan_and_interval(self, not_na, nulls_fixture): # GH#27937 dtype = CategoricalDtype(categories=[not_na]) - ser = Series([na, na, na, na], dtype=dtype) - ser.iloc[:3] = [na, not_na, na] - exp = Series([na, not_na, na, na], dtype=dtype) + ser = Series([nulls_fixture, nulls_fixture, nulls_fixture, + nulls_fixture], dtype=dtype) + ser.iloc[:3] = [nulls_fixture, not_na, nulls_fixture] + exp = Series([nulls_fixture, not_na, nulls_fixture, + nulls_fixture], dtype=dtype) tm.assert_series_equal(ser, exp) def test_iloc_setitem_empty_frame_raises_with_3d_ndarray(self): From 9e1d9f6f075f4f98fdd911047d8828f7506ab1b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoshiki=20V=C3=A1zquez=20Baeza?= Date: Mon, 4 Oct 2021 10:30:11 -0700 Subject: [PATCH 4/4] STY: Fix pre-commit changes --- pandas/tests/indexing/test_iloc.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 2135a8e9aa3e8..b8c53c7b59239 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -935,11 +935,11 @@ def test_iloc_setitem_td64_values_cast_na(self, value): def test_setitem_mix_of_nan_and_interval(self, not_na, nulls_fixture): # GH#27937 dtype = CategoricalDtype(categories=[not_na]) - ser = Series([nulls_fixture, nulls_fixture, nulls_fixture, - nulls_fixture], dtype=dtype) + ser = Series( + [nulls_fixture, nulls_fixture, nulls_fixture, nulls_fixture], dtype=dtype + ) ser.iloc[:3] = [nulls_fixture, not_na, nulls_fixture] - exp = Series([nulls_fixture, not_na, nulls_fixture, - nulls_fixture], dtype=dtype) + exp = Series([nulls_fixture, not_na, nulls_fixture, nulls_fixture], dtype=dtype) tm.assert_series_equal(ser, exp) def test_iloc_setitem_empty_frame_raises_with_3d_ndarray(self):