HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/reshape/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/reshape/__pycache__/pivot.cpython-36.pyc
3

���h>\�@s�ddlmZmZmZmZmZmZmZmZm	Z	ddl
Zddlm
Z
ddlmZmZddlmZddlmZmZmZddlmZmZddljjZddlmZdd	l m!Z!dd
l"m#Z#m$Z$m%Z%ddl&m'Z'ddl(m)Z)dd
l*m+Z+er�ddl,m-Z-ed�eeddd�d4dd�dd���Z.d5ed6e/d�dd�Z0d7e/d�dd �Z1d8e/d�d!d"�Z2d9de/d�d#d$�Z3d%d&�Z4ed�eed'dd�d:deee
ee
feee
ee
feee
ee
fdd(�d)d'���Z5d;e/e6dd*�d+d,�Z7d<e6d-�d.d/�Z8d=e/d1�d2d3�Z9dS)>�)	�
TYPE_CHECKING�Callable�Dict�List�Optional�Sequence�Tuple�Union�castN)�Label)�Appender�Substitution)�maybe_downcast_to_dtype)�is_integer_dtype�is_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�meanFT�Allr)�returnc
Cst|�}t|�}t|t�rvg}
g}xD|D]<}t||||||||||	d�
}
|
j|
�|jt|d|��q(Wt|
|dd�S||}|dk	}|�r>t|�r�d}t|�}n
d}|g}x|D]}||kr�t|��q�Wg}xP||D]D}t|t	�r�|j
}y||k�r|j|�Wq�tk
�rYq�Xq�Wt|�t|j
�k�r�||}nJ|j
}x:|D]2}y|j|�}Wntttfk
�rxYnX�qJWt|�}|j||	d�}|j|�}|�r$t|t��r$t|j
��r$|jdd	�}xX|D]P}||k�r�t||��r�||k�r�t||��r�t||||j�||<�q�W|}
|
jjdk�r�|�r�|jjdt|��}g}xRtt|�t|��D]<}|jj|}|dk�s�||k�r�|j|�n
|j|��qhW|j|�}
|�s$t|
jt��r�tjt|
jj�|
jjd
�}|
j |dd�}
t|
j
t��r$tjt|
j
j�|
j
jd
�}|
j |dd�}
t|
t��r<|
j!dd�}
|dk	�rf|
j"|d
d�}|dk	�sbt#�|}
|�r�|�r�||j$�j%dd�}t&|
||||||||d�	}
|�r�|�r�|
j'�r�|
j
jdk�r�|
|d}
t|�dk�r�t|�dk�r�|
j(}
t|
t��r|�r|
jddd�}
|
S)N)	�values�index�columns�
fill_value�aggfunc�margins�dropna�margins_name�observed�__name__r)�keys�axisTF)r+�all)�how)�namesr)r.Zinfer)Zdowncast)�rows�colsr'r+r*r&)r0r.))�_convert_by�
isinstance�listr�append�getattrrr�KeyErrorr�key�	TypeError�lenr%Zdrop�
ValueError�groupby�aggrr)rr�dtyper$Znlevelsr1�range�unstackr�from_arraysrZlevels�reindexZ
sort_index�fillna�AssertionErrorZnotnar/�_add_margins�empty�T)�datar#r$r%r'r&r(r)r*r+�piecesr-�func�tableZ
values_passedZvalues_multi�iZ	to_filter�xr:ZgroupedZagged�vZindex_namesZ
to_unstack�name�mZ_table�rS�;/tmp/pip-build-5_djhm0z/pandas/pandas/core/reshape/pivot.pyr$s�
















