B
    W0df                 @  s  d dl mZ d dlmZmZmZmZmZ d dlZ	d dl
mZmZmZmZmZ d dlmZmZ d dlmZ d dlmZmZmZ d dlmZmZ d dlm  mZ d d	l m!Z! d d
l"m#Z# d dl$m%Z%m&Z&m'Z' d dl(m)Z) d dl*m+Z+ d dl,m-Z- erd dl.m/Z/ edee!d dddBddddddZ0dddddddddd d!Z1dCd"dd#d$d%Z2dDdd&d'd(Z3dEdd&d)d*Z4dFddd#d+d,Z5d-d. Z6edee!d/ dddGdd0d0d0dd1d2d/Z7dHdddd3d4d5Z8dIdd6d7d8Z9dJdd:d;d<Z:d=d=d>d?d@dAZ;dS )K    )annotations)TYPE_CHECKINGCallableHashableSequencecastN)AggFuncTypeAggFuncTypeBaseAggFuncTypeDictFrameOrSeriesUnion
IndexLabel)AppenderSubstitution)maybe_downcast_to_dtype)is_integer_dtypeis_list_like	is_scalar)ABCDataFrame	ABCSeries)_shared_docs)Grouper)Index
MultiIndexget_objs_combined_axis)concat)cartesian_product)Series)	DataFramez
data : DataFramepivot_table   )indentsmeanFTAllr   r   )dataaggfuncreturnc             C  s   t |}t |}t|trg }g }xF|D ]>}t| |||||||||	|
d}|| |t|d| q(W t||dd}|j| ddS t| |||||||||	|
}|j| ddS )N)
valuesindexcolumns
fill_valuer$   marginsdropnamargins_nameobservedsort__name__r   )keysaxisr   )method)_convert_by
isinstancelist__internal_pivot_tableappendgetattrr   Z__finalize__)r#   r&   r'   r(   r$   r)   r*   r+   r,   r-   r.   piecesr0   func_tabletable r=   K/var/www/html/venv/lib/python3.7/site-packages/pandas/core/reshape/pivot.pyr   6   sF    


