Skip to content

Commit

Permalink
BUG: Fixed bug, where pandas._libs.lib.maybe_convert_objects function…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
AnnaDaglis committed Mar 9, 2020
1 parent 3e1275e commit 787dc8a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
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 @@ -2296,7 +2296,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 @@ -575,6 +575,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 @@ -2458,6 +2458,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)

0 comments on commit 787dc8a

Please sign in to comment.