r)rMr*c	CsHt|t�std��d|�d�}	x(|jjD]}
||jj|
�kr(t|	��q(Wt||||�}|jdkr�x0|jjdd�D]}
||jj|
�krrt|	��qrWt	|�dkr�|fdt	|�d}n|}|r�t|t
�r�|jt|||i��S|�rt
||||||||�}
t|
t��s|
S|
\}}}n>t|t��s(t�t|||||||�}
t|
t��sL|
S|
\}}}|j|j|d�}x8|D]0}t|t��r�||||<n||d||<�qlWdd	lm}|||gd
�j}|jj}yJx:t|j�D],}|j|g�j}||jt|fd�||<�q�W|j|�}Wn,tk
�r:|jj�|_|j|�}YnX||j_|S)
Nz&margins_name argument must be a stringzConflicting name "z" in margins�r�)r&r)r)r%)�args)rV)r5�strr=r$r1Zget_level_values�_compute_grand_margin�ndimr%r<rr7r�_generate_marginal_results�tuplerrF�)_generate_marginal_results_without_valuesrD�pandasrrI�setZdtypesZ
select_dtypes�applyrr;�_to_safe_for_reshape)rMrJr#r2r3r'r+r*r&�msg�level�grand_marginr:Zmarginal_result_set�result�margin_keys�
row_margin�krZmargin_dummyZ	row_namesr@rSrSrTrG�s^



rG)r*cCs�|r�i}x�||j�D]�\}}ylt|t�r<t||��||<nLt|t�r|t||t�rjt|||��||<q�|||�||<n||�||<Wqtk
r�YqXqW|S|||j�iSdS)N)�itemsr5rXr8�dictr;r$)rJr#r'r*rdrhrPrSrSrTrY s


