From c46b2019f28c87010bb9e40b1ea782c3d8828951 Mon Sep 17 00:00:00 2001 From: Bhavay192 Date: Thu, 26 Aug 2021 20:46:53 +0530 Subject: [PATCH 1/6] TST Providing unit test to snippet GH32414 --- pandas/tests/series/methods/test_fillna.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 03e126587ce1a..5d06866dbe095 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -671,6 +671,27 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu result = ser.fillna(fill_value) tm.assert_series_equal(result, exp) + @pytest.mark.parametrize( + "fill_value, expected_output", + [ + ("B", ["A", "B", "B", "B", "C"]), + ("C", ["A", "B", "C", "C", "C"]) + ], + ) + + def test_series_fill(self, fill_value, expected_output): + data = ["A", "B", np.nan, np.nan, "C"] + ser = Series(Categorical(data, categories=["A", "B"])) + + msg = "Element not present in categories. Cannot be filled in series." + with pytest.raises(TypeError, match=msg): + ser.fillna("D") + + exp = Series(Categorical(expected_output, categories=["A", "B"])) + result = ser.fillna(fill_value) + tm.assert_series_equal(result, exp) + + def test_fillna_categorical_raises(self): data = ["a", np.nan, "b", np.nan, np.nan] ser = Series(Categorical(data, categories=["a", "b"])) From 02db9f0cc3b6f5c42c3d40e221fe2fcfdf0c07fd Mon Sep 17 00:00:00 2001 From: Bhavay192 Date: Thu, 26 Aug 2021 20:51:12 +0530 Subject: [PATCH 2/6] TST Providing unit test to snippet GH#32414 --- pandas/tests/series/methods/test_fillna.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 5d06866dbe095..99b226c01d21d 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -680,6 +680,7 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu ) def test_series_fill(self, fill_value, expected_output): + # GH#32414 data = ["A", "B", np.nan, np.nan, "C"] ser = Series(Categorical(data, categories=["A", "B"])) From b872815e29ceea0f94aa0c9e7ae3dbf72a2f0612 Mon Sep 17 00:00:00 2001 From: Bhavay192 Date: Mon, 30 Aug 2021 14:33:36 +0530 Subject: [PATCH 3/6] Updated the test_fillna file based on GH43230 --- pandas/tests/series/methods/test_fillna.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 99b226c01d21d..c38899b592285 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -679,18 +679,18 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu ], ) - def test_series_fill(self, fill_value, expected_output): + def test_series_fill(fill_value, expected_output): # GH#32414 data = ["A", "B", np.nan, np.nan, "C"] - ser = Series(Categorical(data, categories=["A", "B"])) + ser = Series(Categorical(data, categories=["A", "B", "C"])) - msg = "Element not present in categories. Cannot be filled in series." - with pytest.raises(TypeError, match=msg): - ser.fillna("D") + # msg = "Element not present in categories. Cannot be filled in series." + # with pytest.raises(TypeError, match=msg): + # ser.fillna("D") - exp = Series(Categorical(expected_output, categories=["A", "B"])) + exp = Series(Categorical(expected_output, categories=["A", "B", "C"])) result = ser.fillna(fill_value) - tm.assert_series_equal(result, exp) + tm.assert_almost_equal(result, exp) def test_fillna_categorical_raises(self): From 49b693c51b8cdf2c49ff13303b777f28dfea6c37 Mon Sep 17 00:00:00 2001 From: Bhavay192 Date: Mon, 30 Aug 2021 20:02:55 +0530 Subject: [PATCH 4/6] Updated with required changes --- pandas/tests/series/methods/test_fillna.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index c38899b592285..02dff164e539b 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -679,18 +679,13 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu ], ) - def test_series_fill(fill_value, expected_output): + def test_series_fill(self, fill_value, expected_output): # GH#32414 data = ["A", "B", np.nan, np.nan, "C"] - ser = Series(Categorical(data, categories=["A", "B", "C"])) - - # msg = "Element not present in categories. Cannot be filled in series." - # with pytest.raises(TypeError, match=msg): - # ser.fillna("D") - - exp = Series(Categorical(expected_output, categories=["A", "B", "C"])) - result = ser.fillna(fill_value) - tm.assert_almost_equal(result, exp) + series = Series(Categorical(data, categories=["A", "B", "C"])) + expected = Series(Categorical(expected_output, categories=["A", "B", "C"])) + result = series.fillna(fill_value) + tm.assert_almost_equal(result, expected) def test_fillna_categorical_raises(self): From edf6af75a7dce64bf36ddc533d7e7483b830f9f8 Mon Sep 17 00:00:00 2001 From: Bhavay192 Date: Fri, 3 Sep 2021 19:01:44 +0530 Subject: [PATCH 5/6] Updated the changes --- pandas/tests/series/methods/test_fillna.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 02dff164e539b..cd5bab7812f8b 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -679,13 +679,17 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu ], ) - def test_series_fill(self, fill_value, expected_output): - # GH#32414 + def test_series_fill(fill_value, expected_output): + # GH32414 data = ["A", "B", np.nan, np.nan, "C"] - series = Series(Categorical(data, categories=["A", "B", "C"])) - expected = Series(Categorical(expected_output, categories=["A", "B", "C"])) - result = series.fillna(fill_value) - tm.assert_almost_equal(result, expected) + cat = Categorical(data, categories=["A", "B", "C"]) + ser = Series(cat) + exp = Categorical(expected_output, categories=["A", "B", "C"]) + exp_ser = Series(exp) + result = ser.fillna(fill_value) + filled = cat.fillna(fill_value) + tm.assert_almost_equal(result, exp_ser) + tm.assert_almost_equal(filled, exp) def test_fillna_categorical_raises(self): From 12ab6aa4bc12178ba9e7408000f3894e01facdd4 Mon Sep 17 00:00:00 2001 From: Bhavay192 Date: Sat, 20 Nov 2021 15:59:37 +0530 Subject: [PATCH 6/6] test_fillna.py updated --- pandas/tests/series/methods/test_fillna.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index cd5bab7812f8b..4aaaea12b393d 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -7,6 +7,7 @@ import numpy as np import pytest import pytz +from typing_extensions import Literal from pandas import ( Categorical, @@ -679,17 +680,17 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu ], ) - def test_series_fill(fill_value, expected_output): + def test_fillna_categorical(self, fill_value, expected_output): # GH32414 data = ["A", "B", np.nan, np.nan, "C"] cat = Categorical(data, categories=["A", "B", "C"]) ser = Series(cat) - exp = Categorical(expected_output, categories=["A", "B", "C"]) - exp_ser = Series(exp) - result = ser.fillna(fill_value) + exp_cat = Categorical(expected_output, categories=["A", "B", "C"]) + exp_ser = Series(exp_cat) + result_ser = ser.fillna(fill_value) filled = cat.fillna(fill_value) - tm.assert_almost_equal(result, exp_ser) - tm.assert_almost_equal(filled, exp) + tm.assert_almost_equal(result_ser, exp_ser) + tm.assert_almost_equal(filled, exp_cat) def test_fillna_categorical_raises(self):