Skip to content

Commit 1069679

Browse files
AnnaDaglisSeeminSyed
authored andcommitted
BUG: Fixed bug, where pandas._libs.lib.maybe_convert_objects function improperly handled arrays with bools and NaNs (pandas-dev#32242)
* BUG: Fixed bug, where pandas._libs.lib.maybe_convert_objects function improperly handled arrays with bools and NaNs
1 parent 24c0028 commit 1069679

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
@@ -2296,7 +2296,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
22962296
return uints
22972297
else:
22982298
return ints
2299-
elif seen.is_bool:
2299+
elif seen.is_bool and not seen.nan_:
23002300
return bools.view(np.bool_)
23012301

23022302
return objects

pandas/tests/dtypes/test_inference.py

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

576576
tm.assert_extension_array_equal(result, exp)
577577

578+
def test_maybe_convert_objects_bool_nan(self):
579+
# GH32146
580+
ind = pd.Index([True, False, np.nan], dtype=object)
581+
exp = np.array([True, False, np.nan], dtype=object)
582+
out = lib.maybe_convert_objects(ind.values, safe=1)
583+
tm.assert_numpy_array_equal(out, exp)
584+
578585
def test_mixed_dtypes_remain_object_array(self):
579586
# GH14956
580587
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
@@ -2458,6 +2458,17 @@ def test_intersect_str_dates(self):
24582458
expected = Index([], dtype=object)
24592459
tm.assert_index_equal(result, expected)
24602460

2461+
def test_index_repr_bool_nan(self):
2462+
# GH32146
2463+
arr = Index([True, False, np.nan], dtype=object)
2464+
exp1 = arr.format()
2465+
out1 = ["True", "False", "NaN"]
2466+
assert out1 == exp1
2467+
2468+
exp2 = repr(arr)
2469+
out2 = "Index([True, False, nan], dtype='object')"
2470+
assert out2 == exp2
2471+
24612472

24622473
class TestIndexUtils:
24632474
@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)