
    Pi                    6   d dl mZ d dlZd dlmZmZmZ d dlZd dl	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c 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% erd dl&m'Z'm(Z( d dl)m*Z*m+Z+m,Z,m-Z-m.Z. d dl/m0Z0  ed          	 	 	 	 	 	 	 	 	 	 dAdBd"            Z1dCd$Z2	 	 	 dDdEd'Z3	 dFdGd(Z4	 	 dHdId)Z5	 	 dHdJd*Z6d+ Z7 ed          e
j8        e
j8        d,dKd2            Z9 ed          	 	 	 	 	 	 	 	 dLdMd5            Z:	 dFdNd6Z;dOdPd;Z<dQd@Z=dS )R    )annotationsN)TYPE_CHECKINGLiteralcast)lib)
set_module)maybe_downcast_to_dtype)is_list_likeis_nested_list_like	is_scalar)ExtensionDtype)ABCDataFrame	ABCSeries)Grouper)Index
MultiIndexget_objs_combined_axis)concat)Series)CallableHashable)AggFuncTypeAggFuncTypeBaseAggFuncTypeDict
IndexLabelSequenceNotStr	DataFramepandasmeanFTAlldatar   aggfuncr   marginsbooldropnamargins_namer   observedsortreturnc                   t          |          }t          |          }t          |t                    rg }g }|D ]V}t          | |||||||||	|
|          }|                    |           |                    t          |d|                     Wt          ||d          }|                    | d          S t          | |||||||||	|
|          }|                    | d          S )a!  
    Create a spreadsheet-style pivot table as a DataFrame.

    The levels in the pivot table will be stored in MultiIndex objects
    (hierarchical indexes) on the index and columns of the result DataFrame.

    Parameters
    ----------
    data : DataFrame
        Input pandas DataFrame object.
    values : list-like or scalar, optional
        Column or columns to aggregate.
    index : column, Grouper, array, or sequence of the previous
        Keys to group by on the pivot table index. If a list is passed,
        it can contain any of the other types (except list). If an array is
        passed, it must be the same length as the data and will be used in
        the same manner as column values.
    columns : column, Grouper, array, or sequence of the previous
        Keys to group by on the pivot table column. If a list is passed,
        it can contain any of the other types (except list). If an array is
        passed, it must be the same length as the data and will be used in
        the same manner as column values.
    aggfunc : function, list of functions, dict, default "mean"
        If a list of functions is passed, the resulting pivot table will have
        hierarchical columns whose top level are the function names
        (inferred from the function objects themselves).
        If a dict is passed, the key is column to aggregate and the value is
        function or list of functions. If ``margins=True``, aggfunc will be
        used to calculate the partial aggregates.
    fill_value : scalar, default None
        Value to replace missing values with (in the resulting pivot table,
        after aggregation).
    margins : bool, default False
        If ``margins=True``, special ``All`` columns and rows
        will be added with partial group aggregates across the categories
        on the rows and columns.
    dropna : bool, default True
        Do not include columns whose entries are all NaN. If True,

        * rows with an NA value in any column will be omitted before computing margins,
        * index/column keys containing NA values will be dropped (see ``dropna``
          parameter in :meth:``DataFrame.groupby``).

    margins_name : str, default 'All'
        Name of the row / column that will contain the totals
        when margins is True.
    observed : bool, default False
        This only applies if any of the groupers are Categoricals.
        If True: only show observed values for categorical groupers.
        If False: show all values for categorical groupers.

        .. versionchanged:: 3.0.0

            The default value is now ``True``.

    sort : bool, default True
        Specifies if the result should be sorted.

    **kwargs : dict
        Optional keyword arguments to pass to ``aggfunc``.

        .. versionadded:: 3.0.0

    Returns
    -------
    DataFrame
        An Excel style pivot table.

    See Also
    --------
    DataFrame.pivot : Pivot without aggregation that can handle
        non-numeric data.
    DataFrame.melt: Unpivot a DataFrame from wide to long format,
        optionally leaving identifiers set.
    wide_to_long : Wide panel to long format. Less flexible but more
        user-friendly than melt.

    Notes
    -----
    Reference :ref:`the user guide <reshaping.pivot>` for more examples.

    Examples
    --------
    >>> df = pd.DataFrame(
    ...     {
    ...         "A": ["foo", "foo", "foo", "foo", "foo", "bar", "bar", "bar", "bar"],
    ...         "B": ["one", "one", "one", "two", "two", "one", "one", "two", "two"],
    ...         "C": [
    ...             "small",
    ...             "large",
    ...             "large",
    ...             "small",
    ...             "small",
    ...             "large",
    ...             "small",
    ...             "small",
    ...             "large",
    ...         ],
    ...         "D": [1, 2, 2, 3, 3, 4, 5, 6, 7],
    ...         "E": [2, 4, 5, 5, 6, 6, 8, 9, 9],
    ...     }
    ... )
    >>> df
         A    B      C  D  E
    0  foo  one  small  1  2
    1  foo  one  large  2  4
    2  foo  one  large  2  5
    3  foo  two  small  3  5
    4  foo  two  small  3  6
    5  bar  one  large  4  6
    6  bar  one  small  5  8
    7  bar  two  small  6  9
    8  bar  two  large  7  9

    This first example aggregates values by taking the sum.

    >>> table = pd.pivot_table(
    ...     df, values="D", index=["A", "B"], columns=["C"], aggfunc="sum"
    ... )
    >>> table
    C        large  small
    A   B
    bar one    4.0    5.0
        two    7.0    6.0
    foo one    4.0    1.0
        two    NaN    6.0

    We can also fill missing values using the `fill_value` parameter.

    >>> table = pd.pivot_table(
    ...     df, values="D", index=["A", "B"], columns=["C"], aggfunc="sum", fill_value=0
    ... )
    >>> table
    C        large  small
    A   B
    bar one      4      5
        two      7      6
    foo one      4      1
        two      0      6

    The next example aggregates by taking the mean across multiple columns.

    >>> table = pd.pivot_table(
    ...     df, values=["D", "E"], index=["A", "C"], aggfunc={"D": "mean", "E": "mean"}
    ... )
    >>> table
                    D         E
    A   C
    bar large  5.500000  7.500000
        small  5.500000  8.500000
    foo large  2.000000  4.500000
        small  2.333333  4.333333

    We can also calculate multiple types of aggregations for any given
    value column.

    >>> table = pd.pivot_table(
    ...     df,
    ...     values=["D", "E"],
    ...     index=["A", "C"],
    ...     aggfunc={"D": "mean", "E": ["min", "max", "mean"]},
    ... )
    >>> table
                      D   E
                   mean max      mean  min
    A   C
    bar large  5.500000   9  7.500000    6
        small  5.500000   9  8.500000    8
    foo large  2.000000   5  4.500000    4
        small  2.333333   6  4.333333    2
    )valuesindexcolumns
