Skip to content

Commit da865e2

Browse files
committed
BUG: Categorical constructor not idempotent with ext dtype
1 parent ff435ba commit da865e2

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ Bug Fixes
14271427

14281428
- Bug in ``SeriesGroupBy.transform`` with datetime values and missing groups (:issue:`13191`)
14291429
- Bug where empty ``Series`` were incorrectly coerced in datetime-like numeric operations (:issue:`13844`)
1430-
1430+
- Bug in ``Categorical`` constructor when passed a ``Categorical`` containing datetimes with timezones (:issue:`14190`)
14311431
- Bug in ``Series.str.extractall()`` with ``str`` index raises ``ValueError`` (:issue:`13156`)
14321432
- Bug in ``Series.str.extractall()`` with single group and quantifier (:issue:`13382`)
14331433
- Bug in ``DatetimeIndex`` and ``Period`` subtraction raises ``ValueError`` or ``AttributeError`` rather than ``TypeError`` (:issue:`13078`)

pandas/core/categorical.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def __init__(self, values, categories=None, ordered=False,
259259
ordered = values.ordered
260260
if categories is None:
261261
categories = values.categories
262-
values = values.__array__()
262+
values = values.get_values()
263263

264264
elif isinstance(values, (ABCIndexClass, ABCSeries)):
265265
pass

pandas/tests/test_categorical.py

+15
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,21 @@ def test_constructor_from_index_series_period(self):
362362
result = pd.Categorical(pd.Series(idx))
363363
tm.assert_index_equal(result.categories, idx)
364364

365+
def test_constructor_invariant(self):
366+
# GH 14190
367+
vals = [
368+
np.array([1., 1.2, 1.8]),
369+
np.array([1, 2, 3], dtype='int64'),
370+
['a', 'b', 'c'],
371+
pd.period_range('2014-01-01', '2014-01-05'),
372+
pd.date_range('2014-01-01', '2014-01-05'),
373+
pd.date_range('2014-01-01', '2014-01-05', tz='US/Eastern')
374+
]
375+
for val in vals:
376+
c = Categorical(val)
377+
c2 = Categorical(c)
378+
tm.assert_categorical_equal(c, c2)
379+
365380
def test_from_codes(self):
366381

367382
# too few categories

0 commit comments

Comments
 (0)