Skip to content

Backport PR #32242 on branch 1.0.x (BUG: Fixed bug, where pandas._libs.lib.maybe_convert_objects function improperly handled arrays with bools and NaNs) #32552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.0.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Fixed regressions
- Fixed regression in :class:`DataFrame` arithmetic operations with mis-matched columns (:issue:`31623`)
- Fixed regression in :meth:`GroupBy.agg` calling a user-provided function an extra time on an empty input (:issue:`31760`)
- Joining on :class:`DatetimeIndex` or :class:`TimedeltaIndex` will preserve ``freq`` in simple cases (:issue:`32166`)
- Fixed bug in the repr of an object-dtype ``Index`` with bools and missing values (:issue:`32146`)
-

.. ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2235,7 +2235,7 @@ def maybe_convert_objects(ndarray[object] objects, bint try_float=0,
return uints
else:
return ints
elif seen.is_bool:
elif seen.is_bool and not seen.nan_:
return bools.view(np.bool_)

return objects
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ def test_maybe_convert_objects_nullable_integer(self, exp):

tm.assert_extension_array_equal(result, exp)

def test_maybe_convert_objects_bool_nan(self):
# GH32146
ind = pd.Index([True, False, np.nan], dtype=object)
exp = np.array([True, False, np.nan], dtype=object)
out = lib.maybe_convert_objects(ind.values, safe=1)
tm.assert_numpy_array_equal(out, exp)

def test_mixed_dtypes_remain_object_array(self):
# GH14956
array = np.array([datetime(2015, 1, 1, tzinfo=pytz.utc), 1], dtype=object)
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2697,6 +2697,17 @@ def test_intersect_str_dates(self):
expected = Index([], dtype=object)
tm.assert_index_equal(result, expected)

def test_index_repr_bool_nan(self):
# GH32146
arr = Index([True, False, np.nan], dtype=object)
exp1 = arr.format()
out1 = ["True", "False", "NaN"]
assert out1 == exp1

exp2 = repr(arr)
out2 = "Index([True, False, nan], dtype='object')"
assert out2 == exp2


class TestIndexUtils:
@pytest.mark.parametrize(
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/series/methods/test_value_counts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import pytest

import pandas as pd
from pandas import Categorical, CategoricalIndex, Series
Expand Down Expand Up @@ -177,3 +178,28 @@ def test_value_counts_categorical_with_nan(self):
exp = Series([2, 1, 3], index=CategoricalIndex(["a", "b", np.nan]))
res = ser.value_counts(dropna=False, sort=False)
tm.assert_series_equal(res, exp)

@pytest.mark.parametrize(
"ser, dropna, exp",
[
(
pd.Series([False, True, True, pd.NA]),
False,
pd.Series([2, 1, 1], index=[True, False, pd.NA]),
),
(
pd.Series([False, True, True, pd.NA]),
True,
pd.Series([2, 1], index=[True, False]),
),
(
pd.Series(range(3), index=[True, False, np.nan]).index,
False,
pd.Series([1, 1, 1], index=[True, False, pd.NA]),
),
],
)
def test_value_counts_bool_with_nan(self, ser, dropna, exp):
# GH32146
out = ser.value_counts(dropna=dropna)
tm.assert_series_equal(out, exp)