a
    DBìjøC  ã                   @  s:  d dl mZ d dlZd dl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 d dlm  mZ d dlmZ d d	lmZ d d
lmZ d dlmZ d dlmZ erÎd dlmZ d dlm Z  d dl!m"Z" dddœdd„Z#eed dddœ ƒd)dddddœdd„ƒZ$d*dd ddd!œd"d#„Z%d+ddddd&œd'd(„Z&dS ),é    )ÚannotationsN)ÚTYPE_CHECKING)ÚAppender)Úis_list_like)Úconcat_compat)Únotna)Ú
MultiIndex)Úconcat)Útile_compat)Ú_shared_docs)Ú
to_numeric)ÚHashable)ÚAnyArrayLike)Ú	DataFrameÚstrÚlist)ÚvariableÚreturnc                 C  sL   | d urDt | ƒs| gS t|tƒr:t| tƒs:t|› d�ƒ‚qHt| ƒS ng S d S )Nz7 must be a list of tuples when columns are a MultiIndex)r   Ú
isinstancer   r   Ú
ValueError)Zarg_varsr   Úcolumns© r   úW/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/pandas/core/reshape/melt.pyÚensure_list_vars   s    ÿ
r   Úmeltzpd.melt(df, zDataFrame.melt)ZcallerÚotherÚvalueTr   r   Úbool)ÚframeÚ
value_nameÚignore_indexr   c                   sÔ  |ˆ j v rtd|› d�ƒ‚t|dˆ j ƒ}|d u}t|dˆ j ƒ}|sF|rÎ|d ur\ˆ j  |¡}nˆ j }|| }	| |	¡}
|
dk}| ¡ r¦dd„ t|	|ƒD ƒ}td|› �ƒ‚|rÄˆ jd d …t	 
|
¡f ‰ qÖˆ  ¡ ‰ nˆ  ¡ ‰ |d urìˆ j  |¡ˆ _ |d u �rftˆ j tƒ�rHtˆ j jƒttˆ j jƒƒk�r,ˆ j j}nd	d„ ttˆ j jƒƒD ƒ}nˆ j jd u�r^ˆ j jnd
g}n"t|ƒ�r‚td|›d�ƒ‚n|g}ˆ j\}}|t|ƒ }i }|D ]p}ˆ  |¡}t|jtjƒ�s|dk�ræt|g| dd�||< nt|ƒg |j|jd�||< nt |j|¡||< �q¦|| |g }ˆ jd dk�rttdd„ ˆ jD ƒƒ�stt‡ fdd„tˆ jd ƒD ƒƒj||< nˆ j d¡||< t|ƒD ] \}}ˆ j   |¡ !|¡||< �qŒˆ j"||d�}|�sÐt#ˆ j$|ƒ|_$|S )Nzvalue_name (z3) cannot match an element in the DataFrame columns.Úid_varsÚ
value_varséÿÿÿÿc                 S  s   g | ]\}}|r|‘qS r   r   )Ú.0ZlabÚ	not_foundr   r   r   Ú
<listcomp>G   s   zmelt.<locals>.<listcomp>zFThe following id_vars or value_vars are not present in the DataFrame: c                 S  s   g | ]}d |› �‘qS )Z	variable_r   ©r$   Úir   r   r   r&   ^   ó    r   z	var_name=z must be a scalar.r   T)r    )ÚnameÚdtypeé   c                 s  s"   | ]}t |tjƒ o|jV  qd S ©N)r   Únpr+   Z_supports_2d)r$   Údtr   r   r   Ú	<genexpr>z   s   zmelt.<locals>.<genexpr>c                   s   g | ]}ˆ j d d …|f ‘qS r-   )Úilocr'   ©r   r   r   r&   ~   r)   ÚF©r   )%r   r   r   Zget_level_valuesZget_indexer_forÚanyÚzipÚKeyErrorr1   ÚalgosÚuniqueÚcopyr   r   ÚlenÚnamesÚsetÚranger*   r   ÚshapeÚpopr+   r.   r	   ÚtypeÚtileÚ_valuesZdtypesÚvaluesZravelÚ	enumerateZ_get_level_valuesÚrepeatÚ_constructorr
   Úindex)r   r!   r"   Úvar_namer   Z	col_levelr    Zvalue_vars_was_not_noneÚlevelÚlabelsÚidxÚmissingZmissing_labelsZnum_rowsÚKZnum_cols_adjustedÚmdataÚcolZid_dataZmcolumnsr(   Úresultr   r2   r   r   +   s~    


ÿ
ÿÿÿ


ÿ



