Skip to content

Commit 939a221

Browse files
chris-b1jreback
authored andcommitted
BUG: Categorical constructor not idempotent with ext dtype
closes pandas-dev#14190 Author: Chris <[email protected]> Closes pandas-dev#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
1 parent 8af6264 commit 939a221

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.19.0.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ Bug Fixes
14261426

14271427
- Bug in ``SeriesGroupBy.transform`` with datetime values and missing groups (:issue:`13191`)
14281428
- Bug where empty ``Series`` were incorrectly coerced in datetime-like numeric operations (:issue:`13844`)
1429-
1429+
- Bug in ``Categorical`` constructor when passed a ``Categorical`` containing datetimes with timezones (:issue:`14190`)
14301430
- Bug in ``Series.str.extractall()`` with ``str`` index raises ``ValueError`` (:issue:`13156`)
14311431
- Bug in ``Series.str.extractall()`` with single group and quantifier (:issue:`13382`)
14321432
- 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

+16
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,22 @@ 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, np.nan]),
369+
np.array([1, 2, 3], dtype='int64'),
370+
['a', 'b', 'c', np.nan],
371+
[pd.Period('2014-01'), pd.Period('2014-02'), pd.NaT],
372+
[pd.Timestamp('2014-01-01'), pd.Timestamp('2014-01-02'), pd.NaT],
373+
[pd.Timestamp('2014-01-01', tz='US/Eastern'),
374+
pd.Timestamp('2014-01-02', tz='US/Eastern'), pd.NaT],
375+
]
376+
for val in vals:
377+
c = Categorical(val)
378+
c2 = Categorical(c)
379+
tm.assert_categorical_equal(c, c2)
380+
365381
def test_from_codes(self):
366382

367383
# too few categories

0 commit comments

Comments
 (0)