rYcst��dk�r�g}g}	��fdd�}
t|�dkr�|||j||d�j|�}d}x�|jd||d�D]x\}
}|
|
�}|j�}y||
||<Wn:tk
r�|j|j|�j�|dd�||
||<YnX|j|�|	j|�qbWn�dd	l	m
}d}x||jd||d�D]h\}
}t��dk�r"|
|
�}n�}|j|�||j|��j}t
|g|jjd
�|_|j|�|	j|��qWt||d�}t|�dk�r�|Sn
|}|j}	t��dk�r�|�|j�|d�j|�}|j�}t��gttt����}|jj|�|_nttj|jd�}||	|fS)
Nrcs|�fdt��dS)NrVr)rV)r<)r:)r3r*rSrT�_all_key>sz,_generate_marginal_results.<locals>._all_key)r+r)rcr.r+T)r.Zinplace)r)rQ)r.)r$)r<r>r?�copyr;Zset_axisZ	_get_axisrar7r^rr`rIrr$rQrr%�stackr6rAZreorder_levelsr�np�nan)rMrJr#r2r3r'r+r*Ztable_piecesrfrk�marginZcat_axisr:Zpiece�all_keyrZtransformed_piecerergZ	new_orderrS)r3r*rTr[6sV



r[c
s�t��dkr�g}��fdd�}t|�dkr`||j||d�j|�}	|�}
|	||
<|}|j|
�q�|jdd|d�j|�}	|�}
|	||
<|}|j|
�|Sn
|}|j}t��r�|�j�|d�j|�}nttj|jd�}|||fS)Nrcs&t��dkr�S�fdt��dS)NrrV)rV)r<rS)r3r*rSrTrk�sz;_generate_marginal_results_without_values.<locals>._all_key)r+)rcr.r+)r$)r<r>r`r7r%rrnro)
rMrJr2r3r'r+r*rfrkrprqrergrS)r3r*rTr]�s*
r]cCsH|dkrg}n6t|�s4t|tjtttf�s4t|d�r<|g}nt|�}|S)N�__call__)	rr5rnZndarrayrrr�hasattrr6)ZbyrSrSrTr4�s
r4�pivot)rJr$r%r#r"cs|dkrtd��tj|�}|dkr\|dk	r6tj|�}ng}|j|�|dk}�j||d�}n�|dkrzt�j�jjd�g}ntj|�}�fdd�|D�}�fdd�|D�}|j|�tj	|�}t
|�r�t|t�r�t
tt|�}�j�|j||d�}n�j�|j|d�}|j|�S)	Nz.pivot() missing 1 required argument: 'columns')r7)rQcsg|]}�|�qSrSrS)�.0�idx)rJrSrT�
<listcomp>�szpivot.<locals>.<listcomp>csg|]}�|�qSrSrS)ru�col)rJrSrTrw�s)r$r%)r$)r;�comZconvert_to_list_like�extendZ	set_indexrr$rQrrCrr5r\r
rrZ_constructorZ_valuesZ_constructor_slicedrB)rJr$r%r#r3r7ZindexedZdata_columnsrS)rJrTrt�s.




)r*r)r"c
CsTtj|�}tj|�}t||dd�}t||dd�}d}
dd�||D�}|rXt|ddd	�}
i}|jt||��|jt||��|dkr�|dk	r�td
��|dk	r�|dkr�td��dd
lm}
|
||
d�}|j	}|dkr�d|d<t
dd�}n||d<d|i}|jdgf|||||d�|��}|j�s6|j	j
|�d}||}|	dk	�rPt||	||d�}|S)ac

    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.

    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
    �row)�prefixrxNcSsg|]}t|ttf�r|�qSrS)r5rr)rurOrSrSrTrwNszcrosstab.<locals>.<listcomp>TF)Z	intersect�sortz&aggfunc cannot be used without values.z)values cannot be used without an aggfunc.r)r)r$�	__dummy__)r'r&r')r$r%r(r*r))�	normalizer(r*)ryZmaybe_make_list�
_get_namesr�update�zipr=r^rr%r<rrH�
difference�
_normalize)r$r%r#ZrownamesZcolnamesr'r(r*r)rZ
common_idxZ	pass_objsrJrZdfZoriginal_df_cols�kwargsrMZ	cols_diffrSrSrT�crosstab�sLg


r�)r(cCsTt|ttf�sRddd�}y||}Wn,tk
rP}ztd�|�WYdd}~XnX|dkr�dd�dd�d	d�d
�}|d|d<y||}Wn,tk
r�}ztd�|�WYdd}~XnX||�}|jd
�}�n�|dk�rH|j}|j}	||jddd�fj	k||jdd�dfj	kB�r$t|�d���|jdd�df}
|jddd�f}|jdd�dd�f}t
||dd�}|dk�r�|
|
j�}
t||
gdd�}|jd
�}|	|_n�|dk�r�||j�}|j
|�}|jd
�}||_np|dk�s�|dk�r>|
|
j�}
||j�}d|j|<t||
gdd�}|j
|�}|jd
�}||_|	|_ntd��ntd��|S)Nr$r%)rrzNot a valid normalize argumentFcSs||jdd�jdd�S)Nr)r.r)�sum)rOrSrSrT�<lambda>�sz_normalize.<locals>.<lambda>cSs||j�S)N)r�)rOrSrSrTr��scSs|j|jdd�dd�S)Nr)r.r)�divr�)rOrSrSrTr��s)r/r%r$r/Trrz not in pivoted DataFrame)rr()r.zNot a valid margins argument���r�r�r�r�r�r�r�)r5�boolrXr9r=rEr$r%ZilocrQr�r�rr7�loc)rMrr(r*Z	axis_subs�errZnormalizers�fZtable_indexZ
table_columnsZ
column_marginZindex_marginrSrSrTr��sb










r�r{)r|cCs�|dkrZg}xvt|�D]>\}}t|t�r@|jdk	r@|j|j�q|j|�d|���qWn*t|�t|�krrtd��t|t�s�t|�}|S)N�_z*arrays and names must have the same length)�	enumerater5rrQr7r<rFr6)Zarrsr1r|rNZarrrSrSrTr��s
r�)	NNNr NFTr!F)Nr!N)rr)r!)r!)r!)NNN)NNNNFr!TF)r!)r{):Ztypingrrrrrrrr	r
ZnumpyrnZpandas._typingrZpandas.util._decoratorsrr
Zpandas.core.dtypes.castrZpandas.core.dtypes.commonrrrZpandas.core.dtypes.genericrrZpandas.core.common�core�commonryZpandas.core.framerZpandas.core.groupbyrZpandas.core.indexes.apirrrZpandas.core.reshape.concatrZpandas.core.reshape.utilrZpandas.core.seriesrr^rrrXrGrYr[r]r4rtr�r�r�r�rSrSrSrT�<module>sj,!MJ%:(Q