a
    DBìjõp  ã                   @  s„  d dl mZ d dlmZmZ d dlmZmZmZm	Z	 d dl
Z
d dlZd dlmZ d dlmZ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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/m0Z0 e�r4d dl1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7 edƒee$d dd�ddddddddej8df
dddddd ddd!œd"d„ƒƒZ9dd#dddd ddd!œd$d%„Z:dKd&dddd'œd(d)„Z;dLddd*œd+d,„Z<dMdddd-œd.d/„Z=dNddddd'œd0d1„Z>d2d3„ Z?edƒee$d4 dd�ej8ej8d5œdd6d7d7dd8œd9d4„ƒƒZ@dOdddd:dd;œd<d=„ZAdPddddd>œd?d@„ZBdQdBdCœdDdE„ZCdFdFdGdHœdIdJ„ZDdS )Ré    )Úannotations)ÚHashableÚSequence)ÚTYPE_CHECKINGÚCallableÚLiteralÚcastN)Úlib)ÚAppenderÚSubstitution)Úfind_stack_level)Úmaybe_downcast_to_dtype)Úis_list_likeÚis_nested_list_likeÚ	is_scalar)ÚExtensionDtype)ÚABCDataFrameÚ	ABCSeries)Ú_shared_docs)ÚGrouper)ÚIndexÚ
MultiIndexÚget_objs_combined_axis)Úconcat)Úcartesian_product)ÚSeries)ÚAggFuncTypeÚAggFuncTypeBaseÚAggFuncTypeDictÚ
IndexLabel©Ú	DataFramez
data : DataFrameÚpivot_tableé   )ÚindentsÚmeanFTÚAllr!   r   Úboolr   zbool | lib.NoDefault)ÚdataÚaggfuncÚmarginsÚdropnaÚmargins_nameÚobservedÚsortÚreturnc                 C  s¬   t |ƒ}t |ƒ}t|tƒr‚g }g }|D ]>}t| |||||||||	|
d�}| |¡ | t|d|ƒ¡ q&t||dd�}|j| dd�S t| |||||||||	|
ƒ}|j| dd�S )N)
ÚvaluesÚindexÚcolumnsÚ
fill_valuer)   r*   r+   r,   r-   r.   Ú__name__r#   )ÚkeysÚaxisr"   )Úmethod)Ú_convert_byÚ
isinstanceÚlistÚ__internal_pivot_tableÚappendÚgetattrr   Z__finalize__)r(   r0   r1   r2   r)   r3   r*   r+   r,   r-   r.   Úpiecesr5   ÚfuncÚ_tableÚtable© rB   úX/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/reshape/pivot.pyr"   =   sJ    
õ
õz!AggFuncTypeBase | AggFuncTypeDictc                 C  s�  || }|du}|r¶t |ƒr*d}t|ƒ}n
d}|g}|D ]}|| vr8t|ƒ‚q8g }|| D ]>}t|tƒrn|j}z|| v r‚| |¡ W qZ ty–   Y qZ0 qZt|ƒt| j	ƒk rö| | } n@| j	}|D ],}z| 
|¡}W qÀ tttfyê   Y qÀ0 qÀt|ƒ}|	tju �rdn|	}| j|||
|d�}|	tju �rRtdd„ |jjD ƒƒ�rRtjdttƒ d� | |¡}|�r†t|tƒ�r†t|j	ƒ�r†|jd	d
�}|}|jjdk�r|�r|jjdt|ƒ… }g }tt|ƒt|ƒƒD ]<}|jj| }|du �sì||v �rø| |¡ n
| |¡ �qÈ|j||d�}|�sŠt|jtƒ�rRtjt |jj!ƒ|jjd�}|j"|d|d�}t|j	tƒ�rŠtjt |j	j!ƒ|j	jd�}|j"|d|d�}|
du �r¬t|tƒ�r¬|j#dd�}|du�rè| $|¡}|tu �rè|	�sèt %|¡�rè| &t'j(¡}|�r"|�r| |  )¡ j*dd� } t+|| |||||||d�	}|�rJ|�sJ|j	jdk�rJ|j	 ,d¡|_	t|ƒdk�rlt|ƒdk�rl|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.   r+   c                 s  s   | ]}|j V  qd S ©N)Z_passed_categorical)Ú.0ZpingrB   rB   rC   Ú	<genexpr>­   s   z)__internal_pivot_table.<locals>.<genexpr>zÀThe default value of observed=False is deprecated and will change to observed=True in a future version of pandas. Specify observed=False to silence this warning and retain the current behavior)ÚcategoryÚ
stacklevelÚall)Úhowr#   ©r3   ©Únamesr   )r6   r3   ©r6   )ÚrowsÚcolsr)   r-   r,   r3   )rJ   r6   ).r   r:   ÚKeyErrorr9   r   Úkeyr<   Ú	TypeErrorÚlenr2   ZdropÚ
ValueErrorr	   Ú
no_defaultÚgroupbyÚanyÚ_grouperZ	groupingsÚwarningsÚwarnÚFutureWarningr   Úaggr   r+   r1   ÚnlevelsrM   ÚrangeÚunstackr   Úfrom_arraysr   ZlevelsÚreindexZ
sort_indexÚfillnaÚ
is_integerZastypeÚnpZint64ZnotnarI   Ú_add_marginsZ	droplevelÚT)r(   r0   r1   r2   r)   r3   r*   r+   r,   r-   r.   r5   Zvalues_passedZvalues_multiÚiZ	to_filterÚxrR   Zobserved_boolÚgroupedZaggedrA   Zindex_namesZ
to_unstackÚnameÚmrB   rB   rC   r;   v   s´    



