Skip to content

BUG: Fixed bug, where pandas._libs.lib.maybe_convert_objects function improperly handled arrays with bools and NaNs #32242

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
Merged
Show file tree
Hide file tree
Changes from 6 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, where the ``Series`` representation of an object ``Index`` with bools and ``NaN`` values was wrong (:issue:`32146`)
-

.. ---------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ Performance improvements
Bug fixes
~~~~~~~~~

-

Categorical
^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,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
37 changes: 37 additions & 0 deletions pandas/tests/dtypes/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,43 @@ 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
arr = pd.Index([True, False, np.nan], dtype=object)
exp = np.array([True, False, np.nan], dtype=object)
out = lib.maybe_convert_objects(arr.values, safe=1)
tm.assert_numpy_array_equal(out, exp)

@pytest.mark.parametrize(
"arr, 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_maybe_convert_objects_value_counts(self, arr, dropna, exp):
out = arr.value_counts(dropna=dropna)
tm.assert_series_equal(out, exp)

def test_maybe_convert_objects_index_representation(self):
arr = pd.Index([True, False, np.nan], dtype=object)
exp = arr.format()
out = ["True", "False", "NaN"]
assert 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