o
    kìEiõ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r™d dl1m2Z2m3Z3m4Z4m5Z5 d dl6m7Z7 edƒee$d dd�ddddddddej8df
dRd)d„ƒƒZ9dSd+d,„Z:		dTdUd/d0„Z;	dVdWd1d2„Z<	dVdXd3d4„Z=	dVdYd5d6„Z>d7d8„ Z?edƒee$d9 dd�ej8ej8d:œdZd@d9„ƒƒZ@								d[d\dCdD„ZA	dVd]dEdF„ZBd^d_dJdK„ZCd`dPdQ„ZDdS )aé    )Ú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ÚAllÚdatar!   Úaggfuncr   ÚmarginsÚboolÚdropnaÚmargins_namer   Úobservedúbool | lib.NoDefaultÚsortÚreturnc                 C  s¬   t |ƒ}t |ƒ}t|tƒrAg }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   Ú__finalize__)r'   r1   r2   r3   r(   r4   r)   r+   r,   r-   r/   Úpiecesr6   ÚfuncÚ_tableÚtable© rD   úV/var/www/agentarbitrage/venv/lib/python3.10/site-packages/pandas/core/reshape/pivot.pyr"   =   sJ   
õ
õú!AggFuncTypeBase | AggFuncTypeDictc                 C  sv  || }|du}|rZt |ƒrd}t|ƒ}nd}|g}|D ]
}|| vr&t|ƒ‚qg }|| D ]}t|tƒr7|j}z|| v rA| |¡ W q- tyK   Y q-w t|ƒt| j	ƒk rY| | } n| j	}|D ]}z| 
|¡}W q_ tttfyt   Y q_w t|ƒ}|	tju r€dn|	}| j|||
|d�}|	tju r¤tdd„ |jjD ƒƒr¤t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ð| |¡ qÚ| |¡ qÚ|j||d�}|�s8t|jtƒ�rtjt |jj!ƒ|jjd�}|j"|d|d�}t|j	tƒ�r8tjt |j	j!ƒ|j	jd�}|j"|d|d�}|
du �rIt|tƒ�rI|j#dd�}|du�rg| $|¡}|tu �rg|	�sgt %|¡�rg| &t'j(¡}|�r„|�rw| |  )¡ j*dd� } t+|| |||||||d�	}|�r˜|�s˜|j	jdk�r˜|j	 ,d¡|_	t|ƒdk�r©t|ƒ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/   r+   c                 s  s   � | ]}|j V  qd S ©N)Ú_passed_categorical)Ú.0ÚpingrD   rD   rE   Ú	<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#   ©r4   ©Únamesr   )r7   r4   ©r7   )ÚrowsÚcolsr(   r-   r,   r4   )rO   r7   ).r   r;   ÚKeyErrorr:   r   Úkeyr=   Ú	TypeErrorÚlenr3   ÚdropÚ
ValueErrorr	   Ú
no_defaultÚgroupbyÚanyÚ_grouperÚ	groupingsÚwarningsÚwarnÚFutureWarningr   Úaggr   r+   r2   ÚnlevelsrR   ÚrangeÚunstackr   Úfrom_arraysr   ÚlevelsÚreindexÚ
sort_indexÚfillnaÚ
is_integerÚastypeÚnpÚint64ÚnotnarN   Ú_add_marginsÚ	droplevelÚT)r'   r1   r2   r3   r(   r4   r)   r+   r,   r-   r/   r6   Úvalues_passedÚvalues_multiÚiÚ	to_filterÚxrW   Úobserved_boolÚgroupedÚaggedrC   Úindex_namesÚ
to_unstackÚnameÚmrD   rD   rE   r<   v   s¾   
ÿ

€ÿ€ÿÿû
ÿÿ

÷r<   rC   úDataFrame | Seriesc	              	   C  s  t |tƒs	tdƒ‚d|› d�}	| jjD ]}
|| j |
¡v r!t|	ƒ‚qt||||ƒ}| jdkrE| jjdd … D ]}
|| j |
¡v rDt|	ƒ‚q6t	|ƒdkrW|fdt	|ƒd   }n|}|slt | t
ƒrl|  |  ||| i¡¡S |r†t| |||||||ƒ}t |tƒs€|S |\}}}nt | tƒs�J ‚t| ||||||ƒ}t |tƒsž|S |\}}}|j|j|d�}|D ]}t |tƒr»|| ||< q­||d  ||< q­d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#   ©Ú rP   r   r    )r3   )Úargs)r:   Ústrr[   r2   rR   Úget_level_valuesÚ_compute_grand_marginÚndimr3   rY   r   Ú_appendÚ_constructorÚ_generate_marginal_resultsÚtupler   Ú)_generate_marginal_results_without_valuesrj   Úpandasr!   r   rt   ÚsetÚdtypesr   Úselect_dtypesÚapplyr   )rC   r'   r1   rT   rU   r(   r-   r,   r4   ÚmsgÚlevelÚgrand_marginrW   Úmarginal_result_setÚresultÚmargin_keysÚ
row_marginÚkr!   Úmargin_dummyÚ	row_namesÚdtyperD   rD   rE   rr      sd   
ÿ
ÿÿ
ÿ



ÿ
rr   c              	   C  s®   |rPi }| |   ¡ D ]C\}}z6t|tƒrt||ƒƒ ||< n&t|tƒr=t|| tƒr4t||| ƒƒ ||< n|| |ƒ||< n||ƒ||< W q
 tyM   Y q
w |S ||| jƒiS rG   )Úitemsr:   r†   r>   ÚdictrX   r2   )r'   r1   r(   r,   r–   r›   ÚvrD   rD   rE   rˆ   U  s"   

€ÿrˆ   c                   s
  t ˆ ƒdkrºg }g }	‡ ‡fdd„}
t |ƒdkrP|||  j||d� |¡}d}| jjd|d�D ]\}}|j}|
|ƒ}| ¡ }|| ||< | |¡ |	 |¡ q/nWddlm} d}| jd|d�D ]G\}}t ˆ ƒdkrn|
|ƒ}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 )Nrƒ   r#   ©rY   )rW   ©rU   r,   rD   rE   Ú_all_key|  s   z,_generate_marginal_results.<locals>._all_key©r-   r#   )r•   r-   r    rQ   ©r   rS   T)Úfuture_stackc                   s   g | ]}ˆ j j| ‘qS rD   )r2   rR   ©rI   rw   )rš   rD   rE   Ú
<listcomp>¸  ó    z._generate_marginal_results.<locals>.<listcomp>©r2   )rY   r]   rd   rt   Úcopyr=   r�   r!   r“   r:   r2   r   Úfrom_tuplesrR   r   r   r   r3   Ústackr;   rf   Úreorder_levelsÚ_constructor_slicedro   Únan)rC   r'   r1   rT   rU   r(   r-   r,   Útable_piecesr™   r¤   ÚmarginÚcat_axisrW   ÚpieceÚall_keyr!   Útransformed_piecer˜   Únew_order_indicesÚnew_order_namesrD   )rU   r,   rš   rE   rŒ   l  s\   
÷


ÿ
ÿ
rŒ   c                   sÜ   t ˆ ƒdkrKg }‡ ‡fdd„}t |ƒdkr0|j||d�|  |¡}	|ƒ }
|	| |
< | }| |
¡ n |jdd|d� |¡}	|ƒ }
|	| |
< | }| |
¡ |S | }| j}t ˆ ƒra|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ƒ   r¢   rD   r£   rD   rE   r¤   Î  s   z;_generate_marginal_results_without_values.<locals>._all_keyr¥   )r•   r7   r-   r«   )rY   r]   r“   r=   r3   r   ro   r±   )rC   r'   rT   rU   r(   r-   r,   r™   r¤   r³   r¶   r˜   rš   rD   r£   rE   rŽ   À  s*   


rŽ   c                 C  sJ   | d u rg } | S t | ƒst| tjtttfƒst| ƒr| g} | S t| ƒ} | S rG   )	r   r:   ro   Úndarrayr   r   r   Úcallabler;   )ÚbyrD   rD   rE   r9   í  s   	ùÿþýÿr9   Úpivot)r2   r1   r3   r   r2   úIndexLabel | lib.NoDefaultr1   c                  sr  t  |¡}ˆ jdd�‰ ˆ j ¡ ˆ _dd„ ˆ jjD ƒˆ j_|tju r=|tjur,t  |¡}ng }|tju }ˆ j|| |d�}nj|tju rbtˆ jt	ƒrV‡ fdd„t
ˆ jjƒD ƒ}nˆ 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 rG   ©r	   r\   ©rI   r   rD   rD   rE   r©     ó    ÿzpivot.<locals>.<listcomp>)r=   c                   s   g | ]}ˆ j  |¡‘qS rD   )r2   r‡   r¨   ©r'   rD   rE   r©   !  s    ÿr¦   c                   ó   g | ]}ˆ | ‘qS rD   rD   )rI   ÚidxrÃ   rD   rE   r©   )  ó    c                   rÄ   rD   rD   )rI   ÚcolrÃ   rD   rE   r©   +  rÆ   )r2   r3   r«   c                 S  s   g | ]}|t jur|nd ‘qS rG   rÀ   rÁ   rD   rD   rE   r©   ;  rÂ   )ÚcomÚconvert_to_list_liker¬   r2   rR   r	   r\   Ú	set_indexr:   r   rf   re   r°   r   Úextendrh   r   r�   r   r   r   r‹   Ú_valuesrg   )r'   r3   r2   r1   Úcolumns_listlikerU   r=   ÚindexedÚ
index_listÚdata_columnsÚ
multiindexr˜   rD   rÃ   rE   r½   û  sH   
	
ÿ


ÿ


ÿÿ

ÿ

ÿÚ	normalizeú/bool | Literal[0, 1, 'all', 'index', 'columns']c
              	   C  s^  |du r|durt dƒ‚|dur|du rt dƒ‚t| ƒs| g} t|ƒs&|g}d}
dd„ | | D ƒ}|r:t|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 rD   )r:   r   r   )rI   ry   rD   rD   rE   r©   ¸  s    zcrosstab.<locals>.<listcomp>TF)Ú	intersectr/   Úrow)ÚprefixrÇ   r   r    r«   Ú	__dummy__)r(   r4   r(   )r2   r3   r)   r,   r+   r-   )rÒ   r)   r,   )r2   r7   r#   )r3   r7   )r×   )r[   r   r   Ú
_get_namesÚ_build_names_mapperr�   r!   r    ÚziprY   r"   Ú
_normalizeÚrename_axis)r2   r3   r1   ÚrownamesÚcolnamesr(   r)   r,   r+   rÒ   Ú
common_idxÚ	pass_objsÚrownames_mapperÚunique_rownamesÚcolnames_mapperÚunique_colnamesr!   r'   ÚdfÚkwargsrC   rD   rD   rE   ÚcrosstabB  sf   jûÿþÿùøÿrç   c              
   C  s,  t |ttfƒs$dddœ}z|| }W n ty# } ztdƒ|‚d }~ww |du r]dd„ dd„ d	d„ d
œ}|d |d< z|| }W n tyQ } ztdƒ|‚d }~ww || ƒ} |  d¡} | S |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¡} |	| _| S |dkrÚ|| ¡  }|  |¡} |  d¡} || _| S |dksã|du �r|| ¡  }|| ¡  }d|j|< t| |gdd�} |  |¡} |  d¡} || _|	| _| S tdƒ‚tdƒ‚)Nr2   r3   )r   r#   zNot a valid normalize argumentFc                 S  s   | | j dd�j dd� S ©Nr#   rS   r   ©Úsum©ry   rD   rD   rE   Ú<lambda>ü  rª   z_normalize.<locals>.<lambda>c                 S  s   | |   ¡  S rG   ré   rë   rD   rD   rE   rì   ý  s    c                 S  s   | j | jdd�dd�S rè   )Údivrê   rë   rD   rD   rE   rì   þ  s    )rN   r3   r2   rN   Tr   éÿÿÿÿz not in pivoted DataFrame)rÒ   r)   r#   rS   zNot a valid margins argument)r:   r*   r†   rV   r[   rl   r2   r3   Úilocr   rÛ   rê   r   rŠ   Úloc)rC   rÒ   r)   r,   Ú	axis_subsÚerrÚnormalizersÚfÚtable_indexÚtable_columnsÚlast_ind_or_colÚcolumn_marginÚindex_marginrD   rD   rE   rÛ   ï  sr   

€ÿý
€ÿ
3
Ï
é

ï


ûrÛ   rÕ   rÖ   r†   c                 C  s†   |d u r,g }t | ƒD ]\}}t|tƒr|jd ur| |j¡ q
| |› d|› �¡ q
|S t|ƒt| ƒkr8tdƒ‚t|tƒsAt|ƒ}|S )NÚ_z*arrays and names must have the same length)Ú	enumerater:   r   r   r=   rY   ÚAssertionErrorr;   )ÚarrsrR   rÖ   rw   ÚarrrD   rD   rE   rØ   ?  s   û
rØ   rÝ   ú	list[str]rÞ   ú;tuple[dict[str, str], list[str], dict[str, str], list[str]]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 rD   rD   rÁ   ©ÚseenrD   rE   Ú	<setcomp>p  rª   z>_build_names_mapper.<locals>.get_duplicates.<locals>.<setcomp>)r�   rQ   rD   r  rE   Úget_duplicatesn  s   z+_build_names_mapper.<locals>.get_duplicatesc                   ó$   i | ]\}}|ˆ v rd |› �|“qS ©Úrow_rD   ©rI   rw   r   ©Ú	dup_namesrD   rE   Ú
<dictcomp>u  ó    z'_build_names_mapper.<locals>.<dictcomp>c                   ó&   g | ]\}}|ˆ v rd |› �n|‘qS r  rD   r  r	  rD   rE   r©   x  ó    ÿz'_build_names_mapper.<locals>.<listcomp>c                   r  ©Úcol_rD   r  r	  rD   rE   r  |  r  c                   r  r  rD   r  r	  rD   rE   r©     r  )r�   Úintersectionrû   )rÝ   rÞ   r  Úshared_namesrá   râ   rã   rä   rD   r	  rE   rÙ   P  s    
ÿ
ÿ
ÿ
ÿrÙ   )r'   r!   r(   r   r)   r*   r+   r*   r,   r   r-   r.   r/   r*   r0   r!   )r'   r!   r(   rF   r)   r*   r+   r*   r,   r   r-   r.   r/   r*   r0   r!   )r&   N)rC   r�   r'   r!   r-   r*   r,   r   )r&   )r'   r!   r,   r   )r'   r!   r-   r*   r,   r   )rC   r!   r'   r!   r-   r*   r,   r   )
r'   r!   r3   r   r2   r¾   r1   r¾   r0   r!   )NNNNFr&   TF)
r)   r*   r,   r   r+   r*   rÒ   rÓ   r0   r!   )rC   r!   r)   r*   r,   r   r0   r!   )rÕ   )rÖ   r†   )rÝ   rÿ   rÞ   rÿ   r0   r   )EÚ
__future__r   Úcollections.abcr   r   Útypingr   r   r   r   ra   Únumpyro   Úpandas._libsr	   Úpandas.util._decoratorsr
   r   Úpandas.util._exceptionsr   Úpandas.core.dtypes.castr   Úpandas.core.dtypes.commonr   r   r   Úpandas.core.dtypes.dtypesr   Úpandas.core.dtypes.genericr   r   Úpandas.core.commonÚcoreÚcommonrÈ   Úpandas.core.framer   Úpandas.core.groupbyr   Úpandas.core.indexes.apir   r   r   Úpandas.core.reshape.concatr   Úpandas.core.reshape.utilr   Úpandas.core.seriesr   Úpandas._typingr   r   r   r   r�   r!   r\   r"   r<   rr   rˆ   rŒ   rŽ   r9   r½   rç   rÛ   rØ   rÙ   rD   rD   rD   rE   Ú<module>   s„    õ
7 ÷Vÿø[ù-ûHö /ÿP