ÿ
û
ÿÿ

÷r;   zDataFrame | Series)rA   r(   r-   r,   c	              	   C  s  t |tƒstdƒ‚d|› d�}	| jjD ]}
|| j |
¡v r&t|	ƒ‚q&t||||ƒ}| jdkrŠ| jjdd … D ]}
|| j |
¡v rlt|	ƒ‚qlt	|ƒdkr®|fdt	|ƒd   }n|}|sØt | t
ƒrØ|  |  ||| i¡¡S |�rt| |||||||ƒ}t |tƒ�s|S |\}}}n>t | tƒ�s J ‚t| ||||||ƒ}t |tƒ�sD|S |\}}}|j|j|d�}|D ]0}t |tƒ�r€|| ||< n||d  ||< �qbdd	lm} ||t|gƒd
�j}|jj}t|jƒD ]<}t |tƒ�rÚ�qÆ| |g¡j}|| jt|fd�||< �qÆ| |¡}||j_|S )Nz&margins_name argument must be a stringzConflicting name "z" in marginsé   r#   ©Ú rK   r   r    )r2   )Úargs)r9   ÚstrrU   r1   rM   Úget_level_valuesÚ_compute_grand_marginÚndimr2   rT   r   Ú_appendÚ_constructorÚ_generate_marginal_resultsÚtupler   Ú)_generate_marginal_results_without_valuesrb   Úpandasr!   r   rg   ÚsetZdtypesr   Zselect_dtypesÚapplyr   )rA   r(   r0   rO   rP   r)   r-   r,   r3   ÚmsgÚlevelÚgrand_marginrR   Zmarginal_result_setÚresultÚmargin_keysÚ
row_marginÚkr!   Zmargin_dummyZ	row_namesZdtyperB   rB   rC   rf      s`    



ÿÿ
ÿ
rf   )r(   r,   c              	   C  s´   |r¢i }| |   ¡ D ]ˆ\}}zlt|tƒr:t||ƒƒ ||< nLt|tƒrzt|| tƒrht||| ƒƒ ||< q†|| |ƒ||< n||ƒ||< W q tyš   Y q0 q|S ||| jƒiS d S rD   )Úitemsr9   rq   r=   ÚdictrS   r1   )r(   r0   r)   r,   r   rƒ   ÚvrB   rB   rC   rs   U  s    

rs   )r(   r-   r,   c                   s  t ˆ ƒdk�r|g }g }	‡ ‡fdd„}
t |ƒdkr¢|||  j||d� |¡}d}| jjd|d�D ]>\}}|j}|
|ƒ}| ¡ }|| ||< | |¡ |	 |¡ q`n°ddlm} d}| jd|d�D ]�\}}t ˆ ƒdkrÞ|
|ƒ}nˆ}| |¡ || |¡ƒj}t	|j
tƒ�r(tj|g|j
jd g d�|_
nt|g|j
jd	�|_
| |¡ |	 |¡ qÀ|�s\| S t||d
�}t |ƒdk�r†|S n
| }| j}	t ˆ ƒdk�rø|ˆ |  jˆ |d� |¡‰ˆjdd�‰t ˆ ƒgttt ˆ ƒƒƒ }‡fdd„|D ƒ}ˆj
 |¡ˆ_
