|
31 | 31 |
|
32 | 32 | from pandas.core.dtypes.cast import is_nested_object |
33 | 33 | from pandas.core.dtypes.common import is_dict_like, is_list_like |
34 | | -from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries |
| 34 | +from pandas.core.dtypes.generic import ABCDataFrame, ABCNDFrame, ABCSeries |
35 | 35 |
|
36 | 36 | from pandas.core.base import DataError, SpecificationError |
37 | 37 | import pandas.core.common as com |
@@ -621,58 +621,27 @@ def aggregate(obj, arg: AggFuncType, *args, **kwargs): |
621 | 621 | # set the final keys |
622 | 622 | keys = list(arg.keys()) |
623 | 623 |
|
624 | | - # combine results |
625 | | - |
626 | | - def is_any_series() -> bool: |
627 | | - # return a boolean if we have *any* nested series |
628 | | - return any(isinstance(r, ABCSeries) for r in results.values()) |
629 | | - |
630 | | - def is_any_frame() -> bool: |
631 | | - # return a boolean if we have *any* nested series |
632 | | - return any(isinstance(r, ABCDataFrame) for r in results.values()) |
633 | | - |
634 | | - if isinstance(results, list): |
635 | | - return concat(results, keys=keys, axis=1, sort=True), True |
636 | | - |
637 | | - elif is_any_frame(): |
638 | | - # we have a dict of DataFrames |
639 | | - # return a MI DataFrame |
| 624 | + # Avoid making two isinstance calls in all and any below |
| 625 | + is_ndframe = [isinstance(r, ABCNDFrame) for r in results.values()] |
640 | 626 |
|
| 627 | + # combine results |
| 628 | + if all(is_ndframe): |
641 | 629 | keys_to_use = [k for k in keys if not results[k].empty] |
642 | 630 | # Have to check, if at least one DataFrame is not empty. |
643 | 631 | keys_to_use = keys_to_use if keys_to_use != [] else keys |
644 | | - return ( |
645 | | - concat([results[k] for k in keys_to_use], keys=keys_to_use, axis=1), |
646 | | - True, |
| 632 | + axis = 0 if isinstance(obj, ABCSeries) else 1 |
| 633 | + result = concat({k: results[k] for k in keys_to_use}, axis=axis) |
| 634 | + elif any(is_ndframe): |
| 635 | + # There is a mix of NDFrames and scalars |
| 636 | + raise ValueError( |
| 637 | + "cannot perform both aggregation " |
| 638 | + "and transformation operations " |
| 639 | + "simultaneously" |
647 | 640 | ) |
| 641 | + else: |
| 642 | + from pandas import Series |
648 | 643 |
|
649 | | - elif isinstance(obj, ABCSeries) and is_any_series(): |
650 | | - |
651 | | - # we have a dict of Series |
652 | | - # return a MI Series |
653 | | - try: |
654 | | - result = concat(results) |
655 | | - except TypeError as err: |
656 | | - # we want to give a nice error here if |
657 | | - # we have non-same sized objects, so |
658 | | - # we don't automatically broadcast |
659 | | - |
660 | | - raise ValueError( |
661 | | - "cannot perform both aggregation " |
662 | | - "and transformation operations " |
663 | | - "simultaneously" |
664 | | - ) from err |
665 | | - |
666 | | - return result, True |
667 | | - |
668 | | - # fall thru |
669 | | - from pandas import DataFrame, Series |
670 | | - |
671 | | - try: |
672 | | - result = DataFrame(results) |
673 | | - except ValueError: |
674 | 644 | # we have a dict of scalars |
675 | | - |
676 | 645 | # GH 36212 use name only if obj is a series |
677 | 646 | if obj.ndim == 1: |
678 | 647 | obj = cast("Series", obj) |
|
0 commit comments