
    `Vgk                       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 d dlmZmZmZ d dlmZ d dlmZ d dlmZmZmZmZ d d	lmZmZ d dlm c 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l1m2Z2  ed       ee$d   d      	 	 	 	 	 	 	 	 	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%d              Z3	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d&dZ4	 	 	 d'	 	 	 	 	 d(dZ5d)d*dZ6	 d)	 d+dZ7	 d)	 	 	 d,dZ8d Z9 ed       ee$d   d       eddg      	 	 	 d-	 	 	 	 	 	 	 	 	 d.d                     Z:	 	 	 	 	 	 	 	 d/	 	 	 	 	 	 	 d0d Z;	 d)	 	 	 	 	 d1d!Z<d2d3d"Z=	 	 	 	 	 	 d4d#Z>y)5    )annotations)TYPE_CHECKINGCallableHashableSequencecastN)AggFuncTypeAggFuncTypeBaseAggFuncTypeDict
IndexLabel)AppenderSubstitutiondeprecate_nonkeyword_arguments)rewrite_warning)maybe_downcast_to_dtype)is_integer_dtypeis_list_likeis_nested_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datac                |   t        |      }t        |      }t        |t              rog }g }|D ]E  }t        | |||||||||	|
      }|j	                  |       |j	                  t        |d|             G t        ||d      }|j                  | d      S t        | |||||||||	|
      }|j                  | d      S )N)
valuesindexcolumns
fill_valueaggfuncmarginsdropnamargins_nameobservedsort__name__r#   )keysaxisr"   )method)_convert_by
isinstancelist__internal_pivot_tableappendgetattrr   __finalize__)r%   r'   r(   r)   r+   r*   r,   r-   r.   r/   r0   piecesr2   func_tabletables                   Z/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/pandas/core/reshape/pivot.pyr"   r"   8   s     E'"G'4 "$ 	9D+%)!F MM&!KKj$78	9" vDq1!!$}!=="E d=99    c                T	   ||z   }|du}|rt        |      rd}t        |      }nd}|g}|D ]  }|| vst        |       g }||z   D ]4  }t        |t              r|j
                  }	 || v r|j                  |       6 t        |      t        | j                        k  r6| |   } n0| j                  }|D ]  }	 |j                  |      } t        |      }| j                  ||	|
      }d}t        dt        |      5  |j                  |      }ddd       |rt        t               rt        |j                        r|j#                  d	      }|D ]  }|| v st%        | |         s||v st%        ||         r+t        ||   t               r?t        | |   j&                  t(        j&                        sgt+        ||   | |   j&                        ||<    }|j,                  j.                  d
kD  r|r|j,                  j0                  dt        |       }g }t3        t        |      t        |            D ]D  }|j,                  j0                  |   }|||v r|j                  |       4|j                  |       F |j5                  |      }|st        |j,                  t6              r[t7        j8                  t;        |j,                  j<                        |j,                  j0                        }|j?                  |d      }t        |j                  t6              r[t7        j8                  t;        |j                  j<                        |j                  j0                        }|j?                  |d
      }|
du r"t        |t               r|jA                  d
      }||jC                  |d      }|r9|r#| | jE                         jG                  d
         } tI        || |||||||	      }|r.s,|j                  j.                  d
kD  r|jK                  dd
      }t        |      dk(  rt        |      dkD  r|jL                  }t        |t               r|r|j#                  dd
      }|S # t        $ r Y 7w xY w# t        t        t        f$ r Y w xY w# 1 sw Y   xY w)zL
    Helper of :func:`pandas.pivot_table` for any non-list ``aggfunc``.
    NTF)r/   r0   zpivot_table dropped a column because it failed to aggregate. This behavior is deprecated and will raise in a future version of pandas. Select only the columns that can be aggregated.z!The default value of numeric_only)target_messagetarget_categorynew_messageall)howr#   )namesr   r3   infer)downcast)rowscolsr+   r/   r.   r*   )rG   r3   )'r   r7   KeyErrorr6   r   keyr9   	TypeErrorlenr)   drop
ValueErrorgroupbyr   FutureWarningaggr   r-   r   dtypenpr   r(   nlevelsrH   rangeunstackr   from_arraysr   levelsreindex
sort_indexfillnanotnarF   _add_margins	droplevelT)r%   r'   r(   r)   r+   r*   r,   r-   r.   r/   r0   r2   values_passedvalues_multii	to_filterxrO   groupedmsgaggedvr?   index_names
to_unstacknamems                              r@   r8   r8   q   sf     7?D$&ML&\F LXF  	"A}qk!	" 	 	A!W%EE9$$Q'	 y>C--	?D  	CS)	
 fll4(l>G	* 
 
:%
 %
 G$% *UL1c%--6H'  	PAT	$T!W-J(q2!%(L9jGMM288?  7uQxaOE!H	P" E {{Q5 kk''#e*5
s5z3t9- 	(A;;$$Q'D|t{2!!!$!!$'	( j)ekk:.&&!%++"4"45U[[=N=NA MM!!M,EemmZ0&&!%--"6"67u}}?R?RA MM!!M,Et|
5,7  a (Z':

((a(01D%!

 \emm.C.Ca.G*
5zQ3w<!+ %&6Q/Lc   z84 % %s0   'Q27R=R2	Q?>Q?RRR'c	           
        t        |t              st        d      d| d}	| j                  j                  D ]*  }
|| j                  j                  |
      v s!t        |	       t        ||||      }| j                  dk(  rF| j                  j                  dd  D ]*  }
|| j                  j                  |
      v s!t        |	       t        |      dkD  r|fdt        |      dz
  z  z   }n|}|s/t        | t              r| j                  t        |||   i            S |r+t        | |||||||      }t        |t              s|S |\  }}}n;t        | t              sJ t!        | ||||||      }t        |t              s|S |\  }}}|j#                  |j                  |      }|D ]&  }t        |t              r	||   ||<   ||d      ||<   ( dd	lm}  ||t)        |g      
      j*                  }|j                  j                  }t-        |j.                        D ]<  }|j1                  |g      j                  }||   j3                  t4        |f      ||<   > |j                  |      }||j                  _        |S )Nz&margins_name argument must be a stringzConflicting name "z" in margins   r#    )r*   r   r    )r)   )args)r6   strrS   r(   rH   get_level_values_compute_grand_marginndimr)   rQ   r   _appendr   _generate_marginal_resultstupler   )_generate_marginal_results_without_valuesr^   pandasr!   r   rd   setdtypesselect_dtypesapplyr   )r?   r%   r'   rL   rM   r+   r/   r.   r*   rk   levelgrand_marginrO   marginal_result_setresultmargin_keys
row_marginkr!   margin_dummy	row_namesrW   s                         r@   rb   rb     s    lC(ABB|nL
9C"" "5;;77>>S/!" )vwMLzzQ]]((, 	&Eu}}==eDD o%	&
 4y1}oTQ 77j	2 }}VS,|*D$EFGG	84tWh
 -u5&&*='Z %...G4tWh
 -u5&&*='Z##FNNz#JJ /a(OJqM(1.JqM	/ !Zu>@@L""IV]]# 
##UG,44)$/55#5( 6 
T

 ^^L)F"FLLMrA   c                   |ri }| |   j                         D ]  \  }}	 t        |t              r t        ||             ||<   nUt        |t              r:t        ||   t              r t        |||                ||<   n ||   |      ||<   n ||      ||<    |S | || j                        iS # t
        $ r Y w xY wN)itemsr6   rw   r:   dictrP   r(   )r%   r'   r+   r.   r   r   rm   s          r@   ry   ry   \  s    L&&( 	DAqgs+&9ga&9&;LO.!'!*c2*@'!WQZ*@*BQ*4'!*Q-Q&-ajLO	 gdjj122	  s   A:B11	B=<B=c                   t              dkD  rvg }g }	fd}
t        |      dkD  r|||z      j                  ||      j                  |      }d}| j                  d||      D ]G  \  }} |
|      }|j                         }||   ||<   |j	                  |       |	j	                  |       I nddlm} d}| j                  d||      D ]  \  }}t              dkD  r	 |
|      }n}|j	                  |        ||j                  |            j                  }t        |g|j                  j                        |_
        |j	                  |       |	j	                  |        t        ||      }t        |      dk(  r|S | }| j                  }	t              dkD  r||z      j                  |      j                  |      }|j                         }t              gt        t!        t                          z   }|j                  j#                  |      |_
        n%t%        t&        j(                  |j                  	      }||	|fS )
Nr   c                0    | fdt              dz
  z  z   S )Nrt   r#   rQ   )rO   rM   r.   s    r@   _all_keyz,_generate_marginal_results.<locals>._all_keyz  s     &#d)a-)@@@rA   r/   r#   r   r3   r/   r    rp   rI   r(   )rQ   rT   rV   copyr9   r   r!   r   rd   r   r(   rp   r   r)   stackr7   rZ   reorder_levelsr   rX   nan)r?   r%   r'   rL   rM   r+   r/   r.   table_piecesr   r   margincat_axisrO   pieceall_keyr!   transformed_piecer   r   	new_orders       `  `             r@   r|   r|   r  s.    4y1}	A t9q=$-(000IMMgVFH#mm!(XmV ,
U"3- 

!'g##E*""7+, )H#mm!(XmV ,
Ut9q=&smG*G##E* %.ekk'.B$C$E$E!*/	@P@P*Q!' ##$56""7+, 84t9>Mmm
4y1}$-(000IMMgV
%%'
 YK$uSY'7"88	%++::9E
BFF&..9
;
**rA   c                2   t              dkD  rg }fd}t        |      dkD  rE||   j                  ||      j                  |      }	 |       }
|	| |
<   | }|j                  |
       nR|j                  dd|      j                  |      }	 |       }
|	| |
<   | }|j                  |
       |S | }| j                  }t              r&|   j                  |      j                  |      }n%t        t        j                  |j                        }|||fS )Nr   c                 N    t               dk(  rS fdt               dz
  z  z   S )Nr#   rt   r   )rM   r.   s   r@   r   z;_generate_marginal_results_without_values.<locals>._all_key  s.    4yA~## ?Uc$i!m%<<<rA   r   r   r   )rQ   rT   r   r9   r)   r   rX   r   )r?   r%   rL   rM   r+   r/   r.   r   r   r   r   r   r   s      `  `      r@   r~   r~     s    4y1}$&	=
 t9q=$Z''x'@FFwOFjG#E'NFw' \\H\EKKGTFjG#E'NFw'Mmm
4y$Z''x'@FFwO
BFF&..9
;
**rA   c                    | g } | S t        |       s5t        | t        j                  t        t
        t        f      st        |       r| g} | S t        |       } | S r   )	r   r6   rX   ndarrayr   r   r   callabler7   )bys    r@   r5   r5     sX    	z I 	"b2::uiABB<T I "XIrA   pivot)versionallowed_argsc                   |t        d      t        j                  |      }|6|t        j                  |      }ng }|d u }| j                  ||z   |      }n`|t	        | j
                  t              rFt        | j
                  j                        D cg c]  }| j
                  j                  |       }	}nRt        | j
                  | j
                  j                        g}	n%t        j                  |      D 
