§
    
�’jàX  ã                  óZ  — 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
mZ d dlmZ d dlmZ d dlmc 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„Z  ed¦  «        	 	 	 	 	 	 d)d*d„¦   «         Z! ed¦  «        d+d,d!„¦   «         Z" ed¦  «        	 d-d.d'„¦   «         Z#dS )/é    )ÚannotationsN)ÚTYPE_CHECKING)Ú
set_module)Úis_iteratorÚis_list_like)Úconcat_compat)Únotna)Ú
MultiIndex)Úconcat)Ú
to_numeric)ÚHashable)ÚAnyArrayLike)Ú	DataFrameÚvariableÚstrÚreturnÚlistc                óÄ   — | �]t          | ¦  «        s| gS t          |t          ¦  «        r't          | t          ¦  «        st	          |› d�¦  «        ‚t          | ¦  «        S g S )Nz7 must be a list of tuples when columns are a MultiIndex)r   Ú
isinstancer
   r   Ú
ValueError)Úarg_varsr   Úcolumnss      úFC:\Projects\DSCons\.venv\Lib\site-packages\pandas\core\reshape\melt.pyÚensure_list_varsr      st   € ØÐÝ˜HÑ%Ô%ð 	"Ø�:ÐÝ˜¥Ñ,Ô,ð 	"µZÀÍ$Ñ5OÔ5Oð 	"ÝØÐTÐTÐTñô ð õ ˜‘>”>Ð!àˆ	ó    ÚpandasÚvalueTÚframer   Ú
value_namer   Úignore_indexÚboolc           
     ó
  ‡ — |‰ j         v rt          d|› d�¦  «        ‚t          |d‰ j         ¦  «        }|du}t          |d‰ j         ¦  «        }t          ‰ j                              |¦  «        ¦  «        t          |¦  «        k    rt          d¦  «        ‚|s|rÃ|�‰ j                              |¦  «        }n‰ j         }||z   }	|                     |	¦  «        }
