3
���h1 � @ s� d Z ddlmZ ddlmZ ddlmZmZmZm Z m
Z
mZmZm
Z
mZ ddlmZmZ ddlmZmZ ddlmZ ddljjZdd lmZ dd
lmZmZ ee e
e ee ee
e! ee
e" f d�dd
�Z#e d�dd�Z$e%e
e%e
e! e
e" f d�dd�Z&ee
eef ee
eef d�dd�Z'ee ee d�dd�Z(eed�dd�Z)ee e!e
eee!f f e
e
e" e eef d�dd�Z*e%e
e
e! e
ee!ed ef f f d�d!d"�Z+dS )#z�
aggregation.py contains utility functions to handle multiple named and lambda
kwarg aggregations in groupby and DataFrame/Series aggregation
� )�defaultdict)�partial) �Any�Callable�DefaultDict�Dict�List�Optional�Sequence�Tuple�Union)�AggFuncType�Label)�is_dict_like�is_list_like)�SpecificationErrorN)�Index)�FrameOrSeriesUnion�Series)�func�returnc K st | dkot f |�}d}d}|sVt| t�rFt| �tt| ��krFtd��n| dkrVtd��|rht|�\} }}|| ||fS )a�
This is the internal function to reconstruct func given if there is relabeling
or not and also normalize the keyword to get new order of columns.
If named aggregation is applied, `func` will be None, and kwargs contains the
column and aggregation function information to be parsed;
If named aggregation is not applied, `func` is either string (e.g. 'min') or
Callable, or list of them (e.g. ['min', np.max]), or the dictionary of column name
and str/Callable/list of them (e.g. {'A': 'min'}, or {'A': [np.min, lambda x: x]})
If relabeling is True, will return relabeling, reconstructed func, column
names, and the reconstructed order of columns.
If relabeling is False, the columns and order will be None.
Parameters
----------
func: agg function (e.g. 'min' or Callable) or list of agg functions
(e.g. ['min', np.max]) or dictionary (e.g. {'A': ['min', np.max]}).
**kwargs: dict, kwargs used in is_multi_agg_with_relabel and
normalize_keyword_aggregation function for relabelling
Returns
-------
relabelling: bool, if there is relabelling or not
func: normalized and mangled func
columns: list of column names
order: list of columns indices
Examples
--------
>>> reconstruct_func(None, **{"foo": ("col", "min")})
(True, defaultdict(<class 'list'>, {'col': ['min']}), ('foo',), array([0]))
>>> reconstruct_func("min")
(False, 'min', None, None)
NzFFunction names must be unique if there is no new column names assignedz4Must provide 'func' or tuples of '(column, aggfunc).)�is_multi_agg_with_relabel�
isinstance�list�len�setr � TypeError�normalize_keyword_aggregation)r �kwargsZ
relabeling�columns�order� r! �9/tmp/pip-build-5_djhm0z/pandas/pandas/core/aggregation.py�reconstruct_func s )r# )r c K s" |