Skip to content

BUG-19214 int categoricals are formatted as ints #24494

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 14 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,7 @@ Categorical
- Bug in many methods of the ``.str``-accessor, which always failed on calling the ``CategoricalIndex.str`` constructor (:issue:`23555`, :issue:`23556`)
- Bug in :meth:`Series.where` losing the categorical dtype for categorical data (:issue:`24077`)
- Bug in :meth:`Categorical.apply` where ``NaN`` values could be handled unpredictably. They now remain unchanged (:issue:`24241`)
- Bug where an integer ``Categorical`` would be formatted as if it had floats if ``NaN`` values were present (:issue:`19214`)

Datetimelike
^^^^^^^^^^^^
Expand Down
5 changes: 5 additions & 0 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,11 @@ def _format_strings(self):
na_rep=self.na_rep, digits=self.digits,
space=self.space, justify=self.justify,
leading_space=self.leading_space)

if (is_categorical_dtype(values.dtype) and
is_integer_dtype(values.dtype.categories)):
# integers were coerced to float for array with NaN (GH 19214)
return [value.replace(".0", "") for value in fmt_values]
return fmt_values


Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/arrays/categorical/test_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ def test_categorical_repr_datetime_ordered(self):

assert repr(c) == exp

def test_categorical_repr_int_with_nan(self):
s = Series([1, 2, np.nan], dtype="object").astype("category")
assert ".0" not in repr(s)

def test_categorical_repr_period(self):
idx = period_range('2011-01-01 09:00', freq='H', periods=5)
c = Categorical(idx)
Expand Down