Skip to content

Commit fd0891e

Browse files
jbrockmendelproost
authored andcommitted
REF: dont _try_cast for user-defined functions (pandas-dev#29698)
1 parent 39192f9 commit fd0891e

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ Groupby/resample/rolling
511511
- Bug in :meth:`DataFrame.groupby` losing column name information when grouping by a categorical column (:issue:`28787`)
512512
- Bug in :meth:`DataFrameGroupBy.rolling().quantile()` ignoring ``interpolation`` keyword argument (:issue:`28779`)
513513
- Bug in :meth:`DataFrame.groupby` where ``any``, ``all``, ``nunique`` and transform functions would incorrectly handle duplicate column labels (:issue:`21668`)
514+
-
514515

515516
Reshaping
516517
^^^^^^^^^

pandas/core/apply.py

+2
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ def apply_raw(self):
226226
if "Function does not reduce" not in str(err):
227227
# catch only ValueError raised intentionally in libreduction
228228
raise
229+
# We expect np.apply_along_axis to give a two-dimensional result, or
230+
# also raise.
229231
result = np.apply_along_axis(self.f, self.axis, self.values)
230232

231233
# TODO: mixed type case

pandas/core/groupby/generic.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1109,12 +1109,12 @@ def _aggregate_frame(self, func, *args, **kwargs) -> DataFrame:
11091109
if axis != obj._info_axis_number:
11101110
for name, data in self:
11111111
fres = func(data, *args, **kwargs)
1112-
result[name] = self._try_cast(fres, data)
1112+
result[name] = fres
11131113
else:
11141114
for name in self.indices:
11151115
data = self.get_group(name, obj=obj)
11161116
fres = func(data, *args, **kwargs)
1117-
result[name] = self._try_cast(fres, data)
1117+
result[name] = fres
11181118

11191119
return self._wrap_frame_output(result, obj)
11201120

@@ -1424,6 +1424,8 @@ def _transform_fast(self, result: DataFrame, func_nm: str) -> DataFrame:
14241424
output = []
14251425
for i, _ in enumerate(result.columns):
14261426
res = algorithms.take_1d(result.iloc[:, i].values, ids)
1427+
# TODO: we have no test cases that get here with EA dtypes;
1428+
# try_cast may not be needed if EAs never get here
14271429
if cast:
14281430
res = self._try_cast(res, obj.iloc[:, i])
14291431
output.append(res)

0 commit comments

Comments
 (0)