|
dk    }|                     ¦   «         r.d„ t          |	|d	¬
¦  «        D ¦   «         }t          d|› �¦  «        ‚|r$‰ j	        dd…t          j        |
¦  «        f         Š n-‰                      d¬¦  «        Š n‰                      d¬¦  «        Š |�‰ j                              |¦  «        ‰ _         |€¶t          ‰ j         t          ¦  «        r€t          ‰ j         j        ¦  «        t          t!          ‰ j         j        ¦  «        ¦  «        k    r‰ j         j        }�nd„ t#          t          ‰ j         j        ¦  «        ¦  «        D ¦   «         }n×‰ j         j        �‰ j         j        ndg}n»t'          |¦  «        r©t          ‰ j         t          ¦  «        r|t)          |¦  «        rt+          |¦  «        }t          |¦  «        t          ‰ j         ¦  «        k    r8t          d|›dt          |¦  «        › dt          ‰ j         ¦  «        › d�¦  «        ‚nt          d|›d�¦  «        ‚|g}‰ j        \  }}|t          |¦  «        z
  }i }|D ]›}‰                      |¦  «        }t          |j        t2          j        ¦  «        sH|dk    rt5          |g|z  d	¬¦  «        ||<   ŒU t7          |¦  «        g |j        |j        ¬¦  «        ||<   Œ~t3          j        |j        |¦  «        ||<   Œœ||z   |gz   }‰ j        d         dk    r\t          d„ ‰ j        D ¦   «         ¦  «        s>t5          ˆ fd„t#          ‰ j        d         ¦  «        D ¦   «         d	¬¦  «        j        ||<   n‰ j                              d¦  «        ||<   tC          |¦  «        D ]5\  }}‰ j          "                    |¦  «         #                    |¦  «        ||<   Œ6‰  $                    ||¬¦  «        }|sSt3          j        t3          j%        t          ‰ ¦  «        ¦  «        |¦  «        }‰ j&         '                    |¦  «        |_&        |S )a¶  
    Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.

    This function is useful to reshape a DataFrame into a format where one
    or more columns are identifier variables (`id_vars`), while all other
    columns are considered measured variables (`value_vars`), and are "unpivoted" to
    the row axis, leaving just two non-identifier columns, 'variable' and
    'value'.

    Parameters
    ----------
    frame : DataFrame
        The DataFrame to unpivot.
    id_vars : scalar, tuple, list, or ndarray, optional
        Column(s) to use as identifier variables.
    value_vars : scalar, tuple, list, or ndarray, optional
        Column(s) to unpivot. If not specified, uses all columns that
        are not set as `id_vars`.
    var_name : scalar, tuple, list, or ndarray, optional
        Name to use for the 'variable' column. If None it uses
        ``frame.columns.name`` or 'variable'. Must be a scalar if columns are a
        MultiIndex.
    value_name : scalar, default 'value'
        Name to use for the 'value' column, can't be an existing column label.
    col_level : scalar, optional
        If columns are a MultiIndex then use this level to melt.
    ignore_index : bool, default True
        If True, original index is ignored. If False, the original index is retained.
        Index labels will be repeated as necessary.

    Returns
    -------
    DataFrame
        Unpivoted DataFrame.

    See Also
    --------
    DataFrame.melt : Identical method.
    pivot_table : Create a spreadsheet-style pivot table as a DataFrame.
    DataFrame.pivot : Return reshaped DataFrame organized
        by given index / column values.
    DataFrame.explode : Explode a DataFrame from list-like
            columns to long format.

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

    Examples
    --------
    >>> df = pd.DataFrame(
    ...     {
    ...         "A": {0: "a", 1: "b", 2: "c"},
    ...         "B": {0: 1, 1: 3, 2: 5},
    ...         "C": {0: 2, 1: 4, 2: 6},
    ...     }
    ... )
    >>> df
    A  B  C
    0  a  1  2
    1  b  3  4
    2  c  5  6

    >>> pd.melt(df, id_vars=["A"], value_vars=["B"])
    A variable  value
    0  a        B      1
    1  b        B      3
    2  c        B      5

    >>> pd.melt(df, id_vars=["A"], value_vars=["B", "C"])
    A variable  value
    0  a        B      1
    1  b        B      3
    2  c        B      5
    3  a        C      2
    4  b        C      4
    5  c        C      6

    The names of 'variable' and 'value' columns can be customized:

    >>> pd.melt(
    ...     df,
    ...     id_vars=["A"],
    ...     value_vars=["B"],
    ...     var_name="myVarname",
    ...     value_name="myValname",
    ... )
    A myVarname  myValname
    0  a         B          1
    1  b         B          3
    2  c         B          5

    Original index values can be kept around:

    >>> pd.melt(df, id_vars=["A"], value_vars=["B", "C"], ignore_index=False)
    A variable  value
    0  a        B      1
    1  b        B      3
    2  c        B      5
    0  a        C      2
    1  b        C      4
    2  c        C      6

    If you have multi-index columns:

    >>> df.columns = [list("ABC"), list("DEF")]
    >>> df
    A  B  C
    D  E  F
    0  a  1  2
    1  b  3  4
    2  c  5  6

    >>> pd.melt(df, col_level=0, id_vars=["A"], value_vars=["B"])
    A variable  value
    0  a        B      1
    1  b        B      3
    2  c        B      5

    >>> pd.melt(df, id_vars=[("A", "D")], value_vars=[("B", "E")])
    (A, D) variable_0 variable_1  value
    0      a          B          E      1
    1      b          B          E      3
    2      c          B          E      5
    zvalue_name (z3) cannot match an element in the DataFrame columns.Úid_varsNÚ
