From da865e2fc03b11309ec50dac5ede2874d5a40925 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 8 Sep 2016 19:06:17 -0500 Subject: [PATCH 1/2] BUG: Categorical constructor not idempotent with ext dtype --- doc/source/whatsnew/v0.19.0.txt | 2 +- pandas/core/categorical.py | 2 +- pandas/tests/test_categorical.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v0.19.0.txt b/doc/source/whatsnew/v0.19.0.txt index d672b9b897fda..7e40d04c6b889 100644 --- a/doc/source/whatsnew/v0.19.0.txt +++ b/doc/source/whatsnew/v0.19.0.txt @@ -1427,7 +1427,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`) diff --git a/pandas/core/categorical.py b/pandas/core/categorical.py index 48054c5bd34fa..0a13c8936eeec 100644 --- a/pandas/core/categorical.py +++ b/pandas/core/categorical.py @@ -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 diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index c4ddd2c0981d9..747d5ec8f7002 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -362,6 +362,21 @@ 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.array([1, 2, 3], dtype='int64'), + ['a', 'b', 'c'], + pd.period_range('2014-01-01', '2014-01-05'), + pd.date_range('2014-01-01', '2014-01-05'), + pd.date_range('2014-01-01', '2014-01-05', tz='US/Eastern') + ] + for val in vals: + c = Categorical(val) + c2 = Categorical(c) + tm.assert_categorical_equal(c, c2) + def test_from_codes(self): # too few categories From 4cad147d42826ca5c8c665a96a97ccd90d226726 Mon Sep 17 00:00:00 2001 From: Chris Date: Thu, 8 Sep 2016 19:20:47 -0500 Subject: [PATCH 2/2] add some nulls to tests --- pandas/tests/test_categorical.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/tests/test_categorical.py b/pandas/tests/test_categorical.py index 747d5ec8f7002..a494a0d53b123 100644 --- a/pandas/tests/test_categorical.py +++ b/pandas/tests/test_categorical.py @@ -365,12 +365,13 @@ def test_constructor_from_index_series_period(self): def test_constructor_invariant(self): # GH 14190 vals = [ - np.array([1., 1.2, 1.8]), + np.array([1., 1.2, 1.8, np.nan]), np.array([1, 2, 3], dtype='int64'), - ['a', 'b', 'c'], - pd.period_range('2014-01-01', '2014-01-05'), - pd.date_range('2014-01-01', '2014-01-05'), - pd.date_range('2014-01-01', '2014-01-05', tz='US/Eastern') + ['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)