Skip to content

Commit 2da967d

Browse files
committed
cover additional case and review edits
1 parent 4ad16c9 commit 2da967d

File tree

8 files changed

+46
-7
lines changed

8 files changed

+46
-7
lines changed

pandas/core/dtypes/dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ def update_dtype(self, dtype):
546546
msg = ("ordered=None is deprecated and will default to False "
547547
"in a future version; ordered=True must be explicitly "
548548
"passed in order to be retained")
549-
warnings.warn(msg, FutureWarning, stacklevel=2)
549+
warnings.warn(msg, FutureWarning, stacklevel=3)
550550

551551
return CategoricalDtype(new_categories, new_ordered)
552552

pandas/core/generic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ def is_copy(self, msg):
175175
def _validate_dtype(self, dtype):
176176
""" validate the passed dtype """
177177

178-
if dtype is not None:
178+
# GH 26336: don't convert 'category' to CategoricalDtype
179+
if isinstance(dtype, str) and dtype == 'category':
180+
pass
181+
elif dtype is not None:
179182
dtype = pandas_dtype(dtype)
180183

181184
# a compound dtype

pandas/core/internals/construction.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ def sanitize_array(data, index, dtype=None, copy=False,
538538
Sanitize input data to an ndarray, copy if specified, coerce to the
539539
dtype if specified.
540540
"""
541-
if dtype is not None:
541+
# GH 26336: don't convert 'category' to CategoricalDtype
542+
if isinstance(dtype, str) and dtype == 'category':
543+
pass
544+
elif dtype is not None:
542545
dtype = pandas_dtype(dtype)
543546

544547
if isinstance(data, ma.MaskedArray):

pandas/io/packers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,12 @@ def decode(obj):
623623
return Interval(obj['left'], obj['right'], obj['closed'])
624624
elif typ == 'series':
625625
dtype = dtype_for(obj['dtype'])
626-
pd_dtype = pandas_dtype(dtype)
626+
627+
# GH 26336: don't convert 'category' to CategoricalDtype
628+
if isinstance(dtype, str) and dtype == 'category':
629+
pd_dtype = dtype
630+
else:
631+
pd_dtype = pandas_dtype(dtype)
627632

628633
index = obj['index']
629634
result = Series(unconvert(obj['data'], dtype, obj['compress']),

pandas/tests/arrays/categorical/test_dtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_astype_category_ordered_none_deprecated(self):
165165
cdt1 = CategoricalDtype(categories=list('cdab'), ordered=True)
166166
cdt2 = CategoricalDtype(categories=list('cedafb'))
167167
cat = Categorical(list('abcdaba'), dtype=cdt1)
168-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
168+
with tm.assert_produces_warning(FutureWarning):
169169
cat.astype(cdt2)
170170

171171
def test_iter_python_types(self):

pandas/tests/dtypes/test_dtypes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,8 @@ def test_update_dtype(self, ordered_fixture, new_categories, new_ordered):
819819

820820
# GH 26336
821821
if new_ordered is None and ordered_fixture is True:
822-
with tm.assert_produces_warning(FutureWarning):
822+
with tm.assert_produces_warning(FutureWarning,
823+
check_stacklevel=False):
823824
result = dtype.update_dtype(new_dtype)
824825
else:
825826
result = dtype.update_dtype(new_dtype)

pandas/tests/indexes/test_category.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ def test_astype_category_ordered_none_deprecated(self):
495495
cdt1 = CategoricalDtype(categories=list('cdab'), ordered=True)
496496
cdt2 = CategoricalDtype(categories=list('cedafb'))
497497
idx = CategoricalIndex(list('abcdaba'), dtype=cdt1)
498-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
498+
with tm.assert_produces_warning(FutureWarning):
499499
idx.astype(cdt2)
500500

501501
def test_reindex_base(self):

pandas/tests/series/test_constructors.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,33 @@ def test_constructor_categorical_dtype(self):
365365
dtype=CategoricalDtype(['a', 'b'], ordered=True))
366366
tm.assert_series_equal(result, expected, check_categorical=True)
367367

368+
def test_constructor_categorical_string(self):
369+
# GH 26336: the string 'category' maintains existing CategoricalDtype
370+
cdt = CategoricalDtype(categories=list('dabc'), ordered=True)
371+
expected = Series(list('abcabc'), dtype=cdt)
372+
373+
# Series(Categorical, dtype='category') keeps existing dtype
374+
cat = Categorical(list('abcabc'), dtype=cdt)
375+
result = Series(cat, dtype='category')
376+
tm.assert_series_equal(result, expected)
377+
378+
# Series(Series[Categorical], dtype='category') keeps existing dtype
379+
result = Series(result, dtype='category')
380+
tm.assert_series_equal(result, expected)
381+
382+
def test_categorical_ordered_none_deprecated(self):
383+
# GH 26336
384+
cdt1 = CategoricalDtype(categories=list('cdab'), ordered=True)
385+
cdt2 = CategoricalDtype(categories=list('cedafb'))
386+
387+
cat = Categorical(list('abcdaba'), dtype=cdt1)
388+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
389+
Series(cat, dtype=cdt2)
390+
391+
s = Series(cat)
392+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
393+
Series(s, dtype=cdt2)
394+
368395
def test_categorical_sideeffects_free(self):
369396
# Passing a categorical to a Series and then changing values in either
370397
# the series or the categorical should not change the values in the

0 commit comments

Comments
 (0)