cg c]  }
| |
   	 }	}
|D cg c]  }| |   	 }}|	j                  |       t        j                  |	      }t        |      rIt	        |t              s9t!        t"        t$           |      }| j'                  | |   j(                  ||      }n | j+                  | |   j(                  |      }|j-                  |      S c c}w c c}
w c c}w )Nz.pivot() missing 1 required argument: 'columns')r9   r   )r(   r)   r   )rP   comconvert_to_list_like	set_indexr6   r(   r   rZ   rY   rx   r   rp   extendr\   r   r}   r   r   r   _constructor_values_constructor_slicedr[   )r%   r(   r)   r'   columns_listlikerM   r9   indexedrg   
index_listidxcoldata_columns
multiindexs                 r@   r   r     s    HII//8 ~++E2DD$ ..##F ! 
 =$**j1 =B$**BTBT<U78DJJ//2
  %TZZdjjooFG
/2/G/G/NO$s)OJO-=>cS	>>,'++J7

65(A(8,f5F''V$$J ( G ..tF|/C/C:.VG ??+,,/ P>s   "GGGc
           	        ||t        d      ||t        d      t        |       s| g} t        |      s|g}d}
| |z   D cg c]  }t        |t        t        f      s| }}|rt        |dd      }
t        | |d      }t        ||d	      }t        ||      \  }}}}d
dlm	} i t        t        ||             t        t        ||            } |||
      }|d
