Skip to content

Commit 791c7fc

Browse files
Backport PR pandas-dev#31939: BUG: Fix construction of Categorical from pd.NA (pandas-dev#32200)
Co-authored-by: Daniel Saxton <[email protected]>
1 parent 4444870 commit 791c7fc

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

doc/source/whatsnew/v1.0.2.rst

+2-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Bug fixes
6565
**Categorical**
6666

6767
- Fixed bug where :meth:`Categorical.from_codes` improperly raised a ``ValueError`` when passed nullable integer codes. (:issue:`31779`)
68+
- Fixed bug where :meth:`Categorical` constructor would raise a ``TypeError`` when given a numpy array containing ``pd.NA``. (:issue:`31927`)
6869
- Bug in :class:`Categorical` that would ignore or crash when calling :meth:`Series.replace` with a list-like ``to_replace`` (:issue:`31720`)
6970

7071
**I/O**
@@ -86,4 +87,4 @@ Bug fixes
8687
Contributors
8788
~~~~~~~~~~~~
8889

89-
.. contributors:: v1.0.1..v1.0.2|HEAD
90+
.. contributors:: v1.0.1..v1.0.2|HEAD

pandas/_libs/hashtable_class_helper.pxi.in

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
1010
# ----------------------------------------------------------------------
1111

1212
from pandas._libs.tslibs.util cimport get_c_string
13+
from pandas._libs.missing cimport C_NA
1314

1415
{{py:
1516

@@ -1032,8 +1033,12 @@ cdef class PyObjectHashTable(HashTable):
10321033
val = values[i]
10331034
hash(val)
10341035

1035-
if ignore_na and ((val != val or val is None)
1036-
or (use_na_value and val == na_value)):
1036+
if ignore_na and (
1037+
(val is C_NA)
1038+
or (val != val)
1039+
or (val is None)
1040+
or (use_na_value and val == na_value)
1041+
):
10371042
# if missing values do not count as unique values (i.e. if
10381043
# ignore_na is True), skip the hashtable entry for them, and
10391044
# replace the corresponding label with na_sentinel

pandas/tests/arrays/categorical/test_constructors.py

+12
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,18 @@ def test_constructor_with_categorical_categories(self):
458458
result = Categorical(["a", "b"], categories=CategoricalIndex(["a", "b", "c"]))
459459
tm.assert_categorical_equal(result, expected)
460460

461+
@pytest.mark.parametrize("klass", [lambda x: np.array(x, dtype=object), list])
462+
def test_construction_with_null(self, klass, nulls_fixture):
463+
# https://github.com/pandas-dev/pandas/issues/31927
464+
values = klass(["a", nulls_fixture, "b"])
465+
result = Categorical(values)
466+
467+
dtype = CategoricalDtype(["a", "b"])
468+
codes = [0, -1, 1]
469+
expected = Categorical.from_codes(codes=codes, dtype=dtype)
470+
471+
tm.assert_categorical_equal(result, expected)
472+
461473
def test_from_codes(self):
462474

463475
# too few categories

0 commit comments

Comments
 (0)