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 8 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: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,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 in :meth:`Categorical.get_values` where integers would be formatted as floats if ``NaN`` values were present (:issue:`19214`)
Copy link
Contributor

Choose a reason for hiding this comment

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

this is not the user facing note though, it is formtting of a Series/Categorical where this happens. .get_values() its just an implementation detail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jreback I'm a bit confused as to what a user-facing note is. Is it a specific section somewhere? Or does it mean to phrase the note differently, like "bug in the Categorical repr"?

Copy link
Contributor

Choose a reason for hiding this comment

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

sorry. what I mean is a user would want to know that the string repr of Series (or categorical dtype) and Categoricals for integer categories will now not be coerced to float. Its maybe worth making this a sub-section (e.g. show previous / new behavior). you can just in-line it here.


Datetimelike
^^^^^^^^^^^^
Expand Down Expand Up @@ -1653,6 +1654,7 @@ Reshaping
- :meth:`DataFrame.nlargest` and :meth:`DataFrame.nsmallest` now returns the correct n values when keep != 'all' also when tied on the first columns (:issue:`22752`)
- Constructing a DataFrame with an index argument that wasn't already an instance of :class:`~pandas.core.Index` was broken (:issue:`22227`).
- Bug in :class:`DataFrame` prevented list subclasses to be used to construction (:issue:`21226`)
- Calling :func:`pandas.concat` on a ``Categorical`` of ints with NA values now causes them to be processed as objects (formerly coerced to floats) (:issue:`19214`)
Copy link
Contributor

@TomAugspurger TomAugspurger Jan 1, 2019

Choose a reason for hiding this comment

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

This only applies when concating a categorical with a different dtype, right? If I concat two integer cats with the same dtype, it’s still categorical right?

- Bug in :func:`DataFrame.unstack` and :func:`DataFrame.pivot_table` returning a missleading error message when the resulting DataFrame has more elements than int32 can handle. Now, the error message is improved, pointing towards the actual problem (:issue:`20601`)

.. _whatsnew_0240.bug_fixes.sparse:
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,6 +1520,11 @@ def get_values(self):
# if we are a datetime and period index, return Index to keep metadata
if is_datetimelike(self.categories):
return self.categories.take(self._codes, fill_value=np.nan)
elif is_integer_dtype(self.categories) and -1 in self._codes:
warn("Integer values represented as objects to accomodate NaNs",
Copy link
Contributor

Choose a reason for hiding this comment

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

no need for a warning

RuntimeWarning)
return self.categories.astype("object").take(self._codes,
fill_value=np.nan)
return np.array(self)

def check_for_ordered(self, op):
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/arrays/categorical/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

class TestCategoricalMissing(object):

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_na_flags_int_categories(self):
# #1457

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/arrays/categorical/test_repr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import numpy as np
import pytest

from pandas.compat import PY3, u

Expand Down Expand Up @@ -240,6 +241,16 @@ def test_categorical_repr_datetime_ordered(self):

assert repr(c) == exp

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
Copy link
Contributor

Choose a reason for hiding this comment

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

no warnings are needed

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
1 change: 1 addition & 0 deletions pandas/tests/frame/test_missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ def test_na_actions_categorical(self):
res = df.fillna("a")
tm.assert_frame_equal(res, df_exp)

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_fillna_categorical_nan(self):
# GH 14021
# np.nan should always be a valid filler
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/reductions/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,7 @@ def test_mode_timedelta(self, dropna, expected1, expected2):
expected2 = Series(expected2, dtype='timedelta64[ns]')
tm.assert_series_equal(result, expected2)

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
@pytest.mark.parametrize('dropna, expected1, expected2, expected3', [
(True, Categorical([1, 2], categories=[1, 2]),
Categorical(['a'], categories=[1, 'a']),
Expand Down
23 changes: 15 additions & 8 deletions pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ def test_concatlike_common_period_mixed_dt_to_object(self):
res = pd.concat([tds, ps1])
tm.assert_series_equal(res, pd.Series(exp, index=[0, 1, 0, 1]))

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_concat_categorical(self):
# GH 13524

Expand All @@ -496,7 +497,7 @@ def test_concat_categorical(self):
s1 = pd.Series([10, 11, np.nan], dtype='category')
s2 = pd.Series([np.nan, 1, 3, 2], dtype='category')

exp = pd.Series([10, 11, np.nan, np.nan, 1, 3, 2])
exp = pd.Series([10, 11, np.nan, np.nan, 1, 3, 2], dtype='object')
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1.append(s2, ignore_index=True), exp)

Expand All @@ -509,19 +510,20 @@ def test_union_categorical_same_categories_different_order(self):
categories=['a', 'b', 'c']))
tm.assert_series_equal(result, expected)

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_concat_categorical_coercion(self):
# GH 13524

