Skip to content

Commit ede1fe9

Browse files
AnnaDaglismeeseeksmachine
authored andcommitted
Backport PR pandas-dev#32242: BUG: Fixed bug, where pandas._libs.lib.maybe_convert_objects function improperly handled arrays with bools and NaNs
1 parent 53f9908 commit ede1fe9

File tree

5 files changed

+46
-1
lines changed

5 files changed

+46
-1
lines changed

doc/source/whatsnew/v1.0.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Fixed regressions
2424
- Fixed regression in :class:`DataFrame` arithmetic operations with mis-matched columns (:issue:`31623`)
2525
- Fixed regression in :meth:`GroupBy.agg` calling a user-provided function an extra time on an empty input (:issue:`31760`)
2626
- Joining on :class:`DatetimeIndex` or :class:`TimedeltaIndex` will preserve ``freq`` in simple cases (:issue:`32166`)
27+
- Fixed bug in the repr of an object-dtype ``Index`` with bools and missing values (:issue:`32146`)
2728
-
2829

2930
.. ---------------------------------------------------------------------------

pandas/_libs/lib.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2235,7 +2235,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
22352235
return uints
22362236
else:
22372237
return ints
2238-
elif seen.is_bool:
2238+
elif seen.is_bool and not seen.nan_:
22392239
return bools.view(np.bool_)
22402240

22412241
return objects

pandas/tests/dtypes/test_inference.py

+7
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,13 @@ def test_maybe_convert_objects_nullable_integer(self, exp):
568568

569569
tm.assert_extension_array_equal(result, exp)
570570

571+
def test_maybe_convert_objects_bool_nan(self):
572+
# GH32146
573+
ind = pd.Index([True, False, np.nan], dtype=object)
574+
exp = np.array([True, False, np.nan], dtype=object)
575+
out = lib.maybe_convert_objects(ind.values, safe=1)
576+
tm.assert_numpy_array_equal(out, exp)
577+
571578
def test_mixed_dtypes_remain_object_array(self):
572579
# GH14956
573580
array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object)

pandas/tests/indexes/test_base.py

+11
Original file line numberDiff line numberDiff line change
@@ -2697,6 +2697,17 @@ def test_intersect_str_dates(self):
26972697
expected = Index([], dtype=object)
26982698
tm.assert_index_equal(result, expected)
26992699

2700+
def test_index_repr_bool_nan(self):
2701+
# GH32146
2702+
arr = Index([True, False, np.nan], dtype=object)
2703+
exp1 = arr.format()
2704+
out1 = ["True", "False", "NaN"]
2705+
assert out1 == exp1
2706+
2707+
exp2 = repr(arr)
2708+
out2 = "Index([True, False, nan], dtype='object')"
2709+
assert out2 == exp2
2710+
27002711

27012712
class TestIndexUtils:
27022713
@pytest.mark.parametrize(

pandas/tests/series/methods/test_value_counts.py

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import numpy as np
2+
import pytest
23

34
import pandas as pd
45
from pandas import Categorical, CategoricalIndex, Series
@@ -177,3 +178,28 @@ def test_value_counts_categorical_with_nan(self):
177178
exp = Series([2, 1, 3], index=CategoricalIndex(["a", "b", np.nan]))
178179
res = ser.value_counts(dropna=False, sort=False)
179180
tm.assert_series_equal(res, exp)
181+
182+
@pytest.mark.parametrize(
183+
"ser, dropna, exp",
184+
[
185+
(
186+
pd.Series([False, True, True, pd.NA]),
187+
False,
188+
pd.Series([2, 1, 1], index=[True, False, pd.NA]),
189+
),
190+
(
191+
pd.Series([False, True, True, pd.NA]),
192+
True,
193+
pd.Series([2, 1], index=[True, False]),
194+
),
195+
(
196+
pd.Series(range(3), index=[True, False, np.nan]).index,
197+
False,
198+
pd.Series([1, 1, 1], index=[True, False, pd.NA]),
199+
),
200+
],
201+
)
202+
def test_value_counts_bool_with_nan(self, ser, dropna, exp):
203+
# GH32146
204+
out = ser.value_counts(dropna=dropna)
205+
tm.assert_series_equal(out, exp)

0 commit comments

Comments
 (0)