n|jtj|jd�‰||	ˆfS )Nr   c                   s   | ˆfdt ˆ ƒd   S )Nrn   r#   ©rT   )rR   ©rP   r,   rB   rC   Ú_all_key|  s    z,_generate_marginal_results.<locals>._all_key©r-   r#   )r~   r-   r    rL   ©rk   rN   T)Zfuture_stackc                   s   g | ]}ˆ j j| ‘qS rB   )r1   rM   ©rE   rh   )r‚   rB   rC   Ú
<listcomp>¸  ó    z._generate_marginal_results.<locals>.<listcomp>©r1   )rT   rW   r]   rg   Úcopyr<   rz   r!   r|   r9   r1   r   Úfrom_tuplesrM   r   rk   r   r2   Ústackr:   r_   Zreorder_levelsÚ_constructor_slicedre   Únan)rA   r(   r0   rO   rP   r)   r-   r,   Ztable_piecesr�   r‰   ÚmarginZcat_axisrR   ZpieceÚall_keyr!   Ztransformed_piecer€   Znew_order_indicesZnew_order_namesrB   )rP   r,   r‚   rC   rw   l  sX    


ÿ

rw   c                   sÞ   t ˆ ƒdkr˜g }‡ ‡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#   rn   r‡   rB   rˆ   rB   rC   r‰   Î  s    z;_generate_marginal_results_without_values.<locals>._all_keyrŠ   )r~   r6   r-   r�   )rT   rW   r|   r<   r2   r   re   r”   )rA   r(   rO   rP   r)   r-   r,   r�   r‰   r•   r–   r€   r‚   rB   rˆ   rC   ry   À  s*    