# category + not-category => not-category
s1 = pd.Series([1, 2, np.nan], dtype='category')
s2 = pd.Series([2, 1, 2])

exp = pd.Series([1, 2, np.nan, 2, 1, 2])
exp = pd.Series([1, 2, np.nan, 2, 1, 2], dtype='object')
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1.append(s2, ignore_index=True), exp)

# result shouldn't be affected by 1st elem dtype
exp = pd.Series([2, 1, 2, 1, 2, np.nan])
exp = pd.Series([2, 1, 2, 1, 2, np.nan], dtype='object')
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s2.append(s1, ignore_index=True), exp)

Expand All @@ -541,11 +543,11 @@ def test_concat_categorical_coercion(self):
s1 = pd.Series([10, 11, np.nan], dtype='category')
s2 = pd.Series([1, 3, 2])

exp = pd.Series([10, 11, np.nan, 1, 3, 2])
exp = pd.Series([10, 11, np.nan, 1, 3, 2], dtype='object')
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1.append(s2, ignore_index=True), exp)

exp = pd.Series([1, 3, 2, 10, 11, np.nan])
exp = pd.Series([1, 3, 2, 10, 11, np.nan], dtype='object')
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s2.append(s1, ignore_index=True), exp)

Expand Down Expand Up @@ -573,6 +575,7 @@ def test_concat_categorical_coercion(self):
tm.assert_series_equal(pd.concat([s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s2.append(s1, ignore_index=True), exp)

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_concat_categorical_3elem_coercion(self):
# GH 13524

Expand All @@ -581,11 +584,13 @@ def test_concat_categorical_3elem_coercion(self):
s2 = pd.Series([2, 1, 2], dtype='category')
s3 = pd.Series([1, 2, 1, 2, np.nan])

exp = pd.Series([1, 2, np.nan, 2, 1, 2, 1, 2, 1, 2, np.nan])
exp = pd.Series([1, 2, np.nan, 2, 1, 2, 1, 2, 1, 2, np.nan],
dtype='object')
tm.assert_series_equal(pd.concat([s1, s2, s3], ignore_index=True), exp)
tm.assert_series_equal(s1.append([s2, s3], ignore_index=True), exp)

exp = pd.Series([1, 2, 1, 2, np.nan, 1, 2, np.nan, 2, 1, 2])
exp = pd.Series([1, 2, 1, 2, np.nan, 1, 2, np.nan, 2, 1, 2],
dtype='object')
tm.assert_series_equal(pd.concat([s3, s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s3.append([s1, s2], ignore_index=True), exp)

Expand Down Expand Up @@ -638,6 +643,7 @@ def test_concat_categorical_multi_coercion(self):
res = s6.append([s5, s4, s3, s2, s1], ignore_index=True)
tm.assert_series_equal(res, exp)

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_concat_categorical_ordered(self):
# GH 13524

Expand All @@ -653,6 +659,7 @@ def test_concat_categorical_ordered(self):
tm.assert_series_equal(pd.concat([s1, s2, s1], ignore_index=True), exp)
tm.assert_series_equal(s1.append([s2, s1], ignore_index=True), exp)

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_concat_categorical_coercion_nan(self):
# GH 13524

Expand All @@ -669,7 +676,7 @@ def test_concat_categorical_coercion_nan(self):
s1 = pd.Series([1, np.nan], dtype='category')
s2 = pd.Series([np.nan, np.nan])

exp = pd.Series([1, np.nan, np.nan, np.nan])
exp = pd.Series([1, np.nan, np.nan, np.nan], dtype='object')
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1.append(s2, ignore_index=True), exp)

Expand Down
1 change: 1 addition & 0 deletions pandas/tests/series/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ def test_setitem_with_tz_dst():
tm.assert_series_equal(s, exp)


@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_categorial_assigning_ops():
orig = Series(Categorical(["b", "b"], categories=["a", "b"]))
s = orig.copy()
Expand Down
1 change: 1 addition & 0 deletions pandas/tests/series/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1314,6 +1314,7 @@ def test_duplicate_keep_all_ties(self):

class TestCategoricalSeriesAnalytics(object):

@pytest.mark.filterwarnings("ignore:Integer values:RuntimeWarning")
def test_count(self):

s = Series(Categorical([np.nan, 1, 2, np.nan],
Expand Down