Skip to content

Commit dbb3876

Browse files
committed
fix up groupby ops
1 parent 6547498 commit dbb3876

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

pandas/core/groupby/groupby.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,15 @@ def f(self, **kwargs):
12811281
result = self.aggregate(
12821282
lambda x: npfunc(x, axis=self.axis))
12831283

1284-
result = self._try_cast(result, self.obj)
1284+
# coerce the columns if we can
1285+
if isinstance(result, DataFrame):
1286+
for col in result.columns:
1287+
result[col] = self._try_cast(
1288+
result[col], self.obj[col])
1289+
else:
1290+
result = self._try_cast(
1291+
result, self.obj)
1292+
12851293
if _convert:
12861294
result = result._convert(datetime=True)
12871295
return result

pandas/core/internals/blocks.py

+13
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,19 @@ def _slice(self, slicer):
17831783

17841784
return self.values[slicer]
17851785

1786+
def _try_cast_result(self, result, dtype=None):
1787+
"""
1788+
if we have an operation that operates on for example floats
1789+
we want to try to cast back to our EA here if possible
1790+
"""
1791+
try:
1792+
result = self._holder._from_sequence(
1793+
result.ravel(), dtype=dtype)
1794+
except Exception:
1795+
pass
1796+
1797+
return result
1798+
17861799
def formatting_values(self):
17871800
# Deprecating the ability to override _formatting_values.
17881801
# Do the warning here, it's only user in pandas, since we

pandas/core/nanops.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,12 @@ def _f(*args, **kwargs):
8989

9090
class bottleneck_switch(object):
9191

92-
def __init__(self, **kwargs):
92+
def __init__(self, name=None, **kwargs):
93+
self.name = name
9394
self.kwargs = kwargs
9495

9596
def __call__(self, alt):
96-
bn_name = alt.__name__
97+
bn_name = self.name or alt.__name__
9798

9899
try:
99100
bn_func = getattr(bn, bn_name)
@@ -725,7 +726,8 @@ def nansem(values, axis=None, skipna=True, ddof=1, mask=None):
725726

726727

727728
def _nanminmax(meth, fill_value_typ):
728-
@bottleneck_switch()
729+
730+
@bottleneck_switch(name='nan' + meth)
729731
def reduction(values, axis=None, skipna=True, mask=None):
730732

731733
values, mask, dtype, dtype_max, fill_value = _get_values(
@@ -745,7 +747,6 @@ def reduction(values, axis=None, skipna=True, mask=None):
745747
result = _wrap_results(result, dtype, fill_value)
746748
return _maybe_null_out(result, axis, mask)
747749

748-
reduction.__name__ = 'nan' + meth
749750
return reduction
750751

751752

pandas/tests/arrays/test_integer.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ def test_reduce_to_float(op):
696696

697697
expected = pd.DataFrame({
698698
"B": np.array([1.0, 3.0]),
699-
"C": integer_array([1, 3], dtype="Int64")
699+
"C": np.array([1.0, 3.0]),
700700
}, index=pd.Index(['a', 'b'], name='A'))
701701
tm.assert_frame_equal(result, expected)
702702

0 commit comments

Comments
 (0)