Skip to content

Commit e450d77

Browse files
topper-123proost
authored andcommitted
BUG: Categorical.from_codes should not warn on empty codes (pandas-dev#29336)
1 parent fdbc0bf commit e450d77

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

pandas/core/arrays/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ def from_codes(cls, codes, categories=None, ordered=None, dtype=None):
673673
raise ValueError(msg)
674674

675675
codes = np.asarray(codes) # #21767
676-
if not is_integer_dtype(codes):
676+
if len(codes) and not is_integer_dtype(codes):
677677
msg = "codes need to be array-like integers"
678678
if is_float_dtype(codes):
679679
icodes = codes.astype("i8")

pandas/tests/arrays/categorical/test_constructors.py

+3
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ def test_from_codes_with_float(self):
526526
codes = [1.0, 2.0, 0] # integer, but in float dtype
527527
dtype = CategoricalDtype(categories=["a", "b", "c"])
528528

529+
# empty codes should not raise for floats
530+
Categorical.from_codes([], dtype.categories)
531+
529532
with tm.assert_produces_warning(FutureWarning):
530533
cat = Categorical.from_codes(codes, dtype.categories)
531534
tm.assert_numpy_array_equal(cat.codes, np.array([1, 2, 0], dtype="i1"))

0 commit comments

Comments
 (0)