fill_valuer#   r$   r&   r'   r(   r)   kwargs__name__   )keysaxispivot_table)method)_convert_by
isinstancelist__internal_pivot_tableappendgetattrr   __finalize__)r"   r,   r-   r.   r#   r/   r$   r&   r'   r(   r)   r0   piecesr3   func_tabletables                    m/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/pandas/core/reshape/pivot.pyr5   r5   6   s2   t E'""G'4   >"$ 	9 	9D+%)!  F MM&!!!KKj$778888vDq111!!$}!===" E d=999    !AggFuncTypeBase | AggFuncTypeDictc                   ||z   }|du}|rt          |          rd}t          |          }nd}|g}|D ]}|| vrt          |          g }||z   D ]H}t          |t                    r|j        }	 || v r|                    |           9# t          $ r Y Ew xY wt          |          t          | j	                  k     r| |         } nN| j	        }|D ]5}	 |
                    |          }# t          t          t          f$ r Y 2w xY wt          |          }|                     ||	|
|          }|r||         } |j        |fi |}|r?t          |t                    r*t          |j	                  r|                    d          }|}|j        j        dk    r|r|j        j        dt          |                   }g }t'          t          |          t          |                    D ]E}|j        j        |         }|||v r|                    |           0|                    |           F|                    ||          }|st          |j        t*                    rBt+          j        |j        j        |j        j        	          }|                    |d
|          }t          |j	        t*                    rBt+          j        |j	        j        |j	        j        	          }|                    |d|          }|
du r+t          |t                    r|                    d          }|S|                    |          }|t          u r5|	s3t7          j        |          r|                    t<          j                  }|rJ|r.| |                                  !                    d                   } tE          || |||||||||          }|r1|s/|j	        j        dk    r|j	        #                    d
          |_	        t          |          d
