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 10 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
2 changes: 1 addition & 1 deletion pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2298,7 +2298,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
9 changes: 9 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2458,6 +2458,15 @@ def test_intersect_str_dates(self):
expected = Index([], dtype=object)
tm.assert_index_equal(result, expected)

def test_index_repr_bool_nan(self):
# Tests for the Index representation,
# specifically for the case that includes bools and NANs
# GH32146
arr = Index([True, False, np.nan], dtype=object)
exp = arr.format()
out = ["True", "False", "NaN"]
assert out == exp


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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may have misunderstood the original issue, but my understanding is that the values were correct, just the repr was wrong. If so, why are we adding new tests here, which I think don't cover the reported issue?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see it was requested by @jreback in https://github.com/pandas-dev/pandas/pull/32242/files#r385098106. I suppose no harm in more tests, but don't we think this is already tested?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TomAugspurger Yes, I agree that this has been tested. Was following the suggestion of @jreback.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback can you comment on the request / need for these tests?

# GH32146
out = ser.value_counts(dropna=dropna)
tm.assert_series_equal(out, exp)