o
    ҷhQm                     @  s  d dl mZ d dlmZmZ d dlmZmZm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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+m,Z, erd dl-m.Z.m/Z/m0Z0m1Z1 d dl2m3Z3 edee d dd										dOdPd'dZ4dQd)d*Z5		dRdSd-d.Z6	dTdUd/d0Z7	dTdVd1d2Z8	dTdWd3d4Z9d5d6 Z:edee d7 ddej;ej;d8dXd>d7Z<								dYdZd@dAZ=	dTd[dBdCZ>d\d]dGdHZ?d^dMdNZ@dS )_    )annotations)HashableSequence)TYPE_CHECKINGCallablecastN)lib)AppenderSubstitution)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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%   r.   r/   r0   r&   r1   r'   r)   r*   r+   r,   piecesr3   func_tabletable rA   L/var/www/html/venv/lib/python3.10/site-packages/pandas/core/reshape/pivot.pyr    :   sJ   

!AggFuncTypeBase | AggFuncTypeDictc                 C  s0  || }|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|}| j||	|
|d}||}|rt|trt|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}|st|jtrt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rD||}|tu rD|	sDt|rD|tj }|ra|rT| | ! j"dd } t#|| |||||||d	}|ru|su|j	jdkru|j	$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,   r)   all)howr!   r1   namesr   )r4   r1   r4   )rowscolsr&   r+   r*   r1   )rE   r4   )&r   r8   KeyErrorr7   r   keyr:   	TypeErrorlenr0   drop
ValueErrorgroupbyaggr   r)   r/   nlevelsrH   rangeunstackr   from_arraysr   levelsreindex
sort_indexfillnar   
is_integerastypenpint64notnarD   _add_margins	droplevelT)r%   r.   r/   r0   r&   r1   r'   r)   r*   r+   r,   r3   values_passedvalues_multii	to_filterxrM   groupedaggedr@   index_names
to_unstacknamemrA   rA   rB   r9   s   s   





r9   r@   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 |rt| |||||||}t |ts|S |\}}}nt | tsJ t| ||||||}t |ts|S |\}}}|j|j|d}|D ]}t |tr|| ||< q||d  ||< qdd	lm} ||t|gd
j}|jj}t|jD ]}t |trq||gj}|| jt|fd||< q||}||j_|S )Nz&margins_name argument must be a stringzConflicting name "z" in margins   r!    rF   r   r   )r0   )args)r7   strrQ   r/   rH   get_level_values_compute_grand_marginndimr0   rO   r   _append_constructor_generate_marginal_resultstupler   )_generate_marginal_results_without_valuesrY   pandasr   r   rc   setdtypesr   select_dtypesapplyr   )r@   r%   r.   rJ   rK   r&   r+   r*   r1   msglevelgrand_marginrM   marginal_result_setresultmargin_keys
row_marginkr   margin_dummy	row_namesdtyperA   rA   rB   ra      sd   







ra   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 N)itemsr7   rt   r;   dictrN   r/   )r%   r.   r&   r*   r   r   vrA   rA   rB   rv   F  s"   

rv   c                   s  t  dkrg }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rt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   }|j
||_
n	|jtj|jd}||	|fS )Nr   c                   s   | fdt  d   S )Nrq   r!   rO   )rM   rK   r*   rA   rB   _all_keym  s   z,_generate_marginal_results.<locals>._all_keyr+   r!   )r   r+   r   rG   rm   rI   T)future_stackr/   )rO   rR   rS   rc   copyr:   r}   r   r   r7   r/   r   from_tuplesrH   r   rm   r   r0   stackr8   rU   reorder_levels_constructor_slicedr^   nan)r@   r%   r.   rJ   rK   r&   r+   r*   table_piecesr   r   margincat_axisrM   pieceall_keyr   transformed_piecer   r   	new_orderrA   r   rB   rz   ]  sZ   