k    rt          |          d
k    r|j$        }t          |t                    r|r|                    dd          }|S )zL
    Helper of :func:`pandas.pivot_table` for any non-list ``aggfunc``.
    NTF)r(   r)   r&   all)howr2   r/   namesr   )r4   r/   r4   )rowscolsr#   r0   r(   r'   r/   r&   )rG   r4   )%r
   r9   KeyErrorr8   r   keyr;   	TypeErrorlenr.   drop
ValueErrorgroupbyaggr   r&   r-   nlevelsrJ   rangeunstackr   from_productlevelsreindex
sort_indexfillnar   
is_integerastypenpint64notnarF   _add_margins	droplevelT)r"   r,   r-   r.   r#   r/   r$   r&   r'   r(   r)   r0   r3   values_passedvalues_multii	to_filterxrO   groupedaggedrA   index_names
to_unstacknamems                             rB   r:   r:     s   " 7?D$&M   	L&\\FF LXF  	" 	"A}}qkk!  	 	 	A!W%% E99$$Q'''   y>>C----	?D  	 	CS))z84   fll4(flMMG " &/GK**6**E (*UL11 (c%-6H6H (''E {Q5 k'#e**5
s5zz3t99-- 	( 	(A;$Q'D|t{22!!!$$$$!!$''''jZ@@ Dek:.. 	D'(:%+BSTTTAMM!!
MCCEemZ00 	D'(<EMDWXXXAMM!!
MCCEt||
5,77|  a ((Z((c>>(>s~j/I/I> LL**E 
 	2

((a(001D%!
 
 
  3\ 3em.Ca.G.G//22
5zzQ3w<<!++ %&& 06 0Q//Ls$   0B


BBC**DDrA   DataFrame | Seriesc                   t          |t                    st          d          d| d}| j        j        D ]-}|| j                            |          v rt          |          .t          |||||          }| j        dk    rB| j        j        dd          D ]-}|| j                            |          v rt          |          .t          |          dk    r|fdt          |          dz
  z  z   }n|}|sEt          | t                    r0|                     |                     |||         i                    S |r6t          | |||||||||

  
        }t          |t                    s|S |\  }}}nKt          | t                    sJ t!          | ||||||||
	  	        }t          |t                    s|S |\  }}}|                    |j        |	          }|D ]4}t          |t                    r||         ||<   #||d                  ||<   5dd	lm}  ||t)          |g          
          j        }|j        j        }t-          |j                  D ]Y}t          |t0                    r|                    |g          j        }||                             t6          |f          ||<   Zt9          ||g          }||j        _        |S )Nz&margins_name argument must be a stringzConflicting name "z" in margins   r2    rH   r   r   )r.   )args)r8   strrS   r-   rJ   get_level_values_compute_grand_marginndimr.   rQ   r   _append_internal_constructor_generate_marginal_resultstupler   )_generate_marginal_results_without_valuesr[   r   r   r   re   setdtypesr   select_dtypesapplyr	   r   )rA   r"   r,   rL   rM   r#   r0   r(   r'   r/   r&   msglevelgrand_marginrO   marginal_result_setresultmargin_keys
row_marginkr   margin_dummy	row_namesdtypes                           rB   rc   rc     sX    lC(( CABBB
9|
9
9
9C" " "5;77>>>>S//! ? )vwUULzQ](, 	& 	&Eu}==eDDDD oo% E 4yy1}}oTQ 77 >j	22 > %%\,%?@AA
 
 	
 
 >8
 
 -u55 	'&&*='ZZ %.....G4tWfhf
 
 -u55 	'&&*='Z##FNz#JJJ / /a 	/(OJqMM(1.JqMM      9Zu>>>@L"I V]## 
 
e^,, 	##UG,,4)$/55#5( 6 
 
T V\*++F"FLMrC   c                   |ri }| |                                          D ]\  }}	 t          |t                    r t          ||          di |||<   not          |t                    rNt          ||         t                    r  t          |||                   di |||<   n ||         |fi |||<   n ||fi |||<   # t
          $ r Y w xY w|S | || j        fi |iS )N )itemsr8   rw   r<   dictrP   r-   )r"   r,   r#   r0   r'   r   r   vs           rB   ry   ry     sD     =L&&(( 	 	DAqgs++ ;&9ga&9&9&C&CF&C&CLOO.. ;!'!*c22 B*@'!WQZ*@*@*J*J6*J*JQ*4'!*Q*A*A&*A*AQ&-ga&:&:6&:&:LO   ggdj;;F;;<<s   BC
CCc
                f   t                    dk    r(g }
g }fd}t          |          dk    r |||z                                |||	          j        |fi |}d}| j                            d|          D ]L\  }}|j        } ||          }||         ||<   |
                    |           |                    |           MnF |d d         |z                                d d         ||	          j        |fi |j        }d}|                     d|          D ]\  }}t                    dk    r ||          }n}|
                    |           ||                                         j        }t          |j        t                    r+t          j	        |gg |j        j
        d           |_        n!t          |g|j        j                  |_        |
                    |           |                    |           |
s| S t          |
|          }t          |          dk    r|S n	| }| j        }t                    dk    r ||z                                ||	          j        |fi |                                t!          j        t                    gt%          t                                        }fd	|D             }j                            |          _        n&|                    t*          j        |j        
          ||fS )Nr   c                8    | fdt                    dz
  z  z   S )Nrt   r2   rQ   )rO   rM   r'   s    rB   _all_keyz,_generate_marginal_results.<locals>._all_key*  s"    &#d))a-)@@@rC   r(   r&   r2   )r   r(   rI   ro   rK   c                4    g | ]}j         j        |         S r   )r-   rJ   ).0rh   r   s     rB   
<listcomp>z._generate_marginal_results.<locals>.<listcomp>p  s$    PPP:+1!4PPPrC   r-   )rQ   rT   rU   re   r;   to_framer8   r-   r   from_tuplesrJ   r   ro   r   r.   stack	itertoolschainrW   reorder_levels_constructor_slicedr`   nan)rA   r"   r,   rL   rM   r#   r0   r(   r'   r&   table_piecesr   r   margincat_axisrO   pieceall_keytransformed_piecer   new_order_indicesnew_order_namesr   s       `   `             @rB   r}   r}     s    4yy1}}	A 	A 	A 	A 	A 	A t99q==TF]#@@W( ( &( ( 
 H#gooAoII , ,
U"(3--!'g##E***""7++++,T"1"X&'bqbHVDDW( ( &( ( 	  H#mm!hmGG , ,
Ut99q==&hsmmGG*G##E***$*3K$8$8$:$:$<!ek:66 V.8.D 	8 1848/ / /%++
 /4WIEKDT.U.U.U%+ ##$5666""7++++ 	9LLx888Ft99>>M  m
4yy1}}DWTHVW<<$ $"$ $ 	
  %%''
 &OSYYKs4yy9I9IJJPPPP>OPPP%+::?KK
--bfFN-KK
;
**rC   c	                l   t                    dk    rg }	fd}
t          |          dk    rQ |                    |||          |         j        |fi |} |
            }|| |<   | }|	                    |           nU |                    d||          j        |fi |} |
            }|| |<   | }|	                    |           |S | }| j        }	t                    r+ |                    ||                   j        |fi |}n t          t          j        |j                  }||	|fS )Nr   c                 `    t                     dk    rS fdt                     dz
  z  z   S )Nr2   rt   r   )rM   r'   s   rB   r   z;_generate_marginal_results_without_values.<locals>._all_key  s4    4yyA~~## ?Uc$ii!m%<<<rC   r   )r   r(   r&   r   )rQ   rT   r   r;   r.   r   r`   r   )rA   r"   rL   rM   r#   r0   r(   r'   r&   r   r   r   r   r   r   s      `   `       rB   r   r   x  s    4yy1}}	= 	= 	= 	= 	= 	=
 t99q==UT\\$&\II$OU ! F hjjG#E'NFw'''' ST\\HV\LLR ! F hjjG#E'NFw'''Mm
4yy :UT\\$&\II$OU
 

 


 BF&.999
;
**rC   c                    | g } n^t          |           s<t          | t          j        t          t
          t          f          st          |           r| g} nt          |           } | S N)	r   r8   r`   ndarrayr   r   r   callabler9   )bys    rB   r7   r7     sd    	z"b2:uiABB B<<
 T"XXIrC   )r-   r,   r.   r   r-   IndexLabel | lib.NoDefaultr,   c                   t          j        |          }t          d  j        j        D                       r6                     d           d  j        j        D              j        _        |t          j        u rO|t          j        urt          j        |          }ng }|t          j        u }                     ||z   |          }nE|t          j        u rht           j        t                    r& fdt           j        j                  D             }nH                      j         j        j                  g}n  fdt          j        |          D             } fd	|D             }	|                    |	           t          j        |          }
t#          |          rGt          |t$                    s2                      |         j        |
t+          d
|                    }n"                      |         j        |
          }t+          d|                    |                    }d |j        j        D             |j        _        |S )a(  
    Return reshaped DataFrame organized by given index / column values.

    Reshape data (produce a "pivot" table) based on column values. Uses
    unique values from specified `index` / `columns` to form axes of the
    resulting DataFrame. This function does not support data
    aggregation, multiple values will result in a MultiIndex in the
    columns. See the :ref:`User Guide <reshaping>` for more on reshaping.

    Parameters
    ----------
    data : DataFrame
        Input pandas DataFrame object.
    columns : Hashable or a sequence of the previous
        Column to use to make new frame's columns.
    index : Hashable or a sequence of the previous, optional
        Column to use to make new frame's index. If not given, uses existing index.
    values : Hashable or a sequence of the previous, optional
        Column(s) to use for populating new frame's values. If not
        specified, all remaining columns will be used and the result will
        have hierarchically indexed columns.

    Returns
    -------
    DataFrame
        Returns reshaped DataFrame.

    Raises
    ------
    ValueError:
        When there are any `index`, `columns` combinations with multiple
        values. `DataFrame.pivot_table` when you need to aggregate.

    See Also
    --------
    DataFrame.pivot_table : Generalization of pivot that can handle
        duplicate values for one index/column pair.
    DataFrame.unstack : Pivot based on the index values instead of a
        column.
    wide_to_long : Wide panel to long format. Less flexible but more
        user-friendly than melt.

    Notes
    -----
    For finer-tuned control, see hierarchical indexing documentation along
    with the related stack/unstack methods.

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

    Examples
    --------
    >>> df = pd.DataFrame(
    ...     {
    ...         "foo": ["one", "one", "one", "two", "two", "two"],
    ...         "bar": ["A", "B", "C", "A", "B", "C"],
    ...         "baz": [1, 2, 3, 4, 5, 6],
    ...         "zoo": ["x", "y", "z", "q", "w", "t"],
    ...     }
    ... )
    >>> df
        foo   bar  baz  zoo
    0   one   A    1    x
    1   one   B    2    y
    2   one   C    3    z
    3   two   A    4    q
    4   two   B    5    w
    5   two   C    6    t

    >>> df.pivot(index="foo", columns="bar", values="baz")
    bar  A   B   C
    foo
    one  1   2   3
    two  4   5   6

    >>> df.pivot(index="foo", columns="bar")["baz"]
    bar  A   B   C
    foo
    one  1   2   3
    two  4   5   6

    >>> df.pivot(index="foo", columns="bar", values=["baz", "zoo"])
          baz       zoo
    bar   A  B  C   A  B  C
    foo
    one   1  2  3   x  y  z
    two   4  5  6   q  w  t

    You could also assign a list of column names or a list of index names.

    >>> df = pd.DataFrame(
    ...     {
    ...         "lev1": [1, 1, 1, 2, 2, 2],
    ...         "lev2": [1, 1, 2, 1, 1, 2],
    ...         "lev3": [1, 2, 1, 2, 1, 2],
    ...         "lev4": [1, 2, 3, 4, 5, 6],
    ...         "values": [0, 1, 2, 3, 4, 5],
    ...     }
    ... )
    >>> df
        lev1 lev2 lev3 lev4 values
    0   1    1    1    1    0
    1   1    1    2    2    1
    2   1    2    1    3    2
    3   2    1    2    4    3
    4   2    1    1    5    4
    5   2    2    2    6    5

    >>> df.pivot(index="lev1", columns=["lev2", "lev3"], values="values")
    lev2    1         2
    lev3    1    2    1    2
    lev1
    1     0.0  1.0  2.0  NaN
    2     4.0  3.0  NaN  5.0

    >>> df.pivot(index=["lev1", "lev2"], columns=["lev3"], values="values")
          lev3    1    2
    lev1  lev2
       1     1  0.0  1.0
             2  2.0  NaN
       2     1  4.0  3.0
             2  NaN  5.0

    A ValueError is raised if there are any duplicates.

    >>> df = pd.DataFrame(
    ...     {
    ...         "foo": ["one", "one", "two", "two"],
    ...         "bar": ["A", "A", "B", "C"],
    ...         "baz": [1, 2, 3, 4],
    ...     }
    ... )
    >>> df
       foo bar  baz
    0  one   A    1
    1  one   A    2
    2  two   B    3
    3  two   C    4

    Notice that the first two rows are the same for our `index`
    and `columns` arguments.

    >>> df.pivot(index="foo", columns="bar", values="baz")
    Traceback (most recent call last):
       ...
    ValueError: Index contains duplicate entries, cannot reshape
    c              3     K   | ]}|d u V  	d S r   r   r   ro   s     rB   	<genexpr>zpivot.<locals>.<genexpr>Z  s&      
5
5D44<
5
5
5
5
5
5rC   F)deepc                .    g | ]}||nt           j        S r   r   
no_defaultr   s     rB   r   zpivot.<locals>.<listcomp>\  s0     
 
 
=AD$DD#.
 
 
rC   )r;   c                D    g | ]}j                             |          S r   )r-   rx   )r   rh   r"   s     rB   r   zpivot.<locals>.<listcomp>s  s6       78DJ//22  rC   r   c                     g | ]
}|         S r   r   )r   idxr"   s     rB   r   zpivot.<locals>.<listcomp>{  s    OOO$s)OOOrC   c                     g | ]
}|         S r   r   )r   colr"   s     rB   r   zpivot.<locals>.<listcomp>}  s    >>>cS	>>>rC   r   )r-   r.   r   r   c                2    g | ]}|t           j        ur|nd S r   r   r   s     rB   r   zpivot.<locals>.<listcomp>  s5       9=CN**  rC   )comconvert_to_list_likeanyr-   rJ   copyr   r   	set_indexr8   r   rW   rV   r   ro   extendfrom_arraysr
   r~   r|   _valuesr   rX   )r"   r.   r-   r,   columns_listlikerM   r;   indexed
index_listdata_columns
multiindexr   s   `           rB   pivotr     s   t /88
 
5
5DJ$4
5
5
555 
yyey$$
 
EIZEU
 
 


 &&+E22DDD#.( ..## ! 
 
 CN""$*j11    <A$*BT<U<U  


 ,,TZdjo,NN

 POOOs/G/N/NOOOJ>>>>-=>>>,'''+J77
 	W
65(A(A 	W''V$ -v66 (  GG ..tF|/C:.VVG
 +w/?@@AAF AGAS  FL MrC   	normalize/bool | Literal[0, 1, 'all', 'index', 'columns']c
           
        ||t          d          ||t          d          t          |           s| g} t          |          s|g}d}
d | |z   D             }|rt          |dd          }
t          | |d	          }t          ||d
	          }t	          ||          \  }}}}ddlm} i t          t          || d                    t          t          ||d                    } |||
          }|d|d<   t          dd}n	||d<   d|i} |j
        	 d||||||d|}|	durt          ||	||          }|                    |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                J    g | ] }t          |t          t          f          |!S r   )r8   r   r   )r   rj   s     rB   r   zcrosstab.<locals>.<listcomp>6  s,    XXXqz!i=V/W/WXXXXrC   TF)	intersectr)   row)prefixr   r   r   )strictr   	__dummy__)r#   r/   r#   )r-   r.   r$   r'   r&   r(   )r   r$   r'   )r-   r4   r2   )r.   r4   )r   )rS   r   r   
