Skip to content

Commit 562d625

Browse files
committed
Make dtype=category always imply ordered=False
1 parent 513e787 commit 562d625

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

pandas/core/generic.py

+18
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
DatetimeTZDtype,
125125
ExtensionDtype,
126126
PeriodDtype,
127+
CategoricalDtype
127128
)
128129
from pandas.core.dtypes.generic import (
129130
ABCDataFrame,
@@ -6454,8 +6455,25 @@ def astype(
64546455

64556456
else:
64566457
# else, only a single dtype is given
6458+
6459+
# GH 61074: Make dtype="category" imply "ordered" = False + add deprecation warning
6460+
if dtype == "category":
6461+
if isinstance(self.dtype, CategoricalDtype):
6462+
if self.dtype.ordered:
6463+
warnings.warn(
6464+
"The 'category' dtype is being set to ordered=False by default.",
6465+
DeprecationWarning,
6466+
stacklevel=3
6467+
)
6468+
6469+
if isinstance(dtype, CategoricalDtype):
6470+
dtype = CategoricalDtype(categories=dtype.categories ,ordered=False)
6471+
else:
6472+
dtype = CategoricalDtype(ordered=False)
6473+
64576474
new_data = self._mgr.astype(dtype=dtype, errors=errors)
64586475
res = self._constructor_from_mgr(new_data, axes=new_data.axes)
6476+
64596477
return res.__finalize__(self, method="astype")
64606478

64616479
# GH 33113: handle empty frame or series

pandas/tests/series/methods/test_astype.py

+22-6
Original file line numberDiff line numberDiff line change
@@ -610,22 +610,38 @@ def test_astype_categoricaldtype(self):
610610
def test_astype_categorical_to_categorical(
611611
self, name, dtype_ordered, series_ordered
612612
):
613+
614+
def check_deprecation_warning(series):
615+
''' Helper function to check DeprecationWarning for ordered = True conversions'''
616+
msg = "The 'category' dtype is being set to ordered=False by default."
617+
with tm.assert_produces_warning(DeprecationWarning, match = msg):
618+
result = series.astype("category")
619+
assert result.dtype.ordered is False
620+
613621
# GH#10696, GH#18593
614622
s_data = list("abcaacbab")
615623
s_dtype = CategoricalDtype(list("bac"), ordered=series_ordered)
616624
ser = Series(s_data, dtype=s_dtype, name=name)
617-
625+
626+
# GH#61074
627+
if series_ordered is True:
628+
check_deprecation_warning(ser)
629+
s_dtype = CategoricalDtype(list("bac"), ordered=False)
630+
ser = Series(s_data, dtype=s_dtype, name=name)
631+
632+
# GH#61074
618633
# unspecified categories
619-
dtype = CategoricalDtype(ordered=dtype_ordered)
620-
result = ser.astype(dtype)
621-
exp_dtype = CategoricalDtype(s_dtype.categories, dtype_ordered)
634+
dtype = CategoricalDtype(ordered=False)
635+
result = ser.astype(dtype)
636+
exp_dtype = CategoricalDtype(s_dtype.categories, ordered=False)
622637
expected = Series(s_data, name=name, dtype=exp_dtype)
623638
tm.assert_series_equal(result, expected)
624639

640+
# GH#61074
625641
# different categories
626-
dtype = CategoricalDtype(list("adc"), dtype_ordered)
642+
dtype = CategoricalDtype(list("adc"), False)
627643
result = ser.astype(dtype)
628-
expected = Series(s_data, name=name, dtype=dtype)
644+
expected = Series(s_data, name=name, dtype=dtype)
629645
tm.assert_series_equal(result, expected)
630646

631647
if dtype_ordered is False:

0 commit comments

Comments
 (0)