Skip to content

Commit 7d9f5ec

Browse files
committed
Drop support for NaN categories in Categorical
Deprecated in 0.17.0. xref gh-10748
1 parent c577c19 commit 7d9f5ec

File tree

3 files changed

+10
-18
lines changed

3 files changed

+10
-18
lines changed

doc/source/whatsnew/v0.20.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,7 @@ Removal of prior version deprecations/changes
811811
in favor of ``iloc`` and ``iat`` as explained :ref:`here <whatsnew_0170.deprecations>` (:issue:`10711`).
812812
- The deprecated ``DataFrame.iterkv()`` has been removed in favor of ``DataFrame.iteritems()`` (:issue:`10711`)
813813
- The ``Categorical`` constructor has dropped the ``name`` parameter (:issue:`10632`)
814+
- ``Categorical`` has dropped support for ``NaN`` categories (:issue:`10748`)
814815
- The ``take_last`` parameter has been dropped from ``duplicated()``, ``drop_duplicates()``, ``nlargest()``, and ``nsmallest()`` methods (:issue:`10236`, :issue:`10792`, :issue:`10920`)
815816
- ``Series``, ``Index``, and ``DataFrame`` have dropped the ``sort`` and ``order`` methods (:issue:`10726`)
816817
- Where clauses in ``pytables`` are only accepted as strings and expressions types and not other data-types (:issue:`12027`)

pandas/core/categorical.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -545,18 +545,11 @@ def _validate_categories(cls, categories, fastpath=False):
545545

546546
if not fastpath:
547547

548-
# check properties of the categories
549-
# we don't allow NaNs in the categories themselves
550-
548+
# Categories cannot contain NaN.
551549
if categories.hasnans:
552-
# NaNs in cats deprecated in 0.17
553-
# GH 10748
554-
msg = ('\nSetting NaNs in `categories` is deprecated and '
555-
'will be removed in a future version of pandas.')
556-
warn(msg, FutureWarning, stacklevel=3)
557-
558-
# categories must be unique
550+
raise ValueError('Categorial categories cannot be NaN')
559551

552+
# Categories must be unique.
560553
if not categories.is_unique:
561554
raise ValueError('Categorical categories must be unique')
562555

pandas/tests/test_categorical.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,6 @@ def f():
222222
cat = pd.Categorical([np.nan, 1., 2., 3.])
223223
self.assertTrue(is_float_dtype(cat.categories))
224224

225-
# Deprecating NaNs in categoires (GH #10748)
226-
# preserve int as far as possible by converting to object if NaN is in
227-
# categories
228-
with tm.assert_produces_warning(FutureWarning):
229-
cat = pd.Categorical([np.nan, 1, 2, 3],
230-
categories=[np.nan, 1, 2, 3])
231-
self.assertTrue(is_object_dtype(cat.categories))
232-
233225
# This doesn't work -> this would probably need some kind of "remember
234226
# the original type" feature to try to cast the array interface result
235227
# to...
@@ -418,6 +410,12 @@ def f():
418410

419411
self.assertRaises(ValueError, f)
420412

413+
# NaN categories included
414+
def f():
415+
Categorical.from_codes([0, 1, 2], ["a", "b", np.nan])
416+
417+
self.assertRaises(ValueError, f)
418+
421419
# too negative
422420
def f():
423421
Categorical.from_codes([-2, 1, 2], ["a", "b", "c"])

0 commit comments

Comments
 (0)