From a23c7d6f4dd11de0126f7498213465aeb6fae360 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 10 Nov 2019 04:42:19 +0000 Subject: [PATCH 1/4] Check error raised with wrong length cat insertion --- pandas/tests/frame/test_dtypes.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index cdcd5996324da..dd96ceb625e01 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -724,6 +724,13 @@ def test_astype_duplicate_col(self): expected = concat([a1_str, b, a2_str], axis=1) tm.assert_frame_equal(result, expected) + def test_wrong_length_cat_dtype_raises(self): + cat = pd.Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"]) + df = pd.DataFrame({"bar": range(10)}) + err = "Length of values does not match length of index" + with pytest.raises(ValueError, match=err): + df["foo"] = cat + @pytest.mark.parametrize( "dtype", [ From b80a665c2a1c8891fe705b1818fba4bc0a7e9164 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 10 Nov 2019 13:19:40 +0000 Subject: [PATCH 2/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 dd96ceb625e01..a5c9c9ae663f3 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -725,6 +725,7 @@ def test_astype_duplicate_col(self): tm.assert_frame_equal(result, expected) def test_wrong_length_cat_dtype_raises(self): + # GH29523 cat = pd.Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"]) df = pd.DataFrame({"bar": range(10)}) err = "Length of values does not match length of index" From 27de8fbc5a56b3b43fc9b2dd90656685f6b8aa5d Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Wed, 13 Nov 2019 01:57:42 +0000 Subject: [PATCH 3/4] Shift test to pandas/tests/frame/test_indexing.py --- pandas/tests/frame/test_indexing.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pandas/tests/frame/test_indexing.py b/pandas/tests/frame/test_indexing.py index 61bf91d3018eb..e37f734c6235e 100644 --- a/pandas/tests/frame/test_indexing.py +++ b/pandas/tests/frame/test_indexing.py @@ -3881,3 +3881,11 @@ def test_loc_indexing_preserves_index_category_dtype(self): result = df.loc[["a"]].index.levels[0] tm.assert_index_equal(result, expected) + + def test_wrong_length_cat_dtype_raises(self): + # GH29523 + cat = pd.Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"]) + df = pd.DataFrame({"bar": range(10)}) + err = "Length of values does not match length of index" + with pytest.raises(ValueError, match=err): + df["foo"] = cat From 27e7b9cccbdc18d9761c5f26e2750cabd98ddebc Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Wed, 13 Nov 2019 02:02:10 +0000 Subject: [PATCH 4/4] remove existing duplicate test --- pandas/tests/frame/test_dtypes.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pandas/tests/frame/test_dtypes.py b/pandas/tests/frame/test_dtypes.py index a5c9c9ae663f3..cdcd5996324da 100644 --- a/pandas/tests/frame/test_dtypes.py +++ b/pandas/tests/frame/test_dtypes.py @@ -724,14 +724,6 @@ def test_astype_duplicate_col(self): expected = concat([a1_str, b, a2_str], axis=1) tm.assert_frame_equal(result, expected) - def test_wrong_length_cat_dtype_raises(self): - # GH29523 - cat = pd.Categorical.from_codes([0, 1, 1, 0, 1, 2], ["a", "b", "c"]) - df = pd.DataFrame({"bar": range(10)}) - err = "Length of values does not match length of index" - with pytest.raises(ValueError, match=err): - df["foo"] = cat - @pytest.mark.parametrize( "dtype", [