value_varsz)id_vars cannot contain duplicate columns.éÿÿÿÿc                ó   — g | ]	\  }}|¯|‘Œ
S © r'   )Ú.0ÚlabÚ	not_founds      r   ú
<listcomp>zmelt.<locals>.<listcomp>É   s1   € ð ð ð Ù&˜˜YÈyðØðð ð r   T)ÚstrictzFThe following id_vars or value_vars are not present in the DataFrame: F)Údeepc                ó   — g | ]}d |› �‘ŒS )Ú	variable_r'   )r(   Úis     r   r+   zmelt.<locals>.<listcomp>à   s   € ÐUÐUÐU°˜O¨˜O˜OÐUÐUÐUr   r   z	var_name=z has z, items, but the dataframe columns only have z levels.z must be a scalar.r   )r    )ÚnameÚdtypeé   c              3  óZ   K  — | ]&}t          |t          j        ¦  «         o|j        V — Œ'd S ©N)r   Únpr2   Ú_supports_2d)r(   Údts     r   ú	<genexpr>zmelt.<locals>.<genexpr>  sI   è è € ð &ð &Ø=?�J�r�2œ8Ñ$Ô$Ð$Ð8¨¬ð&ð &ð &ð &ð &ð &r   c                ó2   •— g | ]}‰j         d d …|f         ‘ŒS r5   )Úiloc)r(   r0   r   s     €r   r+   zmelt.<locals>.<listcomp>	  s(   ø€ Ð=Ð=Ð= !ˆUŒZ˜˜˜˜1˜ÔÐ=Ð=Ð=r   ÚF©r   )(r   r   r   ÚlenÚget_indexer_forÚget_level_valuesÚanyÚzipÚKeyErrorr;   ÚalgosÚuniqueÚcopyr   r
   ÚnamesÚsetÚranger1   r   r   r   ÚshapeÚpopr2   r6   r   ÚtypeÚtileÚ_valuesÚdtypesÚvaluesÚravelÚ	enumerateÚ_get_level_valuesÚrepeatÚ_constructorÚarangeÚindexÚtake)r   r#   r$   Úvar_namer   Ú	col_levelr    Úvalue_vars_was_not_noneÚlevelÚlabelsÚidxÚmissingÚmissing_labelsÚnum_rowsÚKÚnum_cols_adjustedÚmdataÚcolÚid_dataÚmcolumnsr0   ÚresultÚtakers   `                      r   Úmeltrj   ,   sp  ø€ ðN �U”]Ð"Ð"Ýð%˜:ð %ð %ð %ñ
ô 
ð 	
õ ˜w¨	°5´=ÑAÔA€GØ(°Ð4ÐÝ! *¨l¸E¼MÑJÔJ€Jõ ˆ5Œ=×(Ò(¨Ñ1Ô1Ñ2Ô2µS¸±\´\ÒAÐAÝÐDÑEÔEÐEàð '�*ð 'ØÐ Ø”M×2Ò2°9Ñ=Ô=ˆEˆEà”MˆEØ˜:Ñ%ˆØ×#Ò# FÑ+Ô+ˆØ˜’)ˆØ�;Š;‰=Œ=ð 	ðð Ý*-¨f°gÀdÐ*KÑ*KÔ*Kðñ ô ˆNõ ð3Ø"0ð3ð 3ñô ð ð #ð 	+Ø”J˜q˜q˜q¥%¤,¨sÑ"3Ô"3Ð3Ô4ˆEˆEà—J’J E�JÑ*Ô*ˆEˆEà—
’
 �
Ñ&Ô&ˆàÐàœ×6Ò6°yÑAÔAˆŒàÐÝ�e”m¥ZÑ0Ô0ð 	Ý�5”=Ô&Ñ'Ô'­3­s°5´=Ô3FÑ/GÔ/GÑ+HÔ+HÒHÐHØ œ=Ô.�‘àUÐUµU½3¸u¼}Ô?RÑ;SÔ;SÑ5TÔ5TÐUÑUÔU��ð ',¤mÔ&8Ð&D�”Ô"Ð"È*ðˆHˆHõ 
�hÑ	Ô	ð Ý�e”m¥ZÑ0Ô0ð 		?Ý˜8Ñ$Ô$ð *Ý ™>œ>�Ý�8‰}Œ}�s 5¤=Ñ1Ô1Ò1Ð1Ý ðX�xð Xð X¥s¨8¡}¤}ð Xð XÝ;>¸u¼}Ñ;MÔ;MðXð Xð Xñô ð ð 2õ Ð= Ð=Ð=Ð=Ñ>Ô>Ð>à�:ˆà”+�K€HˆaØ�C ™LœLÑ(Ðà*,€EØð 
Eð 
EˆØ—)’)˜C‘.”.ˆÝ˜'œ-­¬Ñ2Ô2ð 	Eà  1Ò$Ð$Ý# W IÐ0AÑ$AÐPTÐUÑUÔU��c‘
�
ð +�T '™]œ]¨2°G´LÈÌÐVÑVÔV��c‘
�
åœ ¤Ð2CÑDÔDˆE�#‰JˆJà˜Ñ! Z LÑ0€Hà„{�1„~˜ÒÐ¥#ð &ð &ØCHÄ<ð&ñ &ô &ñ #ô #Ðõ #Ø=Ð=Ð=Ð=¥u¨U¬[¸¬^Ñ'<Ô'<Ð=Ñ=Ô=ÈDð
ñ 
ô 
ä
ð 	ˆjÑÐð "œM×/Ò/°Ñ4Ô4ˆˆjÑÝ˜HÑ%Ô%ð Ið I‰ˆˆ3Ø”]×4Ò4°QÑ7Ô7×>Ò>¸xÑHÔHˆˆc‰
ˆ
à×Ò ¨xÐÑ8Ô8€Fàð /Ý”�œ	¥# e¡*¤*Ñ-Ô-Ð/@ÑAÔAˆØ”{×'Ò'¨Ñ.Ô.ˆŒà€Mr   ÚdataÚgroupsÚdictÚdropnac                óŒ  ‡ ‡— i }g }t          ¦   «         }t          t          t          |                     ¦   «         ¦  «        ¦  «        ¦  «        }|                     ¦   «         D ]q\  }}t          |¦  «        |k    rt          d¦  «        ‚ˆ fd„|D ¦   «         }	t          |	¦  «        ||<   |                     |¦  «         | 	                    |¦  «        }Œrt          ‰ j                             |¦  «        ¦  «        }
|
D ]%}t          j        ‰ |         j        |¦  «        ||<   Œ&|r…t          j        t          ||d                  ¦  «        t"          ¬¦  «        Š|D ]}‰t%          ||         ¦  «        z  ŠŒ‰                     ¦   «         s ˆfd„|                     ¦   «         D ¦   «         }‰                      ||
|z   ¬¦  «        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                ó*   •— g | ]}‰|         j         ‘ŒS r'   )rN   )r(   re   rk   s     €r   r+   zlreshape.<locals>.<listcomp>\  s    ø€ Ð8Ð8Ð8¨3�T˜#”YÔ&Ð8Ð8Ð8r   r   )r2   c                ó(   •— i | ]\  }}||‰         “ŒS r'   r'   )r(   ÚkÚvÚmasks      €r   ú
<dictcomp>zlreshape.<locals>.<dictcomp>k  s#   ø€ Ð:Ð:Ð:¡D A q�Q˜˜$œÐ:Ð:Ð:r   r=   )rH   r>   ÚnextÚiterrP   Úitemsr   r   ÚappendÚunionr   r   Ú
differencer6   rM   rN   Úonesr!   r	   ÚallrU   )rk   rl   rn   rd   Ú
pivot_colsÚall_colsrb   ÚtargetrG   Ú	to_concatÚid_colsre   Úcrt   s   `            @r   Úlreshaper„     s´  øø€ ðx €EØ€JÝ!™eœe€HÝ�D•�f—m’m‘o”oÑ&Ô&Ñ'Ô'Ñ(Ô(€AØŸš™œð )ð )‰ˆ�Ýˆu‰:Œ:˜Š?ˆ?ÝÐCÑDÔDÐDØ8Ð8Ð8Ð8°%Ð8Ñ8Ô8ˆ	å% iÑ0Ô0ˆˆf‰Ø×Ò˜&Ñ!Ô!Ð!Ø—>’> %Ñ(Ô(ˆˆå�4”<×*Ò*¨8Ñ4Ô4Ñ5Ô5€GØð 3ð 3ˆÝ”W˜T #œYÔ.°Ñ2Ô2ˆˆc‰
ˆ
àð ;ÝŒw•s˜5 ¨A¤Ô/Ñ0Ô0½Ð=Ñ=Ô=ˆØð 	$ð 	$ˆAØ•E˜% œ(‘O”OÑ#ˆDˆDØ�xŠx‰zŒzð 	;Ø:Ð:Ð:Ð:¨E¯KªK©M¬MÐ:Ñ:Ô:ˆEà×Ò˜U¨G°jÑ,@ÐÑAÔAÐAr   Ú ú\d+ÚdfÚsepÚsuffixc                ó€  — dd„}dd„}t          |¦  «        s|g}nt          |¦  «        }| j                             |¦  «                             ¦   «         rt          d¦  «        ‚t          |¦  «        s|g}nt          |¦  «        }| |                              ¦   «                              ¦   «         rt          d¦  «        ‚g }g }	|D ]H}
 || |
||¦  «        }|	                     |¦  «         |                      || |
||||¦  «        ¦  «         ŒIt          |d	¬
¦  «        }| j         
                    |	¦  «        }| |         }t          |¦  «        d	k    r(|                     |¦  «                             |¦  «        S |                     |                     ¦   «         |¬¦  «                             g |¢|‘¦  «        S )aV!  
    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: 0.7},
    ...         "B1980": {0: 3.2, 1: 1.3, 2: 0.1},
    ...         "X": dict(zip(range(3), np.random.randn(3), strict=True)),
    ...     }
    ... )
    >>> 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
    >>> long_format = pd.wide_to_long(df, stubnames="ht", i=["famid", "birth"], j="age")
    >>> long_format
    ... # 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`

    >>> wide_format = long_format.unstack()
    >>> wide_format.columns = wide_format.columns.map("{0[0]}{0[1]}".format)
    >>> wide_format.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

    >>> long_format = pd.wide_to_long(
    ...     df, stubnames="ht", i=["famid", "birth"], j="age", sep="_", suffix=r"\w+"
    ... )
    >>> long_format
    ... # 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
    Ústubr   rˆ   r‰   c                ó²   — dt          j        |¦  «        › t          j        |¦  «        › |› d�}| j        | j        j                             |¦  «                 S )Nú^ú$)ÚreÚescaper   r   Úmatch)r‡   r‹   rˆ   r‰   Úregexs        r   Úget_var_namesz#wide_to_long.<locals>.get_var_nameso  sK   € Ø?•R”Y˜t‘_”_Ð?¥b¤i°¡n¤nÐ?°fÐ?Ð?Ð?ˆØŒz˜"œ*œ.×.Ò.¨uÑ5Ô5Ô6Ð6r   c                óf  — t          | |||                     |¦  «        |¬¦  «        }||         j                             t	          j        ||z   ¦  «        dd¬¦  «        ||<   	 t          ||         ¦  «        ||<   n# t          t          t          f$ r Y nw xY w| 
                    g |¢|‘¦  «        S )N)r#   r$   r   rY   r…   T)r’   )rj   Úrstripr   Úreplacer�   r�   r   Ú	TypeErrorr   ÚOverflowErrorÚ	set_index)r‡   r‹   r0   Újr$   rˆ   Únewdfs          r   Ú	melt_stubzwide_to_long.<locals>.melt_stubs  sÄ   € ÝØØØ!Ø—{’{ 3Ñ'Ô'Øð