rz   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!   rq   r   rA   r   rA   rB   r     s   z;_generate_marginal_results_without_values.<locals>._all_keyr   )r   r4   r+   r   )rO   rR   r   r:   r0   r   r^   r   )r@   r%   rJ   rK   r&   r+   r*   r   r   r   r   r   r   rA   r   rB   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 r   )	r   r7   r^   ndarrayr   r   r   callabler8   )byrA   rA   rB   r6     s   	r6   pivot)r/   r.   r0   r   r/   IndexLabel | lib.NoDefaultr.   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|rt|tst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 r   r   
no_default.0rm   rA   rA   rB   
<listcomp>      zpivot.<locals>.<listcomp>)r:   c                   s   g | ]} j |qS rA   )r/   ru   )r   rf   r%   rA   rB   r     s    r   c                      g | ]} | qS rA   rA   )r   idxr   rA   rB   r         c                   r   rA   rA   )r   colr   rA   rB   r     r   )r/   r0   r   c                 S  s   g | ]}|t jur|nd qS r   r   r   rA   rA   rB   r   *  r   )comconvert_to_list_liker   r/   rH   r   r   	set_indexr7   r   rU   rT   r   rm   extendrW   r   r{   r   r   r   ry   _valuesrV   )r%   r0   r/   r.   columns_listlikerK   r:   indexed
index_listdata_columns
multiindexr   rA   r   rB   r     sH   
	










	normalizec
                 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urt||	||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 rA   )r7   r   r   )r   rh   rA   rA   rB   r     s    zcrosstab.<locals>.<listcomp>TF)	intersectr,   row)prefixr   r   r   r   	__dummy__)r&   r1   r&   )r/   r0   r'   r*   r)   )r   r'   r*   )r/   r4   r!   )r0   r4   )r   )rQ   r   r   
_get_names_build_names_mapperr}   r   r   ziprO   r    
_normalizerename_axis)r/   r0   r.   rownamescolnamesr&   r'   r*   r)   r   
common_idx	pass_objsrownames_mapperunique_rownamescolnames_mapperunique_colnamesr   r%   dfkwargsr@   rA   rA   rB   crosstab1  sd   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@ rt| 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)Nr/   r0   )r   r!   zNot a valid normalize argumentFc                 S  s   | | j ddj dd S Nr!   rI   r   sumrh   rA   rA   rB   <lambda>      z_normalize.<locals>.<lambda>c                 S  s   | |    S r   r   r   rA   rA   rB   r     s    c                 S  s   | j | jddddS r   )divr   r   rA   rA   rB   r     s    )rD   r0   r/   rD   Tr   z not in pivoted DataFrame)r   r'   r!   rI   zNot a valid margins argument)r7   r(   rt   rL   rQ   r[   r/   r0   ilocrm   r   r   r   rx   loc)r@   r   r'   r*   	axis_subserrnormalizersftable_indextable_columnslast_ind_or_colcolumn_marginindex_marginrA   rA   rB   r     sr   



3






r   r   r   rt   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)	enumerater7   r   rm   r:   rO   AssertionErrorr8   )arrsrH   r   rf   arrrA   rA   rB   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 rA   rA   r   seenrA   rB   	<setcomp>^  r   z>_build_names_mapper.<locals>.get_duplicates.<locals>.<setcomp>)r~   rG   rA   r   rB   get_duplicates\  s   z+_build_names_mapper.<locals>.get_duplicatesc                   $   i | ]\}}| v rd | |qS row_rA   r   rf   rm   	dup_namesrA   rB   
<dictcomp>c      z'_build_names_mapper.<locals>.<dictcomp>c                   &   g | ]\}}| v rd | n|qS r   rA   r   r   rA   rB   r   f      z'_build_names_mapper.<locals>.<listcomp>c                   r   col_rA   r   r   rA   rB   r   j  r   c                   r   r   rA   r   r   rA   rB   r   m  r   )r~   intersectionr   )r   r   r   shared_namesr   r   r   r   rA   r   rB   r   >  s    



r   )
NNNr#   NFTr$   FT)r%   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,   r(   r-   r   )r$   N)r@   ro   r%   r   r+   r(   r*   r   )r$   )r%   r   r*   r   )r%   r   r+   r(   r*   r   )r@   r   r%   r   r+   r(   r*   r   )
r%   r   r0   r   r/   r   r.   r   r-   r   )NNNNFr$   TF)
r'   r(   r*   r   r)   r(   r   r(   r-   r   )r@   r   r'   r(   r*   r   r-   r   )r   )r   rt   )r   r   r   r   r-   r   )A
__future__r   collections.abcr   r   typingr   r   r   numpyr^   pandas._libsr   pandas.util._decoratorsr	   r
   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    r9   ra   rv   rz   r|   r6   r   r   r   r   r   r   rA   rA   rA   rB   <module>   s    
7 UZ-G .P