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 2 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
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
12 changes: 10 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ def _get_footer(self):
return compat.text_type(footer)

def _get_formatted_values(self):
return format_array(self.categorical.get_values(), None,
float_format=None, na_rep=self.na_rep)
results = format_array(self.categorical.get_values(), None,
float_format=None, na_rep=self.na_rep)
if is_integer_dtype(self.categorical.dtype.categories):
return [result.replace(".0", "") for result in results]
return results

def to_string(self):
categorical = self.categorical
Expand Down Expand Up @@ -1176,6 +1179,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
9 changes: 9 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,15 @@ def test_categorical_repr_datetime_ordered(self):

assert repr(c) == exp

def test_categorical_repr_int_with_nan(self):
c = Categorical([1, 2, np.nan])
c_exp = """[1, 2, NaN]\nCategories (2, int64): [1, 2]"""
assert repr(c) == c_exp

s = Series([1, 2, np.nan], dtype="object").astype("category")
s_exp = """0 1\n1 2\n2 NaN\ndtype: category\nCategories (2, int64): [1, 2]""" # noqa
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use parenthesis to wrap this line.

assert repr(s) == s_exp

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