_get_names_build_names_mapperr   r   r   ziprQ   r5   
_normalizerename_axis)r-   r.   r,   rownamescolnamesr#   r$   r'   r&   r   
common_idx	pass_objsrownames_mapperunique_rownamescolnames_mapperunique_colnamesr   r"   dfr0   rA   s                        rB   crosstabr     s$   h ~'-ABBBgoDEEEu%% w'' )JXXEGOXXXI S+IERRR
%%888H'8E:::H 	Hh// !     
s?E$777
8
8
s?GD999
:
:D 
4z	*	*	*B~; 22 ;W% BN	!	 	 	 	E Yl
 
 
 O!<<EoA>>ELrC   c                   t          |t          t          f          s1ddd}	 ||         }n"# t          $ r}t	          d          |d }~ww xY w|du rbd d d d	}|d
         |d<   	 ||         }n"# t          $ r}t	          d          |d }~ww xY w ||           } |                     d          } n|du r| j        }| j        }	| j        dd d f         j	        }
||
v||
k    z  rt	          | d          | j        d ddf         }| j        dd df         }| j        d dd df         } t          | |d          } |dk    rG||                                z  }t          | |gd          } |                     d          } |	| _        n|dk    rK||                                z  }|                     |d          } |                     d          } || _        n|d
k    s|du r||                                z  }||                                z  }d|j        |<   t          | |gd          } |                     |d          } |                     d          } || _        |	| _        nt	          d          t	          d          | S )Nr-   r.   )r   r2   zNot a valid normalize argumentFc                \    | |                      d                               d          z  S Nr2   rK   r   sumrj   s    rB   <lambda>z_normalize.<locals>.<lambda>z  s(    QA!2!2!2!:!:: rC   c                0    | |                                  z  S r   r   r   s    rB   r   z_normalize.<locals>.<lambda>{  s    QUUWW rC   c                X    |                      |                     d          d          S r   )divr   r   s    rB   r   z_normalize.<locals>.<lambda>|  s"    quuQUUU]]u;; rC   )rF   r.   r-   rF   Tr   z not in pivoted DataFrame)r   r$   r2   rK   )ignore_indexzNot a valid margins argument)r8   r%   rw   rN   rS   r]   r-   r.   ilocro   r   r   r   r{   loc)rA   r   r$   r'   	axis_subserrnormalizersftable_indextable_columnslast_ind_or_colcolumn_marginindex_margins                rB   r   r   m  s/    i$-- HI..		H!),II 	H 	H 	H=>>CG	H % ;:,,;;3
 3
 (.D	HI&AA 	H 	H 	H=>>CG	H %Q	Dk*RU+0 /LO4ST 	IGGGHHH
3B37+z"crc'* 
3B38$ 5IuEEE 	!!)M,=,=,?,??ME=1:::ELLOOE)EMM'!!',*:*:*<*<<L**<d*KKELLOOE%EKK%9#4#4)M,=,=,?,??M',*:*:*<*<<L-.L\*E=1:::E**<d*KKELLOOE%EK)EMM =>>> 7888Ls,   , 
AAA'A0 0
B:B

