Skip to content

BUG: Categorical.from_codes should not warn on empty codes #29336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions pandas/tests/arrays/categorical/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down