|
39 | 39 | maybe_cast_result_dtype, |
40 | 40 | maybe_convert_objects, |
41 | 41 | maybe_downcast_numeric, |
42 | | - maybe_downcast_to_dtype, |
43 | 42 | ) |
44 | 43 | from pandas.core.dtypes.common import ( |
45 | 44 | ensure_int64, |
@@ -535,26 +534,25 @@ def _transform_general( |
535 | 534 | if isinstance(res, (ABCDataFrame, ABCSeries)): |
536 | 535 | res = res._values |
537 | 536 |
|
538 | | - indexer = self._get_index(name) |
539 | | - ser = klass(res, indexer) |
540 | | - results.append(ser) |
| 537 | + results.append(klass(res, index=group.index)) |
541 | 538 |
|
542 | 539 | # check for empty "results" to avoid concat ValueError |
543 | 540 | if results: |
544 | 541 | from pandas.core.reshape.concat import concat |
545 | 542 |
|
546 | | - result = concat(results).sort_index() |
| 543 | + concatenated = concat(results) |
| 544 | + result = self._set_result_index_ordered(concatenated) |
547 | 545 | else: |
548 | 546 | result = self.obj._constructor(dtype=np.float64) |
549 | | - |
550 | 547 | # we will only try to coerce the result type if |
551 | 548 | # we have a numeric dtype, as these are *always* user-defined funcs |
552 | 549 | # the cython take a different path (and casting) |
553 | | - # make sure we don't accidentally upcast (GH35014) |
554 | | - types = ["bool", "int32", "int64", "float32", "float64"] |
555 | | - dtype = self._selected_obj.dtype |
556 | | - if is_numeric_dtype(dtype) and types.index(dtype) < types.index(result.dtype): |
557 | | - result = maybe_downcast_to_dtype(result, dtype) |
| 550 | + if is_numeric_dtype(result.dtype): |
| 551 | + common_dtype = np.find_common_type( |
| 552 | + [self._selected_obj.dtype, result.dtype], [] |
| 553 | + ) |
| 554 | + if common_dtype is result.dtype: |
| 555 | + result = maybe_downcast_numeric(result, self._selected_obj.dtype) |
558 | 556 |
|
559 | 557 | result.name = self._selected_obj.name |
560 | 558 | result.index = self._selected_obj.index |
|
0 commit comments