z!AggFuncTypeBase | AggFuncTypeDictboolstr)r#   r$   r*   r+   r,   r-   r.   r%   c             C  s  || }|dk	}|rt |r*d}t|}n
d}|g}x|D ]}|| kr:t|q:W g }xL|| D ]@}t|trt|j}y|| kr|| W q` tk
r   Y q`X q`W t|t| j	k r| | } nF| j	}x6|D ].}y|
|}W q tttfk
r   Y qX qW t|}| j||	|
d}||}|rt|trt|j	r|jdd}xt|D ]l}|| krPt| | rP||krPt|| sPt|| tr|| ||< nt|| | | j||< qPW |}|jjdkrN|rN|jjdt| }g }xRtt|t|D ]<}|jj| }|dks(||kr4|| n
|| qW ||}|st|jtrtjt|jj|jjd}|j|d	d
}t|j	trtjt|j	j|j	jd}|j|dd
}t|tr|jdd
}|dk	r|j|dd}|dk	st|}|r<|r"| |   j!dd
 } t"|| |||||||d	}|rd|sd|j	jdkrd|j#d	dd
}t|d	krt|d	kr|j$}t|tr|r|jddd}|S )zL
    Helper of :func:`pandas.pivot_table` for any non-list ``aggfunc``.
    NTF)r-   r.   all)howr   )namesr   )r1   Zinfer)Zdowncast)rowscolsr$   r-   r,   r)   )rB   r1   )%r   r5   KeyErrorr4   r   keyr7   	TypeErrorlenr(   Zdrop
ValueErrorgroupbyaggr   r+   r   r   dtyper'   ZnlevelsrC   rangeunstackr   from_arraysr   ZlevelsreindexZ
sort_indexfillnaAssertionErrorZnotnarA   _add_marginsZ	droplevelT)r#   r&   r'   r(   r$   r)   r*   r+   r,   r-   r.   r0   Zvalues_passedZvalues_multiiZ	to_filterxrG   groupedZaggedvr<   Zindex_namesZ
to_unstacknamemr;   r=   r=   r>   r6   o   s    












r6   r   )r<   r,   c	          	   C  s  t |tstdd| d}	x(| jjD ]}
|| j|
kr(t|	q(W t||||}| jdkrx0| jjdd  D ]}
|| j|
krrt|	qrW t	|dkr|fdt	|d   }n|}|st | t
r| t||| iS |rt| |||||||}t |ts
|S |\}}}n>t | ts&tt| ||||||}t |tsJ|S |\}}}|j|j|d}x8|D ]0}t |tr|| ||< n||d  ||< qjW dd	lm} |||gd
j}|jj}x:t|jD ],}||gj}|| jt|fd||< qW ||}||j_|S )Nz&margins_name argument must be a stringzConflicting name "z" in margins   r   ) )r)   r   )r   )r(   )args)r4   r@   rJ   r'   rC   Zget_level_values_compute_grand_marginndimr(   rI   r   r7   r   _generate_marginal_resultstupler   rS   )_generate_marginal_results_without_valuesrQ   pandasr   rU   setZdtypesZselect_dtypesapplyr   )r<   r#   r&   rD   rE   r$   r-   r,   r)   msglevelgrand_marginrG   Zmarginal_result_setresultmargin_keys
row_marginkr   Zmargin_dummyZ	row_namesrM   r=   r=   r>   rT      sV    




rT   )r,   c          	   C  s   |ri }x| |   D ]\}}ylt|tr<t|| ||< nLt|tr|t|| trjt|||  ||< q|| |||< n||||< W q tk
r   Y qX qW |S ||| jiS d S )N)itemsr4   r@   r8   dictrH   r'   )r#   r&   r$   r,   ri   rm   rY   r=   r=   r>   r_   P  s    


r_   c               s  t  dkrJg }g }	 fdd}
t |dkr|||  j||d|}d}x| jd||dD ]8\}}|
|}| }|| ||< || |	| qbW nddlm} d}xx| jd||dD ]d\}}t  dkr|
|}n}|| |||j}t	|g|j
jd|_
|| |	| qW t||d	}t |dkrT|S n
| }| j}	t  dkr| |  j |d|}| }t  gttt   }|j
||_
nttj|jd
}||	|fS )Nr   c               s   | fdt  d   S )N)r]   r   )rI   )rG   )rE   r,   r=   r>   _all_keyn  s    z,_generate_marginal_results.<locals>._all_key)r-   r   )rh   r1   r-   )r   )rZ   )r1   )r'   )rI   rK   rL   copyr7   rd   r   rf   rU   r   r'   rZ   r   r(   stackr5   rN   Zreorder_levelsr   npnan)r<   r#   r&   rD   rE   r$   r-   r,   Ztable_piecesrk   rp   marginZcat_axisrG   Zpieceall_keyr   Ztransformed_piecerj   rl   Z	new_orderr=   )rE   r,   r>   ra   f  sH    



ra   c               s   t  dkrg } fdd}t |dkr`|| j||d|}	| }
|	| |
< | }||
 q|jdd|d|}	| }
|	| |
< | }||
 |S n
| }| j}t  r|  j |d|}nttj|jd}|||fS )Nr   c                 s&   t  dkrS fdt  d   S )Nr   )r]   )rI   r=   )rE   r,   r=   r>   rp     s    z;_generate_marginal_results_without_values.<locals>._all_key)r-   )rh   r1   r-   )r'   )rI   rK   rf   r7   r(   r   rs   rt   )r<   r#   rD   rE   r$   r-   r,   rk   rp   ru   rv   rj   rl   r=   )rE   r,   r>   rc     s*    
rc   c             C  sF   | d krg } n4t | s2t| tjtttfs2t| r:| g} nt| } | S )N)	r   r4   rs   Zndarrayr   r   r   callabler5   )Zbyr=   r=   r>   r3     s    r3   pivotzIndexLabel | None)r#   r'   r(   r&   r%   c               s  |d krt dt|}|d krV|d k	r6t|}ng }|d k} j|| |d}n|d krtt j jjdg}n fddt|D } fdd|D }	||	 t	|}
t
|rt|tsttt |} j | j|
|d}n j | j|
d}||S )	Nz.pivot() missing 1 required argument: 'columns')r7   )rZ   c               s   g | ]} | qS r=   r=   ).0idx)r#   r=   r>   
<listcomp>  s    zpivot.<locals>.<listcomp>c               s   g | ]} | qS r=   r=   )ry   col)r#   r=   r>   r{     s    )r'   r(   )r'   )rH   comZconvert_to_list_likeZ	set_indexr   r'   rZ   extendr   rP   r   r4   rb   r   r   r   Z_constructorZ_valuesZ_constructor_slicedrO   )r#   r'   r(   r&   Zcolumns_listlikerE   r7   ZindexedZ
index_listZdata_columnsZ
multiindexr=   )r#   r>   rx     s,    


)r,   r+   r%   c
             C  sP  |dkr|dk	rt d|dk	r0|dkr0t dt| } t|}d}
