From 7a13fc417d79d0a07e043fee4afce4d41f8db305 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sat, 16 Nov 2019 22:00:32 +0000 Subject: [PATCH] TST: Check error raised when inserting wrong length categorical column (#29523) --- 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