|d<   t        d
d}n	||d<   d|i} |j                  	 d|||||d|}|	durt        ||	||      }|j                  |d
      }|j                  |d      }|S c c}w )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.TF)	intersectr0   row)prefixr   r   r    r   	__dummy__)r+   r*   r+   )r(   r)   r,   r.   r-   )	normalizer,   r.   )r(   r3   r#   )r)   r3   )r   )rS   r   r6   r   r   r   
_get_names_build_names_mapperr   r!   r   ziprQ   r"   
_normalizerename_axis)r(   r)   r'   rownamescolnamesr+   r,   r.   r-   r   
common_idxri   	pass_objsrownames_mapperunique_rownamescolnames_mapperunique_colnamesr!   r%   dfkwargsr?   s                         r@   crosstabr     s   T ~'-ABBgoDEEu%w')J!GOXqz!i=V/WXIX+IER
%%8H'8E:H 	Hh/ !
s?E*
+
s?G,
-D 
4z	*B~; 2 ;W%BNN! E Yl
 O!<EoA>ELc Ys   E Ec                   t        |t        t        f      sddd}	 ||   }|du r2d d d d	}|d
   |d<   	 ||   } ||       } | j                  d      } | S |du r| j                  }| j                  }	| j                  dd d f   j                  }
||
v||
k7  z  rt	        | d      | j                  d ddf   }| j                  dd df   }| j                  d dd df   } t        | |d      } |dk(  r<||j                         z  }t        | |gd      } | j                  d      } |	| _        | S |dk(  r>||j                         z  }| j                  |      } | j                  d      } || _        | S |d
k(  s|du rv||j                         z  }||j                         z  }d|j                  |<   t        | |gd      } | j                  |      } | j                  d      } || _        |	| _        | S t	        d      t	        d      # t        $ r}t	        d      |d }~ww xY w# t        $ r}t	        d      |d }~ww xY w)Nr(   r)   )r   r#   zNot a valid normalize argumentFc                L    | | j                  d      j                  d      z  S Nr#   rI   r   sumri   s    r@   <lambda>z_normalize.<locals>.<lambda>  s#    QA!2!2!2!:: rA   c                (    | | j                         z  S r   r   r   s    r@   r   z_normalize.<locals>.<lambda>  s    QUUW rA   c                H    | j                  | j                  d      d      S r   )divr   r   s    r@   r   z_normalize.<locals>.<lambda>  s    quuQUUU]u; rA   )rF   r)   r(   rF   Tr   z not in pivoted DataFrame)r   r,   r#   rI   zNot a valid margins argument)r6   boolrw   rN   rS   r`   r(   r)   ilocrp   r   r   r   r{   loc)r?   r   r,   r.   	axis_subserrnormalizersftable_indextable_columnslast_ind_or_colcolumn_marginindex_margins                r@   r   r     s    i$-I.		H!),I % ;,;3
 (.D	HI&A %Qf Lc 
Dkk**RU+00 /LO4ST~-FGHH

3B37+zz"crc'* 

3B38$ 5IuE 	!)M,=,=,??ME=1:ELLOE)EM2 L/ '!',*:*:*<<LMM,/ELLOE%EK& L# %9#4)M,=,=,??M',*:*:*<<L-.L\*E=1:EMM,/ELLOE%EK)EM L =>> 788K  	H=>CG	H   	H=>CG	Hs.   H H) 	H&H!!H&)	I2H>>Ic                N   |eg }t        |       D ]S  \  }}t        |t              r(|j                  |j	                  |j                         >|j	                  | d|        U |S t        |      t        |       k7  rt        d      t        |t              st        |      }|S )N_z*arrays and names must have the same length)	enumerater6   r   rp   r9   rQ   AssertionErrorr7   )arrsrH   r   rg   arrs        r@   r   r     s    }o 	.FAs#y)chh.BSXX&xq_-		. L u:T" !MNN%&KELrA   c                   d }t        |       j                  t        |            } ||        ||      z  |z  }t        |       D ci c]  \  }}||v sd| | }}}t        |       D cg c]  \  }}||v rd| n| }}}t        |      D ci c]  \  }}||v sd| | }	}}t        |      D cg c]  \  }}||v rd| n| }
}}|||	|
fS c c}}w c c}}w c c}}w c c}}w )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                J    t               }| D ch c]	  }||vs| c}S c c}w r   )r   )rH   seenrp   s      r@   get_duplicatesz+_build_names_mapper.<locals>.get_duplicatesJ  s$    E!&;$d*:;;;s   	  row_col_)r   intersectionr   )r   r   r   shared_names	dup_namesrg   rp   r   r   r   r   s              r@   r   r   ,  s?   << x=--c(m<Lx(>(+CClRI )2((;$Qty?P$qc
DO  BK8AT6=adi'$qc
T1O 
 )2((;$Qty?P$qc
DO  BK8AT6=adi'$qc
T1O  O_oMMs$   	CC/CC%"C%;C+)
NNNmeanNFTAllFT)r%   r!   r+   r	   r,   r   r-   r   r.   rw   r/   r   r0   r   returnr!   )r%   r!   r+   z!AggFuncTypeBase | AggFuncTypeDictr,   r   r-   r   r.   rw   r/   r   r0   r   r   r!   )Nr   N)r?   zDataFrame | Seriesr%   r!   r.   rw   )r   )r%   r!   r.   rw   )r.   rw   )r?   r!   r.   rw   )NNN)
r%   r!   r(   IndexLabel | Noner)   r   r'   r   r   r!   )NNNNFr   TF)r,   r   r.   rw   r-   r   r   r!   )r?   r!   r,   r   r   r!   )r   )r   rw   )r   	list[str]r   r   r   z;tuple[dict[str, str], list[str], dict[str, str], list[str]])?
__future__r   typingr   r   r   r   r   numpyrX   pandas._typingr	   r
   r   r   pandas.util._decoratorsr   r   r   pandas.util._exceptionsr   pandas.core.dtypes.castr   pandas.core.dtypes.commonr   r   r   r   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   r   r!   r"   r8   rb   ry   r|   r~   r5   r   r   r   r   r    rA   r@   <module>r     s   "    
 4 ; 
 !   * ' 
 . 6 % 
 "#	,}
%q1 
!4:
4:
 4: 4: 4: 4: 4: 4: 4: 2 $4:nX
X
 /X X X X X X XD MM
M M`3. MR=+FI=+B PU#+#+IL#+L "#	,w
+F8D  $!% $	3-
3-3- 3- 	3-
 3- E , $3-r g g g g gV >COO*.OOd"3N3N#,3N@3NrA   