Skip to content

Commit adc0bca

Browse files
committed
DEPR: Deprecate ordered=None for CategoricalDtype
1 parent 3b24fb6 commit adc0bca

File tree

6 files changed

+39
-1
lines changed

6 files changed

+39
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ Deprecations
257257

258258
- Deprecated the ``units=M`` (months) and ``units=Y`` (year) parameters for ``units`` of :func:`pandas.to_timedelta`, :func:`pandas.Timedelta` and :func:`pandas.TimedeltaIndex` (:issue:`16344`)
259259
- The functions :func:`pandas.to_datetime` and :func:`pandas.to_timedelta` have deprecated the ``box`` keyword. Instead, use :meth:`to_numpy` or :meth:`Timestamp.to_datetime64` or :meth:`Timedelta.to_timedelta64`. (:issue:`24416`)
260+
- The default value ``ordered=None`` in :class:`~pandas.api.types.CategoricalDtype` has been deprecated in favor of ``ordered=False``. When converting between categorical types ``ordered=True`` must be explicitly passed in order to be preserved. (:issue:`26336`)
260261

261262
.. _whatsnew_0250.prior_deprecations:
262263

pandas/core/dtypes/dtypes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,15 @@ def update_dtype(self, dtype):
550550
new_categories = self.categories
551551

552552
new_ordered = dtype.ordered
553+
554+
# TODO(GH26336): remove this if block when ordered=None is removed
553555
if new_ordered is None:
554556
new_ordered = self.ordered
557+
if self.ordered:
558+
msg = ("ordered=None is deprecated and will default to False "
559+
"in a future version; ordered=True must be explicitly "
560+
"passed in order to be retained")
561+
warnings.warn(msg, FutureWarning, stacklevel=2)
555562

556563
return CategoricalDtype(new_categories, new_ordered)
557564

pandas/tests/arrays/categorical/test_dtypes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ def test_astype_category(self, dtype_ordered, cat_ordered):
160160
expected = cat
161161
tm.assert_categorical_equal(result, expected)
162162

163+
def test_astype_category_ordered_none_deprecated(self):
164+
# GH 26336
165+
cdt1 = CategoricalDtype(categories=list('cdab'), ordered=True)
166+
cdt2 = CategoricalDtype(categories=list('cedafb'))
167+
cat = Categorical(list('abcdaba'), dtype=cdt1)
168+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
169+
cat.astype(cdt2)
170+
163171
def test_iter_python_types(self):
164172
# GH-19909
165173
cat = Categorical([1, 2])

pandas/tests/dtypes/test_dtypes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,13 @@ def test_update_dtype(self, ordered_fixture, new_categories, new_ordered):
817817
if expected_ordered is None:
818818
expected_ordered = dtype.ordered
819819

820-
result = dtype.update_dtype(new_dtype)
820+
# GH 26336
821+
if new_ordered is None and ordered_fixture is True:
822+
with tm.assert_produces_warning(FutureWarning):
823+
result = dtype.update_dtype(new_dtype)
824+
else:
825+
result = dtype.update_dtype(new_dtype)
826+
821827
tm.assert_index_equal(result.categories, expected_categories)
822828
assert result.ordered is expected_ordered
823829

pandas/tests/indexes/test_category.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,14 @@ def test_astype_category(self, name, dtype_ordered, index_ordered):
490490
expected = index
491491
tm.assert_index_equal(result, expected)
492492

493+
def test_astype_category_ordered_none_deprecated(self):
494+
# GH 26336
495+
cdt1 = CategoricalDtype(categories=list('cdab'), ordered=True)
496+
cdt2 = CategoricalDtype(categories=list('cedafb'))
497+
idx = CategoricalIndex(list('abcdaba'), dtype=cdt1)
498+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
499+
idx.astype(cdt2)
500+
493501
def test_reindex_base(self):
494502
# Determined by cat ordering.
495503
idx = CategoricalIndex(list("cab"), categories=list("cab"))

pandas/tests/series/test_dtypes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ def test_astype_categories_deprecation(self):
227227
result = s.astype('category', categories=['a', 'b'], ordered=True)
228228
tm.assert_series_equal(result, expected)
229229

230+
def test_astype_category_ordered_none_deprecated(self):
231+
# GH 26336
232+
cdt1 = CategoricalDtype(categories=list('cdab'), ordered=True)
233+
cdt2 = CategoricalDtype(categories=list('cedafb'))
234+
s = Series(list('abcdaba'), dtype=cdt1)
235+
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
236+
s.astype(cdt2)
237+
230238
def test_astype_from_categorical(self):
231239
items = ["a", "b", "c", "a"]
232240
s = Series(items)

0 commit comments

Comments
 (0)