Skip to content

Commit

Permalink
BUG: Categorical constructor not idempotent with ext dtype
Browse files Browse the repository at this point in the history
closes #14190

Author: Chris <cbartak@gmail.com>

Closes #14191 from chris-b1/cat-ctor and squashes the following commits:

4cad147 [Chris] add some nulls to tests
da865e2 [Chris] BUG: Categorical constructor not idempotent with ext dtype
  • Loading branch information
chris-b1 authored and jreback committed Sep 9, 2016
1 parent 8af6264 commit 939a221
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1426,7 +1426,7 @@ Bug Fixes

- Bug in ``SeriesGroupBy.transform`` with datetime values and missing groups (:issue:`13191`)
- Bug where empty ``Series`` were incorrectly coerced in datetime-like numeric operations (:issue:`13844`)

- Bug in ``Categorical`` constructor when passed a ``Categorical`` containing datetimes with timezones (:issue:`14190`)
- Bug in ``Series.str.extractall()`` with ``str`` index raises ``ValueError`` (:issue:`13156`)
- Bug in ``Series.str.extractall()`` with single group and quantifier (:issue:`13382`)
- Bug in ``DatetimeIndex`` and ``Period`` subtraction raises ``ValueError`` or ``AttributeError`` rather than ``TypeError`` (:issue:`13078`)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ def __init__(self, values, categories=None, ordered=False,
ordered = values.ordered
if categories is None:
categories = values.categories
values = values.__array__()
values = values.get_values()

elif isinstance(values, (ABCIndexClass, ABCSeries)):
pass
Expand Down
16 changes: 16 additions & 0 deletions pandas/tests/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,22 @@ def test_constructor_from_index_series_period(self):
result = pd.Categorical(pd.Series(idx))
tm.assert_index_equal(result.categories, idx)

def test_constructor_invariant(self):
# GH 14190
vals = [
np.array([1., 1.2, 1.8, np.nan]),
np.array([1, 2, 3], dtype='int64'),
['a', 'b', 'c', np.nan],
[pd.Period('2014-01'), pd.Period('2014-02'), pd.NaT],
[pd.Timestamp('2014-01-01'), pd.Timestamp('2014-01-02'), pd.NaT],
[pd.Timestamp('2014-01-01', tz='US/Eastern'),
pd.Timestamp('2014-01-02', tz='US/Eastern'), pd.NaT],
]
for val in vals:
c = Categorical(val)
c2 = Categorical(c)
tm.assert_categorical_equal(c, c2)

def test_from_codes(self):

# too few categories
Expand Down

0 comments on commit 939a221

Please sign in to comment.