dd | | D }|rlt|ddd}
t| |d	d
}t||dd
}t||\}}}}ddlm} tt	|| tt	||}|||
d}|dkrd|d< t
dd}n||d< d|i}|jd|||||d|}|	dk	r0t||	||d}|j|dd}|j|dd}|S )ac  
    Compute a simple cross tabulation of two (or more) factors. By default
    computes a frequency table of the factors unless an array of values and an
    aggregation function are passed.

    Parameters
    ----------
    index : array-like, Series, or list of arrays/Series
        Values to group by in the rows.
    columns : array-like, Series, or list of arrays/Series
        Values to group by in the columns.
    values : array-like, optional
        Array of values to aggregate according to the factors.
        Requires `aggfunc` be specified.
    rownames : sequence, default None
        If passed, must match number of row arrays passed.
    colnames : sequence, default None
        If passed, must match number of column arrays passed.
    aggfunc : function, optional
        If specified, requires `values` be specified as well.
    margins : bool, default False
        Add row/column margins (subtotals).
    margins_name : str, default 'All'
        Name of the row/column that will contain the totals
        when margins is True.
    dropna : bool, default True
        Do not include columns whose entries are all NaN.
    normalize : bool, {'all', 'index', 'columns'}, or {0,1}, default False
        Normalize by dividing all values by the sum of values.

        - If passed 'all' or `True`, will normalize over all values.
        - If passed 'index' will normalize over each row.
        - If passed 'columns' will normalize over each column.
        - If margins is `True`, will also normalize margin values.

    Returns
    -------
    DataFrame
        Cross tabulation of the data.

    See Also
    --------
    DataFrame.pivot : Reshape data based on column values.
    pivot_table : Create a pivot table as a DataFrame.

    Notes
    -----
    Any Series passed will have their name attributes used unless row or column
    names for the cross-tabulation are specified.

    Any input passed containing Categorical data will have **all** of its
    categories included in the cross-tabulation, even if the actual data does
    not contain any instances of a particular category.

    In the event that there aren't overlapping indexes an empty DataFrame will
    be returned.

    Examples
    --------
    >>> a = np.array(["foo", "foo", "foo", "foo", "bar", "bar",
    ...               "bar", "bar", "foo", "foo", "foo"], dtype=object)
    >>> b = np.array(["one", "one", "one", "two", "one", "one",
    ...               "one", "two", "two", "two", "one"], dtype=object)
    >>> c = np.array(["dull", "dull", "shiny", "dull", "dull", "shiny",
    ...               "shiny", "dull", "shiny", "shiny", "shiny"],
    ...              dtype=object)
    >>> pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c'])
    b   one        two
    c   dull shiny dull shiny
    a
    bar    1     2    1     0
    foo    2     2    1     2

    Here 'c' and 'f' are not represented in the data and will not be
    shown in the output because dropna is True by default. Set
    dropna=False to preserve categories with no data.

    >>> foo = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'])
    >>> bar = pd.Categorical(['d', 'e'], categories=['d', 'e', 'f'])
    >>> pd.crosstab(foo, bar)
    col_0  d  e
    row_0
    a      1  0
    b      0  1
    >>> pd.crosstab(foo, bar, dropna=False)
    col_0  d  e  f
    row_0
    a      1  0  0
    b      0  1  0
    c      0  0  0
    Nz&aggfunc cannot be used without values.z)values cannot be used without an aggfunc.c             S  s   g | ]}t |ttfr|qS r=   )r4   r   r   )ry   rW   r=   r=   r>   r{   y  s    zcrosstab.<locals>.<listcomp>TF)Z	intersectr.   row)prefixr|   r   )r   )r'   	__dummy__)r$   r)   r$   )r'   r(   r*   r,   r+   )	normalizer*   r,   )r'   r1   r   )r(   r1   )r   )rJ   r}   Zmaybe_make_listr   
_get_names_build_names_mapperrd   r   ro   ziprI   r   
_normalizeZrename_axis)r'   r(   r&   rownamescolnamesr$   r*   r,   r+   r   Z
common_idxZ	pass_objsrownames_mapperunique_rownamescolnames_mapperunique_colnamesr   r#   Zdfkwargsr<   r=   r=   r>   crosstab  sH    g


r   )r*   c          
   C  sH  t |ttfsRddd}y|| }W n, tk
