From 7896908c7db49809c652f3771604dc359b32daf1 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 10 Nov 2019 02:41:24 +0000 Subject: [PATCH 1/4] Add test for single cat col check returns series --- pandas/tests/frame/test_dtypes.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index f7b197a518643..123338ff8ae6a 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -762,6 +762,12 @@ def test_astype_categoricaldtype_class_raises(self, cls): with pytest.raises(TypeError, match=xpr): df["A"].astype(cls) + def test_singlerow_slice_categoricaldtype_gives_series(self): + df = pd.DataFrame({'x': pd.Categorical('a b c d e'.split())}) + raw_cat = pd.Categorical(["a"], categories=["a", "b", "c", "d", "e"]) + expected = pd.Series(raw_cat, index=['x'], name=0, dtype='category') + tm.assert_series_equal(df.iloc[0], expected) + @pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"]) def test_astype_extension_dtypes(self, dtype): # GH 22578 From b20c33e9889c62900a5629cb04a67e1e7cd8b8f3 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 10 Nov 2019 02:52:18 +0000 Subject: [PATCH 2/4] Fix Linting --- pandas/tests/frame/test_dtypes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 123338ff8ae6a..5f01336576d80 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -763,9 +763,9 @@ def test_astype_categoricaldtype_class_raises(self, cls): df["A"].astype(cls) def test_singlerow_slice_categoricaldtype_gives_series(self): - df = pd.DataFrame({'x': pd.Categorical('a b c d e'.split())}) + df = pd.DataFrame({"x": pd.Categorical("a b c d e".split())}) raw_cat = pd.Categorical(["a"], categories=["a", "b", "c", "d", "e"]) - expected = pd.Series(raw_cat, index=['x'], name=0, dtype='category') + expected = pd.Series(raw_cat, index=["x"], name=0, dtype="category") tm.assert_series_equal(df.iloc[0], expected) @pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"]) From 7898b1b15859002a2300afb127628d65644fdb41 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 10 Nov 2019 13:32:17 +0000 Subject: [PATCH 3/4] Add issue number --- pandas/tests/frame/test_dtypes.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 5f01336576d80..98f10a11f7973 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -763,6 +763,7 @@ def test_astype_categoricaldtype_class_raises(self, cls): df["A"].astype(cls) def test_singlerow_slice_categoricaldtype_gives_series(self): + # GH29521 df = pd.DataFrame({"x": pd.Categorical("a b c d e".split())}) raw_cat = pd.Categorical(["a"], categories=["a", "b", "c", "d", "e"]) expected = pd.Series(raw_cat, index=["x"], name=0, dtype="category") From bcb5fd29473802baf796b8e0802e993f5c4d0653 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 10 Nov 2019 13:34:35 +0000 Subject: [PATCH 4/4] define result before tm.assert --- pandas/tests/frame/test_dtypes.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index 98f10a11f7973..89fd7ccd91f81 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -765,9 +765,11 @@ def test_astype_categoricaldtype_class_raises(self, cls): def test_singlerow_slice_categoricaldtype_gives_series(self): # GH29521 df = pd.DataFrame({"x": pd.Categorical("a b c d e".split())}) + result = df.iloc[0] raw_cat = pd.Categorical(["a"], categories=["a", "b", "c", "d", "e"]) expected = pd.Series(raw_cat, index=["x"], name=0, dtype="category") - tm.assert_series_equal(df.iloc[0], expected) + + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("dtype", ["Int64", "Int32", "Int16"]) def test_astype_extension_dtypes(self, dtype):