ÿ
ÿÚdict)ÚdataÚgroupsÚdropnar   c                   s  i }g }t ƒ }ttt| ¡ ƒƒƒ}| ¡ D ]N\}}t|ƒ|krFtdƒ‚‡ fdd„|D ƒ}	t|	ƒ||< | |¡ | 	|¡}q*t
ˆ j |¡ƒ}
|
D ]}t ˆ | j|¡||< qŽ|�rtjt||d  ƒtd�‰|D ]}ˆt|| ƒM ‰qÎˆ ¡ �s‡fdd„| ¡ D ƒ}ˆ j||
| d�S )	aÔ  
    Reshape wide-format data to long. Generalized inverse of DataFrame.pivot.

    Accepts a dictionary, ``groups``, in which each key is a new column name
    and each value is a list of old column names that will be "melted" under
    the new column name as part of the reshape.

    Parameters
    ----------
    data : DataFrame
        The wide-format DataFrame.
    groups : dict
        {new_name : list_of_columns}.
    dropna : bool, default True
        Do not include columns whose entries are all NaN.

    Returns
    -------
    DataFrame
        Reshaped DataFrame.

    See Also
    --------
    melt : Unpivot a DataFrame from wide to long format, optionally leaving
        identifiers set.
    pivot : Create a spreadsheet-style pivot table as a DataFrame.
    DataFrame.pivot : Pivot without aggregation that can handle
        non-numeric data.
    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.

    Examples
    --------
    >>> data = pd.DataFrame({'hr1': [514, 573], 'hr2': [545, 526],
    ...                      'team': ['Red Sox', 'Yankees'],
    ...                      'year1': [2007, 2007], 'year2': [2008, 2008]})
    >>> data
       hr1  hr2     team  year1  year2
    0  514  545  Red Sox   2007   2008
    1  573  526  Yankees   2007   2008

    >>> pd.lreshape(data, {'year': ['year1', 'year2'], 'hr': ['hr1', 'hr2']})
          team  year   hr
    0  Red Sox  2007  514
    1  Yankees  2007  573
    2  Red Sox  2008  545
    3  Yankees  2008  526
    z$All column lists must be same lengthc                   s   g | ]}ˆ | j ‘qS r   )rC   )r$   rP   )rS   r   r   r&   É   r)   zlreshape.<locals>.<listcomp>r   )r+   c                   s   i | ]\}}||ˆ  “qS r   r   )r$   ÚkÚv)Úmaskr   r   Ú
<dictcomp>Ø   r)   zlreshape.<locals>.<dictcomp>r4   )r=   r;   ÚnextÚiterrD   Úitemsr   r   ÚappendÚunionr   r   Ú
differencer.   rB   rC   Zonesr   r   ÚallrG   )rS   rT   rU   rO   Z
pivot_colsZall_colsrN   Útargetr<   Z	to_concatZid_colsrP   Úcr   )rS   rX   r   Úlreshape�   s*    5

rc   Ú ú\d+)ÚdfÚsepÚsuffixr   c              
   C  s$  ddddœdd„}dddœdd„}t |ƒs2|g}nt|ƒ}| j |¡ ¡ rRtdƒ‚t |ƒsb|g}nt|ƒ}| |  ¡  ¡ r‚td	ƒ‚g }g }	|D ]4}
