Handler for dict-like argument. Ensures that necessary columns exist if obj is a DataFrame, and that a nested renamer is not passed. Also normalizes to all lists when values consists of a mix of list and non-lists.
(
self, how: str, obj: DataFrame | Series, func: AggFuncTypeDict
)
| 732 | return result |
| 733 | |
| 734 | def normalize_dictlike_arg( |
| 735 | self, how: str, obj: DataFrame | Series, func: AggFuncTypeDict |
| 736 | ) -> AggFuncTypeDict: |
| 737 | """ |
| 738 | Handler for dict-like argument. |
| 739 | |
| 740 | Ensures that necessary columns exist if obj is a DataFrame, and |
| 741 | that a nested renamer is not passed. Also normalizes to all lists |
| 742 | when values consists of a mix of list and non-lists. |
| 743 | """ |
| 744 | assert how in ("apply", "agg", "transform") |
| 745 | |
| 746 | # Can't use func.values(); wouldn't work for a Series |
| 747 | if ( |
| 748 | how == "agg" |
| 749 | and isinstance(obj, ABCSeries) |
| 750 | and any(is_list_like(v) for _, v in func.items()) |
| 751 | ) or (any(is_dict_like(v) for _, v in func.items())): |
| 752 | # GH 15931 - deprecation of renaming keys |
| 753 | raise SpecificationError("nested renamer is not supported") |
| 754 | |
| 755 | if obj.ndim != 1: |
| 756 | # Check for missing columns on a frame |
| 757 | from pandas import Index |
| 758 | |
| 759 | cols = Index(list(func.keys())).difference(obj.columns, sort=True) |
| 760 | if len(cols) > 0: |
| 761 | # GH 58474 |
| 762 | raise KeyError(f"Label(s) {list(cols)} do not exist") |
| 763 | |
| 764 | aggregator_types = (list, tuple, dict) |
| 765 | |
| 766 | # if we have a dict of any non-scalars |
| 767 | # eg. {'A' : ['mean']}, normalize all to |
| 768 | # be list-likes |
| 769 | # Cannot use func.values() because arg may be a Series |
| 770 | if any(isinstance(x, aggregator_types) for _, x in func.items()): |
| 771 | new_func: AggFuncTypeDict = {} |
| 772 | for k, v in func.items(): |
| 773 | if not isinstance(v, aggregator_types): |
| 774 | new_func[k] = [v] |
| 775 | else: |
| 776 | new_func[k] = v |
| 777 | func = new_func |
| 778 | return func |
| 779 | |
| 780 | def _apply_str(self, obj, func: str, *args, **kwargs): |
| 781 | """ |
no test coverage detected