diff --git a/pandas/core/arrays/categorical.py b/pandas/core/arrays/categorical.py index 4d065bd234e0b..20e666bbc7bdb 100644 --- a/pandas/core/arrays/categorical.py +++ b/pandas/core/arrays/categorical.py @@ -673,7 +673,7 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None): raise ValueError(msg) codes = np.asarray(codes) # #21767 - if not is_integer_dtype(codes): + if len(codes) and not is_integer_dtype(codes): msg = "codes need to be array-like integers" if is_float_dtype(codes): icodes = codes.astype("i8") diff --git a/pandas/tests/arrays/categorical/test_constructors.py b/pandas/tests/arrays/categorical/test_constructors.py index 237ec17f56974..6eb26d26e14bd 100644 --- a/pandas/tests/arrays/categorical/test_constructors.py +++ b/pandas/tests/arrays/categorical/test_constructors.py @@ -526,6 +526,9 @@ def test_from_codes_with_float(self): codes = [1.0, 2.0, 0] # integer, but in float dtype dtype = CategoricalDtype(categories=["a", "b", "c"]) + # empty codes should not raise for floats + Categorical.from_codes([], dtype.categories) + with tm.assert_produces_warning(FutureWarning): cat = Categorical.from_codes(codes, dtype.categories) tm.assert_numpy_array_equal(cat.codes, np.array([1, 2, 0], dtype="i1"))