|| |
||ƒ}|	 |¡ | || |
||||ƒ¡ qŽt	|d
d�}| j 
|	¡}| | }t|ƒd
k�r| |¡ |¡S |j| ¡ |d� ||g ¡S dS )ax   
    Unpivot a DataFrame from wide to long format.

    Less flexible but more user-friendly than melt.

    With stubnames ['A', 'B'], this function expects to find one or more
    group of columns with format
    A-suffix1, A-suffix2,..., B-suffix1, B-suffix2,...
    You specify what you want to call this suffix in the resulting long format
    with `j` (for example `j='year'`)

    Each row of these wide variables are assumed to be uniquely identified by
    `i` (can be a single column name or a list of column names)

    All remaining variables in the data frame are left intact.

    Parameters
    ----------
    df : DataFrame
        The wide-format DataFrame.
    stubnames : str or list-like
        The stub name(s). The wide format variables are assumed to
        start with the stub names.
    i : str or list-like
        Column(s) to use as id variable(s).
    j : str
        The name of the sub-observation variable. What you wish to name your
        suffix in the long format.
    sep : str, default ""
        A character indicating the separation of the variable names
        in the wide format, to be stripped from the names in the long format.
        For example, if your column names are A-suffix1, A-suffix2, you
        can strip the hyphen by specifying `sep='-'`.
    suffix : str, default '\\d+'
        A regular expression capturing the wanted suffixes. '\\d+' captures
        numeric suffixes. Suffixes with no numbers could be specified with the
        negated character class '\\D+'. You can also further disambiguate
        suffixes, for example, if your wide variables are of the form A-one,
        B-two,.., and you have an unrelated column A-rating, you can ignore the
        last one by specifying `suffix='(!?one|two)'`. When all suffixes are
        numeric, they are cast to int64/float64.

    Returns
    -------
    DataFrame
        A DataFrame that contains each stub name as a variable, with new index
        (i, j).

    See Also
    --------
    melt : Unpivot a DataFrame from wide to long format, optionally leaving
        identifiers set.
    pivot : Create a spreadsheet-style pivot table as a DataFrame.
    DataFrame.pivot : Pivot without aggregation that can handle
        non-numeric data.
    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.

    Notes
    -----
    All extra variables are left untouched. This simply uses
    `pandas.melt` under the hood, but is hard-coded to "do the right thing"
    in a typical case.

    Examples
    --------
    >>> np.random.seed(123)
    >>> df = pd.DataFrame({"A1970" : {0 : "a", 1 : "b", 2 : "c"},
    ...                    "A1980" : {0 : "d", 1 : "e", 2 : "f"},
    ...                    "B1970" : {0 : 2.5, 1 : 1.2, 2 : .7},
    ...                    "B1980" : {0 : 3.2, 1 : 1.3, 2 : .1},
    ...                    "X"     : dict(zip(range(3), np.random.randn(3)))
    ...                   })
    >>> df["id"] = df.index
    >>> df
      A1970 A1980  B1970  B1980         X  id
    0     a     d    2.5    3.2 -1.085631   0
    1     b     e    1.2    1.3  0.997345   1
    2     c     f    0.7    0.1  0.282978   2
    >>> pd.wide_to_long(df, ["A", "B"], i="id", j="year")
    ... # doctest: +NORMALIZE_WHITESPACE
                    X  A    B
    id year
    0  1970 -1.085631  a  2.5
    1  1970  0.997345  b  1.2
    2  1970  0.282978  c  0.7
    0  1980 -1.085631  d  3.2
    1  1980  0.997345  e  1.3
    2  1980  0.282978  f  0.1

    With multiple id columns

    >>> df = pd.DataFrame({
    ...     'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],
    ...     'birth': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    ...     'ht1': [2.8, 2.9, 2.2, 2, 1.8, 1.9, 2.2, 2.3, 2.1],
    ...     'ht2': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
    ... })
    >>> df
       famid  birth  ht1  ht2
    0      1      1  2.8  3.4
    1      1      2  2.9  3.8
    2      1      3  2.2  2.9
    3      2      1  2.0  3.2
    4      2      2  1.8  2.8
    5      2      3  1.9  2.4
    6      3      1  2.2  3.3
    7      3      2  2.3  3.4
    8      3      3  2.1  2.9
    >>> l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age')
    >>> l
    ... # doctest: +NORMALIZE_WHITESPACE
                      ht
    famid birth age
    1     1     1    2.8
                2    3.4
          2     1    2.9
                2    3.8
          3     1    2.2
                2    2.9
    2     1     1    2.0
                2    3.2
          2     1    1.8
                2    2.8
          3     1    1.9
                2    2.4
    3     1     1    2.2
                2    3.3
          2     1    2.3
                2    3.4
          3     1    2.1
                2    2.9

    Going from long back to wide just takes some creative use of `unstack`

    >>> w = l.unstack()
    >>> w.columns = w.columns.map('{0[0]}{0[1]}'.format)
    >>> w.reset_index()
       famid  birth  ht1  ht2
    0      1      1  2.8  3.4
    1      1      2  2.9  3.8
    2      1      3  2.2  2.9
    3      2      1  2.0  3.2
    4      2      2  1.8  2.8
    5      2      3  1.9  2.4
    6      3      1  2.2  3.3
    7      3      2  2.3  3.4
    8      3      3  2.1  2.9

    Less wieldy column names are also handled

    >>> np.random.seed(0)
    >>> df = pd.DataFrame({'A(weekly)-2010': np.random.rand(3),
    ...                    'A(weekly)-2011': np.random.rand(3),
    ...                    'B(weekly)-2010': np.random.rand(3),
    ...                    'B(weekly)-2011': np.random.rand(3),
    ...                    'X' : np.random.randint(3, size=3)})
    >>> df['id'] = df.index
    >>> df # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
       A(weekly)-2010  A(weekly)-2011  B(weekly)-2010  B(weekly)-2011  X  id
    0        0.548814        0.544883        0.437587        0.383442  0   0
    1        0.715189        0.423655        0.891773        0.791725  1   1
    2        0.602763        0.645894        0.963663        0.528895  1   2

    >>> pd.wide_to_long(df, ['A(weekly)', 'B(weekly)'], i='id',
    ...                 j='year', sep='-')
    ... # doctest: +NORMALIZE_WHITESPACE
             X  A(weekly)  B(weekly)
    id year
    0  2010  0   0.548814   0.437587
    1  2010  1   0.715189   0.891773
    2  2010  1   0.602763   0.963663
    0  2011  0   0.544883   0.383442
    1  2011  1   0.423655   0.791725
    2  2011  1   0.645894   0.528895

    If we have many columns, we could also use a regex to find our
    stubnames and pass that list on to wide_to_long

    >>> stubnames = sorted(
    ...     set([match[0] for match in df.columns.str.findall(
    ...         r'[A-B]\(.*\)').values if match != []])
    ... )
    >>> list(stubnames)
    ['A(weekly)', 'B(weekly)']

    All of the above examples have integers as suffixes. It is possible to
    have non-integers as suffixes.

    >>> df = pd.DataFrame({
    ...     'famid': [1, 1, 1, 2, 2, 2, 3, 3, 3],
    ...     'birth': [1, 2, 3, 1, 2, 3, 1, 2, 3],
    ...     'ht_one': [2.8, 2.9, 2.2, 2, 1.8, 1.9, 2.2, 2.3, 2.1],
    ...     'ht_two': [3.4, 3.8, 2.9, 3.2, 2.8, 2.4, 3.3, 3.4, 2.9]
    ... })
    >>> df
       famid  birth  ht_one  ht_two
    0      1      1     2.8     3.4
    1      1      2     2.9     3.8
    2      1      3     2.2     2.9
    3      2      1     2.0     3.2
    4      2      2     1.8     2.8
    5      2      3     1.9     2.4
    6      3      1     2.2     3.3
    7      3      2     2.3     3.4
    8      3      3     2.1     2.9

    >>> l = pd.wide_to_long(df, stubnames='ht', i=['famid', 'birth'], j='age',
    ...                     sep='_', suffix=r'\w+')
    >>> l
    ... # doctest: +NORMALIZE_WHITESPACE
                      ht
    famid birth age
    1     1     one  2.8
                two  3.4
          2     one  2.9
                two  3.8
          3     one  2.2
                two  2.9
    2     1     one  2.0
                two  3.2
          2     one  1.8
                two  2.8
          3     one  1.9
                two  2.4
    3     1     one  2.2
                two  3.3
          2     one  2.3
                two  3.4
          3     one  2.1
                two  2.9
    r   )Ústubrg   rh   c                 S  s4   dt  |¡› t  |¡› |› d�}| j| jj |¡ S )Nú^ú$)ÚreÚescaper   r   Úmatch)rf   ri   rg   rh   Úregexr   r   r   Úget_var_namesË  s     z#wide_to_long.<locals>.get_var_names)ri   rg   c              
   S  sz   t | ||| |¡|d�}|| jjt || ¡ddd�||< zt|| ƒ||< W n ttt	fyh   Y n0 | 