Br   r   rw   r9   c                   |ig }t          |           D ]V\  }}t          |t                    r"|j        |                    |j                   <|                    | d|            WnSt          |          t          |           k    rt          d          t          |t                    st          |          }|S )N_z*arrays and names must have the same length)	enumerater8   r   ro   r;   rQ   AssertionErrorr9   )arrsrJ   r   rh   arrs        rB   r   r     s    }oo 	. 	.FAs#y)) .ch.BSX&&&&____----		. u::T"" !MNNN%&& 	 KKELrC   r   	list[str]r   ;tuple[dict[str, str], list[str], dict[str, str], list[str]]c                &   t          |           t          |          z  fdt          |           D             }fdt          |           D             }fdt          |          D             }f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                *    i | ]\  }}|v 	d | |S row_r   r   rh   ro   	dup_namess      rB   
<dictcomp>z'_build_names_mapper.<locals>.<dictcomp>  6       $Qty?P?P
q

D?P?P?PrC   c                ,    g | ]\  }}|v rd | n|S r  r   r  s      rB   r   z'_build_names_mapper.<locals>.<listcomp>  >       6=adi''
q


T  rC   c                *    i | ]\  }}|v 	d | |S col_r   r  s      rB   r  z'_build_names_mapper.<locals>.<dictcomp>  r  rC   c                ,    g | ]\  }}|v rd | n|S r  r   r  s      rB   r   z'_build_names_mapper.<locals>.<listcomp>  r  rC   )r   r  )r   r   r   r   r   r   r  s         @rB   r   r     s    : HH-I   (1((;(;  O   AJ8ATAT  O   (1((;(;  O   AJ8ATAT  O O_oMMrC   )
NNNr    NFTr!   TT)r"   r   r#   r   r$   r%   r&   r%   r'   r   r(   r%   r)   r%   r*   r   )r"   r   r#   rD   r$   r%   r&   r%   r'   r   r(   r%   r)   r%   r*   r   )r!   NT)
rA   rq   r"   r   r(   r%   r'   r   r&   r%   )r!   )r"   r   r'   r   )r!   T)r"   r   r(   r%   r'   r   r&   r%   )
rA   r   r"   r   r(   r%   r'   r   r&   r%   )
r"   r   r.   r   r-   r   r,   r   r*   r   )NNNNFr!   TF)
r$   r%   r'   r   r&   r%   r   r   r*   r   )rA   r   r$   r%   r'   r   r*   r   )r   )r   rw   r*   r9   )r   r  r   r  r*   r  )>
__future__r   r   typingr   r   r   numpyr`   pandas._libsr   pandas.util._decoratorsr   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.groupbyr   pandas.core.indexes.apir   r   r   pandas.core.reshape.concatr   pandas.core.seriesr   collections.abcr   r   pandas._typingr   r   r   r   r   r   r   r5   r:   rc   ry   r}   r   r7   r   r   r   r   r   r   r   rC   rB   <module>r)     s%   " " " " " "                        . . . . . . ; ; ; ; ; ;         
 5 4 4 4 4 4       
 !                 ' ' ' ' ' '         
 . - - - - - % % % % % % !       
              !      H 
!"b: b: b: b: b:J@ @ @ @X #_ _ _ _ _F HM= = = = =@ #]+ ]+ ]+ ]+ ]+P #2+ 2+ 2+ 2+ 2+j   H
 ),),W W W W W Wt H "AFS S S S Sn JOM M M M M`    "-N -N -N -N -N -NrC   