ry   c                 C  sF   | d u rg } n4t | ƒs2t| tjtttfƒs2t| ƒr:| g} nt| ƒ} | S rD   )	r   r9   re   Zndarrayr   r   r   Úcallabler:   )ZbyrB   rB   rC   r8   í  s    ÿþýr8   Úpivot)r1   r0   r   zIndexLabel | lib.NoDefault)r(   r2   r1   r0   r/   c                  sv  t  |¡}ˆ jdd�‰ ˆ j ¡ ˆ _dd„ ˆ jjD ƒˆ j_|tju rz|tjurXt  |¡}ng }|tju }ˆ j|| |d�}nØ|tju rÄtˆ jt	ƒr¬‡ fdd„t
ˆ jjƒD ƒ}qÜˆ jˆ jˆ jjd�g}n‡ fdd„t  |¡D ƒ}‡ fd	d„|D ƒ}	| |	¡ t	 |¡}
t|ƒ�r>t|tƒ�s>ttt |ƒ}ˆ jˆ | j|
|d
�}nˆ jˆ | j|
d�}| |¡}dd„ |jjD ƒ|j_|S )NF)Údeepc                 S  s   g | ]}|d ur|nt j‘qS rD   ©r	   rV   ©rE   rk   rB   rB   rC   r�     s   zpivot.<locals>.<listcomp>)r<   c                   s   g | ]}ˆ j  |¡‘qS rB   )r1   rr   rŒ   ©r(   rB   rC   r�   !  s   r‹   c                   s   g | ]}ˆ | ‘qS rB   rB   )rE   Úidxrœ   rB   rC   r�   )  rŽ   c                   s   g | ]}ˆ | ‘qS rB   rB   )rE   Úcolrœ   rB   rC   r�   +  rŽ   )r1   r2   r�   c                 S  s   g | ]}|t jur|nd ‘qS rD   rš   r›   rB   rB   rC   r�   ;  s   )ÚcomZconvert_to_list_liker�   r1   rM   r	   rV   Z	set_indexr9   r   r_   r^   r“   rk   Úextendra   r   rx   r   r   r   rv   Z_valuesr`   )r(   r2   r1   r0   Zcolumns_listlikerP   r<   ZindexedZ
index_listZdata_columnsZ
multiindexr€   rB   rœ   rC   r˜   û  sH    	
ÿ



ÿ


ÿÿ

ÿ
ÿ
z/bool | Literal[0, 1, 'all', 'index', 'columns'])r*   r,   r+   Ú	normalizer/   c
              	   C  s^  |du r|durt dƒ‚|dur0|du r0t dƒ‚t| ƒs>| g} t|ƒsL|g}d}
dd„ | | D ƒ}|rtt|ddd�}
t| |d	d
�}t||dd
�}t||ƒ\}}}}ddlm} i tt|| ƒƒ¥tt||ƒƒ¥}|||
d�}|du röd|d< t	ddœ}n||d< d|i}|j
d|||||ddœ|¤Ž}|	du�r>t||	||d�}|j|dd�}|j|dd�}|S )aº  
    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.

    Reference :ref:`the user guide <reshaping.crosstabulations>` for more examples.

    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 rB   )r9   r   r   )rE   ri   rB   rB   rC   r�   ¸  rŽ   zcrosstab.<locals>.<listcomp>TF)Z	intersectr.   Úrow©Úprefixrž   r   r    r�   Ú	__dummy__)r)   r3   r)   )r1   r2   r*   r,   r+   r-   )r¡   r*   r,   )r1   r6   r#   )r2   r6   )r¥   )rU   r   r   Ú
_get_namesÚ_build_names_mapperrz   r!   r…   ÚziprT   r"   Ú
_normalizeZrename_axis)r1   r2   r0   ÚrownamesÚcolnamesr)   r*   r,   r+   r¡   Z
common_idxZ	pass_objsÚrownames_mapperÚunique_rownamesÚcolnames_mapperÚunique_colnamesr!   r(   ZdfÚkwargsrA   rB   rB   rC   ÚcrosstabB  sf    jûÿþ ÿùø
ÿr±   )rA   r*   r,   r/   c              
   C  sL  t |ttfƒsTdddœ}z|| }W n. tyR } ztdƒ|‚W Y d }~n
d }~0 0 |du rÒdd„ dd„ d	d„ d
œ}|d |d< z|| }W n. tyº } ztdƒ|‚W Y d }~n
d }~0 0 || ƒ} |  d¡} �nv|du �r@| j}| j}	| jdd d …f j	}
||
v||
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u �r6|| ¡  }|| ¡  }d|j|< t| |gdd�} |  |¡} |  d¡} || _|	| _ntdƒ‚ntdƒ‚| S )Nr1   r2   )r   r#   zNot a valid normalize argumentFc                 S  s   | | j dd�j dd� S ©Nr#   rN   r   ©Úsum©ri   rB   rB   rC   Ú<lambda>ü  rŽ   z_normalize.<locals>.<lambda>c                 S  s   | |   ¡  S rD   r³   rµ   rB   rB   rC   r¶   ý  rŽ   c                 S  s   | j | jdd�dd�S r²   )Údivr´   rµ   rB   rB   rC   r¶   þ  rŽ   )rI   r2   r1   rI   Tr   éÿÿÿÿz not in pivoted DataFrame)r¡   r*   r#   rN   zNot a valid margins argument)r9   r'   rq   rQ   rU   rc   r1   r2   Zilocrk   r©   r´   r   ru   Úloc)rA   r¡   r*   r,   Z	axis_subsÚerrZnormalizersÚfZtable_indexÚtable_columnsZlast_ind_or_colZcolumn_marginZindex_marginrB   rB   rC   r©   ï  sd    
 ý 









r©   r¢   rq   r£   c                 C  s„   |d u rVg }t | ƒD ]>\}}t|tƒr>|jd ur>| |j¡ q| |› d|› �¡ qn*t|ƒt| ƒkrntdƒ‚t|tƒs€t|ƒ}|S )NÚ_z*arrays and names must have the same length)Ú	enumerater9   r   rk   r<   rT   ÚAssertionErrorr:   )ZarrsrM   r¤   rh   ZarrrB   rB   rC   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 | ]}|ˆ vr|’qS rB   rB   r›   ©ÚseenrB   rC   Ú	<setcomp>p  rŽ   z>_build_names_mapper.<locals>.get_duplicates.<locals>.<setcomp>)r{   rL   rB   rÀ   rC   Úget_duplicatesn  s    z+_build_names_mapper.<locals>.get_duplicatesc                   s$   i | ]\}}|ˆ v rd |› �|“qS ©Zrow_rB   ©rE   rh   rk   ©Z	dup_namesrB   rC   Ú
<dictcomp>u  s   z'_build_names_mapper.<locals>.<dictcomp>c                   s&   g | ]\}}|ˆ v rd |› �n|‘qS rÄ   rB   rÅ   rÆ   rB   rC   r�   x  s   z'_build_names_mapper.<locals>.<listcomp>c                   s$   i | ]\}}|ˆ v rd |› �|“qS ©Zcol_rB   rÅ   rÆ   rB   rC   rÇ   |  s   c                   s&   g | ]\}}|ˆ v rd |› �n|‘qS rÈ   rB   rÅ   rÆ   rB   rC   r�     s   )r{   Úintersectionr¾   )rª   r«   rÃ   Zshared_namesr¬   r­   r®   r¯   rB   rÆ   rC   r§   P  s     
ÿ
ÿ
ÿ
ÿr§   )r&   N)r&   )r&   )r&   )NNNNFr&   TF)r&   )r¢   )EÚ
__future__r   Úcollections.abcr   r   Útypingr   r   r   r   rZ   Únumpyre   Zpandas._libsr	   Zpandas.util._decoratorsr
   r   Zpandas.util._exceptionsr   Zpandas.core.dtypes.castr   Zpandas.core.dtypes.commonr   r   r   Zpandas.core.dtypes.dtypesr   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   Zpandas._typingr   r   r   r   rz   r!   rV   r"   r;   rf   rs   rw   ry   r8   r˜   r±   r©   r¦   r§   rB   rB   rB   rC   Ú<module>   s‚   õ"7   ÷V ÿ ø[ ù-ûH        ö / ÿP