ñ 
ô 
ˆð ˜”8”<×'Ò'­¬	°$¸±*Ñ(=Ô(=¸rÈÐ'ÑNÔNˆˆa‰ð	Ý! %¨¤(Ñ+Ô+ˆE�!‰HˆHøÝ�:¥}Ð5ð 	ð 	ð 	àˆDð	øøøð �Š˜w ˜w A˜wÑ'Ô'Ð's   Á$A= Á=BÂBz,stubname can't be identical to a column namez3the id variables need to uniquely identify each rowr3   )Úaxis)ÚonN)r‹   r   rˆ   r   r‰   r   )r‹   r   rˆ   r   )r   r   r   ÚisinrA   r   Ú
duplicatedÚextendry   r   r{   r>   r™   ÚjoinÚmergeÚreset_index)r‡   Ú	stubnamesr0   rš   rˆ   r‰   r“   rœ   Ú_meltedÚvalue_vars_flattenedr‹   Ú	value_varÚmeltedr#   Únews                  r   Úwide_to_longr«   p  sß  € ð~7ð 7ð 7ð 7ð(ð (ð (ð (õ& ˜	Ñ"Ô"ð $Ø�Kˆ	ˆ	å˜‘O”Oˆ	à	„z‡‚�yÑ!Ô!×%Ò%Ñ'Ô'ð IÝÐGÑHÔHÐHå˜‰?Œ?ð ØˆCˆˆå�‰GŒGˆà	ˆ!„u×ÒÑÔ×ÒÑÔð PÝÐNÑOÔOÐOà€GØÐØð Bð BˆØ!�M " d¨C°Ñ8Ô8ˆ	Ø×#Ò# IÑ.Ô.Ð.Ø�Š�y�y  T¨1¨a°¸CÑ@Ô@ÑAÔAÐAÐAå�G !Ð$Ñ$Ô$€FØŒj×#Ò#Ð$8Ñ9Ô9€GØ
ˆWŒ+€Cå
ˆ1�v„v�‚{€{Ø�}Š}˜QÑÔ×$Ò$ VÑ,Ô,Ð,à�yŠy˜×+Ò+Ñ-Ô-°!ˆyÑ4Ô4×>Ò>¸wÀ¸wÀA¸wÑGÔGÐGr   )r   r   r   r   )NNNr   NT)r   r   r   r   r    r!   r   r   )T)rk   r   rl   rm   rn   r!   r   r   )r…   r†   )r‡   r   rˆ   r   r‰   r   r   r   )$Ú
__future__r   r�   Útypingr   Únumpyr6   Úpandas.util._decoratorsr   Úpandas.core.dtypes.commonr   r   Úpandas.core.dtypes.concatr   Úpandas.core.dtypes.missingr	   Úpandas.core.algorithmsÚcoreÚ
algorithmsrD   Úpandas.core.indexes.apir
   Úpandas.core.reshape.concatr   Úpandas.core.tools.numericr   Úcollections.abcr   Úpandas._typingr   r   r   r   rj   r„   r«   r'   r   r   ú<module>r»      s  ðØ "Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø  Ð  Ð  Ð  Ð  Ð  à Ð Ð Ð à .Ð .Ð .Ð .Ð .Ð .ðð ð ð ð ð ð ð ð 4Ð 3Ð 3Ð 3Ð 3Ð 3Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,à &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ð &Ø .Ð .Ð .Ð .Ð .Ð .Ø -Ð -Ð -Ð -Ð -Ð -Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0àð !Ø(Ð(Ð(Ð(Ð(Ð(à+Ð+Ð+Ð+Ð+Ð+à Ð Ð Ð Ð Ð ðð ð ð ð €ˆHÑÔð ØØØ"ØØðið ið ið iñ ÔðiðX €ˆHÑÔðSBð SBð SBð SBñ ÔðSBðl €ˆHÑÔàAGðsHð sHð sHð sHñ ÔðsHð sHð sHr   