rP } ztd|W d d }~X Y nX |dkrdd dd d	d d
}|d |d< y|| }W n, tk
r } ztd|W d d }~X Y nX || } | d} nv|dkr<| j}| j}	| jdd d f j	}
||
k||
k@ rt| d| jd ddf }| jdd df }| jd dd df } t
| |dd} |dkr||  }t| |gdd} | d} |	| _n|dkr||  }| |} | d} || _np|dks|dkr2||  }||  }d|j|< t| |gdd} | |} | d} || _|	| _ntdntd| S )Nr'   r(   )r   r   zNot a valid normalize argumentFc             S  s   | | j ddj dd S )Nr   )r1   r   )sum)rW   r=   r=   r>   <lambda>      z_normalize.<locals>.<lambda>c             S  s   | |    S )N)r   )rW   r=   r=   r>   r     r   c             S  s   | j | jddddS )Nr   )r1   r   )divr   )rW   r=   r=   r>   r     r   )rA   r(   r'   rA   Tr   z not in pivoted DataFrame)r   r*   r   )r1   zNot a valid margins argument)r4   r?   r@   rF   rJ   rR   r'   r(   ZilocrZ   r   r   r   r7   loc)r<   r   r*   r,   Z	axis_subserrZnormalizersfZtable_indextable_columnsZlast_ind_or_colZcolumn_marginZindex_marginr=   r=   r>   r     sb    










r   r   )r   c             C  s   |d krZg }xvt | D ]>\}}t|tr@|jd k	r@||j q|| d|  qW n*t|t| krrtdt|tst|}|S )N_z*arrays and names must have the same length)	enumerater4   r   rZ   r7   rI   rS   r5   )ZarrsrC   r   rV   Zarrr=   r=   r>   r     s    
r   z	list[str]z;tuple[dict[str, str], list[str], dict[str, str], list[str]])r   r   r%   c               s   dd }t | t |}|| ||B |B   fddt| D } fddt| D } fddt|D } fddt|D }||||fS )	a  
    Given the names of a DataFrame's rows and columns, returns a set of unique row
    and column names and mappers that convert to original names.

    A row or column name is replaced if it is duplicate among the rows of the inputs,
    among the columns of the inputs or between the rows and the columns.

    Parameters
    ----------
    rownames: list[str]
    colnames: list[str]

    Returns
    -------
    Tuple(Dict[str, str], List[str], Dict[str, str], List[str])

    rownames_mapper: dict[str, str]
        a dictionary with new row names as keys and original rownames as values
    unique_rownames: list[str]
        a list of rownames with duplicate names replaced by dummy names
    colnames_mapper: dict[str, str]
        a dictionary with new column names as keys and original column names as values
    unique_colnames: list[str]
        a list of column names with duplicate names replaced by dummy names

    c               s   t    fdd| D S )Nc               s   h | ]}| kr|qS r=   r=   )ry   rZ   )seenr=   r>   	<setcomp>.  s    z>_build_names_mapper.<locals>.get_duplicates.<locals>.<setcomp>)re   )rC   r=   )r   r>   get_duplicates,  s    z+_build_names_mapper.<locals>.get_duplicatesc               s$   i | ]\}}| kr|d | qS )row_r=   )ry   rV   rZ   )	dup_namesr=   r>   
<dictcomp>3  s   z'_build_names_mapper.<locals>.<dictcomp>c               s&   g | ]\}}| krd | n|qS )r   r=   )ry   rV   rZ   )r   r=   r>   r{   7  s    z'_build_names_mapper.<locals>.<listcomp>c               s$   i | ]\}}| kr|d | qS )col_r=   )ry   rV   rZ   )r   r=   r>   r   :  s   c               s&   g | ]\}}| krd | n|qS )r   r=   )ry   rV   rZ   )r   r=   r>   r{   >  s    )re   intersectionr   )r   r   r   Zshared_namesr   r   r   r   r=   )r   r>   r     s    

r   )
NNNr!   NFTr"   FT)Nr"   N)r"   )r"   )r"   )NNN)NNNNFr"   TF)r"   )r   )<
__future__r   typingr   r   r   r   r   numpyrs   Zpandas._typingr   r	   r
   r   r   Zpandas.util._decoratorsr   r   Zpandas.core.dtypes.castr   Zpandas.core.dtypes.commonr   r   r   Zpandas.core.dtypes.genericr   r   Zpandas.core.commoncorecommonr}   Zpandas.core.framer   Zpandas.core.groupbyr   Zpandas.core.indexes.apir   r   r   Zpandas.core.reshape.concatr   Zpandas.core.reshape.utilr   Zpandas.core.seriesr   rd   r   r   r6   rT   r_   ra   rc   r3   rx   r   r   r   r   r=   r=   r=   r>   <module>   sl            ,   G@%  +        P