||g ¡S )N)r!   r"   r   rI   rd   T)ro   )r   Úrstripr   Úreplacerl   rm   r   Ú	TypeErrorr   ÚOverflowErrorÚ	set_index)rf   ri   r(   Újr"   rg   Znewdfr   r   r   Ú	melt_stubÏ  s    û$zwide_to_long.<locals>.melt_stubz,stubname can't be identical to a column namez3the id variables need to uniquely identify each rowr,   )Zaxis)ÚonN)r   r   r   Úisinr5   r   Z
duplicatedÚextendr]   r	   r_   r;   ru   ÚjoinÚmergeZreset_index)rf   Z	stubnamesr(   rv   rg   rh   rp   rw   Z_meltedZvalue_vars_flattenedri   Z	value_varZmeltedr!   Únewr   r   r   Úwide_to_longÝ   s2     o
r~   )NNNr   NT)T)rd   re   )'Ú
__future__r   rl   Útypingr   Únumpyr.   Zpandas.util._decoratorsr   Zpandas.core.dtypes.commonr   Zpandas.core.dtypes.concatr   Zpandas.core.dtypes.missingr   Zpandas.core.algorithmsÚcoreZ
algorithmsr8   Zpandas.core.indexes.apir   Zpandas.core.reshape.concatr	   Zpandas.core.reshape.utilr
   Zpandas.core.shared_docsr   Zpandas.core.tools.numericr   Úcollections.abcr   Zpandas._typingr   Zpandasr   r   r   rc   r~   r   r   r   r   Ú<module>   s:         ùaQ ÿ