Skip to content

Commit cf12a1f

Browse files
committed
consolidate special casing
1 parent 77f171d commit cf12a1f

File tree

4 files changed

+11
-21
lines changed

4 files changed

+11
-21
lines changed

pandas/core/generic.py

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

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:
178+
if dtype is not None:
182179
dtype = pandas_dtype(dtype)
183180

184181
# a compound dtype

pandas/core/internals/construction.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,7 @@ 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-
# GH 26336: don't convert 'category' to CategoricalDtype
542-
if isinstance(dtype, str) and dtype == 'category':
543-
pass
544-
elif dtype is not None:
541+
if dtype is not None:
545542
dtype = pandas_dtype(dtype)
546543

547544
if isinstance(data, ma.MaskedArray):

pandas/core/series.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from pandas.util._validators import validate_bool_kwarg
1919

2020
from pandas.core.dtypes.common import (
21-
_is_unorderable_exception, ensure_platform_int, is_bool,
21+
_is_unorderable_exception, ensure_platform_int, is_bool, is_categorical,
2222
is_categorical_dtype, is_datetime64_dtype, is_datetimelike, is_dict_like,
2323
is_extension_array_dtype, is_extension_type, is_hashable, is_integer,
2424
is_iterator, is_list_like, is_scalar, is_string_like, is_timedelta64_dtype)
@@ -168,6 +168,12 @@ def __init__(self, data=None, index=None, dtype=None, name=None,
168168
if data is None:
169169
data = {}
170170
if dtype is not None:
171+
# GH 26336: explicitly handle 'category' to avoid warning
172+
# TODO: Remove after CategoricalDtype defaults to ordered=False
173+
if (isinstance(dtype, str) and dtype == 'category' and
174+
is_categorical(data)):
175+
dtype = data.dtype
176+
171177
dtype = self._validate_dtype(dtype)
172178

173179
if isinstance(data, MultiIndex):

pandas/io/packers.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -623,19 +623,9 @@ def decode(obj):
623623
return Interval(obj['left'], obj['right'], obj['closed'])
624624
elif typ == 'series':
625625
dtype = dtype_for(obj['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)
632-
633626
index = obj['index']
634-
result = Series(unconvert(obj['data'], dtype, obj['compress']),
635-
index=index,
636-
dtype=pd_dtype,
637-
name=obj['name'])
638-
return result
627+
data = unconvert(obj['data'], dtype, obj['compress'])
628+
return Series(data, index=index, dtype=dtype, name=obj['name'])
639629

640630
elif typ == 'block_manager':
641631
axes = obj['axes']

0 commit comments

Comments
 (0)