Skip to content

Commit ce4ac9f

Browse files
committed
BUG: GH16875
Move guard from groupby to in core.dtypes.cast
1 parent 0843f9f commit ce4ac9f

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

pandas/core/dtypes/cast.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,9 @@ def trans(x): # noqa
110110
np.prod(result.shape)):
111111
return result
112112

113-
if issubclass(dtype.type, np.floating):
113+
# don't convert bool to float GH16875
114+
if issubclass(dtype.type, np.floating) and\
115+
not is_bool_dtype(result.dtype):
114116
return result.astype(dtype)
115117
elif is_bool_dtype(dtype) or is_integer_dtype(dtype):
116118

pandas/core/groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3055,7 +3055,7 @@ def transform(self, func, *args, **kwargs):
30553055
# we have a numeric dtype, as these are *always* udfs
30563056
# the cython take a different path (and casting)
30573057
dtype = self._selected_obj.dtype
3058-
if is_numeric_dtype(dtype) and not is_bool_dtype(result.dtype):
3058+
if is_numeric_dtype(dtype):
30593059
result = maybe_downcast_to_dtype(result, dtype)
30603060

30613061
result.name = self._selected_obj.name

pandas/tests/dtypes/test_cast.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from datetime import datetime, timedelta, date
1010
import numpy as np
1111

12-
from pandas import Timedelta, Timestamp, DatetimeIndex, DataFrame, NaT
12+
from pandas import Timedelta, Timestamp, DatetimeIndex, DataFrame, NaT, Series
1313

1414
from pandas.core.dtypes.cast import (
1515
maybe_downcast_to_dtype,
@@ -45,6 +45,12 @@ def test_downcast_conv(self):
4545
expected = np.array([8, 8, 8, 8, 9])
4646
assert (np.array_equal(result, expected))
4747

48+
# GH16875 coercing of bools
49+
ser = Series([True, True, False])
50+
result = maybe_downcast_to_dtype(ser, np.dtype(np.float64))
51+
expected = ser
52+
tm.assert_series_equal(result, expected)
53+
4854
# conversions
4955

5056
expected = np.array([1, 2])

0 commit comments

Comments
 (0)