Ë
    ¤ehøC  ã                  óJ  — 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c 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„Z# eed   dddœz  «      	 	 	 	 	 	 d	 	 	 	 	 	 	 dd„«       Z$ddd„Z%	 d	 	 	 	 	 	 	 dd„Z&y)é    )ÚannotationsN)ÚTYPE_CHECKING)ÚAppender)Úis_list_like)Úconcat_compat)Únotna)Ú
MultiIndex)Úconcat)Útile_compat)Ú_shared_docs)Ú
to_numeric)ÚHashable)ÚAnyArrayLike)Ú	DataFramec                ó˜   — | �Gt        | «      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	   ÚlistÚ
ValueError)Úarg_varsÚvariableÚcolumnss      úV/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/pandas/core/reshape/melt.pyÚensure_list_varsr      sT   € ØÐÜ˜HÔ%Ø�:ÐÜ˜¤Ô,´ZÀÌ$Ô5OÜØ�*ÐSÐTóð ô ˜“>Ð!àˆ	ó    Úmeltzpd.melt(df, zDataFrame.melt)ÚcallerÚotherc                ó\  — || j                   v rt        d|› d�«      ‚t        |d| j                   «      }|d u}t        |d| j                   «      }|s|r¾|�| j                   j                  |«      }n| j                   }||z   }	|j	                  |	«      }
|
dk(  }|j                  «       r/t        |	|«      D ��cg c]
  \  }}|sŒ	|‘Œ }}}t        d|› �«      ‚|r'| j                  d d …t        j                  |
«      f   } n!| j                  «       } n| j                  «       } |� | j                   j                  |«      | _         |€ât        | j                   t        «      r˜t        | j                   j                  «      t        t!        | j                   j                  «      «      k(  r| j                   j                  }n†t#        t        | j                   j                  «      «      D �cg c]  }d|› �‘Œ	 }}nM| j                   j$                  �| j                   j$                  ndg}nt'        |«      rt        d	|›d
�«      ‚|g}| j(                  \  }}|t        |«      z
  }i }|D ]   }| j+                  |«      }t        |j,                  t.        j,                  «      sF|dkD  rt1        |g|z  d¬«      ||<   ŒR t3        |«      g |j$                  |j,                  ¬«      ||<   Œ~t/        j4                  |j6                  |«      ||<   Œ¢ ||z   |gz   }| j(                  d   dkD  rjt        d„ | j8                  D «       «      sNt1        t#        | j(                  d   «      D �cg c]  }| j                  d d …|f   ‘Œ c}«      j:                  ||<   n| j6                  j=                  d«      ||<   t?        |«      D ]2  \  }}| j                   jA                  |«      jC                  |«      ||<   Œ4 | jE                  ||¬«      }|stG        | jH                  |«      |_$        |S c c}}w c c}w c c}w )Nzvalue_name (z3) cannot match an element in the DataFrame columns.Úid_varsÚ
value_varséÿÿÿÿzFThe following id_vars or value_vars are not present in the DataFrame: Ú	variable_r   z	var_name=z must be a scalar.r   T)Úignore_index)ÚnameÚdtypeé   c              3  ón   K  — | ]-  }t        |t        j                  «       xr |j                  –— Œ/ y ­w)N)r   Únpr%   Ú_supports_2d)Ú.0Údts     r   ú	<genexpr>zmelt.<locals>.<genexpr>z   s/   è ø€ ò &Ø=?ŒJ�rœ2Ÿ8™8Ó$Ð$Ò8¨¯©Ó8ñ&ùs   ‚35ÚF©r   )%r   r   r   Úget_level_valuesÚget_indexer_forÚanyÚzipÚKeyErrorÚilocÚalgosÚuniqueÚcopyr   r	   ÚlenÚnamesÚsetÚranger$   r   ÚshapeÚpopr%   r(   r
   ÚtypeÚtileÚ_valuesÚdtypesÚvaluesÚravelÚ	enumerateÚ_get_level_valuesÚrepeatÚ_constructorr   Úindex)Úframer   r    Úvar_nameÚ
value_nameÚ	col_levelr#   Úvalue_vars_was_not_noneÚlevelÚlabelsÚidxÚmissingÚlabÚ	not_foundÚmissing_labelsÚiÚnum_rowsÚKÚnum_cols_adjustedÚmdataÚcolÚid_dataÚmcolumnsÚresults                           r   r   r   +   sÙ  € ð �U—]‘]Ñ"ÜØ˜:˜,ð '%ð %ó
ð 	
ô ˜w¨	°5·=±=ÓA€GØ(°Ð4ÐÜ! *¨l¸E¿M¹MÓJ€Já‘*ØÐ Ø—M‘M×2Ñ2°9Ó=‰Eà—M‘MˆEØ˜:Ñ%ˆØ×#Ñ# FÓ+ˆØ˜‘)ˆØ�;‰;Œ=ä*-¨f°gÓ*>÷Ù&˜˜YÂ)’ðˆNñ ô ð"Ø"0Ð!1ð3óð ñ #Ø—J‘Jšq¤%§,¡,¨sÓ"3Ð3Ñ4‰Eà—J‘J“L‰Eà—
‘
“ˆàÐàŸ™×6Ñ6°yÓAˆŒàÐÜ�e—m‘m¤ZÔ0Ü�5—=‘=×&Ñ&Ó'¬3¬s°5·=±=×3FÑ3FÓ/GÓ+HÒHØ Ÿ=™=×.Ñ.‘ä5:¼3¸u¿}¹}×?RÑ?RÓ;SÓ5TÖU°˜i¨ sšOÐU�ÑUð ',§m¡m×&8Ñ&8Ð&D�—‘×"Ò"È*ð‰Hô 
�hÔ	Ü˜I˜H˜;Ð&8Ð9Ó:Ð:à�:ˆà—+‘+�K€HˆaØœC ›LÑ(Ðà*,€EØò 
EˆØ—)‘)˜C“.ˆÜ˜'Ÿ-™-¬¯©Ô2à  1Ò$Ü# W IÐ0AÑ$AÐPTÔU��c’
ð +œT '›]¨2°G·L±LÈÏÉÔV��c’
äŸ™ §¡Ð2CÓDˆE�#ŠJð
Eð ˜Ñ! Z LÑ0€Hà‡{�{�1�~˜Ò¤#ñ &ØCHÇ<Á<ô&ô #ô #Ü',¨U¯[©[¸©^Ó'<Ö= !ˆU�Z‰Zš˜1˜ÓÒ=ó
ç
‰&ð 	ˆjÒð "ŸM™M×/Ñ/°Ó4ˆˆjÑÜ˜HÓ%ò I‰ˆˆ3Ø—]‘]×4Ñ4°QÓ7×>Ñ>¸xÓHˆˆcŠ
ðIð ×Ñ ¨xÐÓ8€FáÜ" 5§;¡;Ð0AÓBˆŒà€MùóGùò. Vùò@ >s   Â8
PÃPÇ)P$ÍP)c                óF  — i }g }t        «       }t        t        t        |j	                  «       «      «      «      }|j                  «       D ]j  \  }}t        |«      |k7  rt        d«      ‚|D �	cg c]  }	| |	   j                  ‘Œ }
}	t        |
«      ||<   |j                  |«       |j                  |«      }Œl t        | j                  j                  |«      «      }|D ](  }	t        j                  | |	   j                  |«      ||	<   Œ* |rxt        j                   t        ||d      «      t"        ¬«      }|D ]  }|t%        ||   «      z  }Œ |j'                  «       s&|j                  «       D ��ci c]  \  }}|||   “Œ }}}| j)                  |||z   ¬«      S c c}	w c c}}w )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 lengthr   )r%   r.   )r:   r8   ÚnextÚiterrB   Úitemsr   r@   r   ÚappendÚunionr   r   Ú
differencer(   r?   ÚonesÚboolr   ÚallrG   )ÚdataÚgroupsÚdropnarY   Ú
pivot_colsÚall_colsrW   Útargetr9   rZ   Ú	to_concatÚid_colsÚmaskÚcÚkÚvs                   r   Úlreshapert   �   s†  € ðj €EØ€JÜ!›e€HÜŒD”�f—m‘m“oÓ&Ó'Ó(€AØŸ™›ò )‰ˆ�Üˆu‹:˜Š?ÜÐCÓDÐDØ27Ö8¨3�T˜#‘Y×&Ó&Ð8ˆ	Ð8ä% iÓ0ˆˆf‰Ø×Ñ˜&Ô!Ø—>‘> %Ó(‰ð)ô �4—<‘<×*Ñ*¨8Ó4Ó5€GØò 3ˆÜ—W‘W˜T #™Y×.Ñ.°Ó2ˆˆcŠ
ð3ñ Ü�w‰w”s˜5 ¨A¡Ñ/Ó0¼Ô=ˆØò 	$ˆAØ”E˜% ™(“OÑ#‰Dð	$à�x‰xŒzØ,1¯K©K«M×:¡D A q�Q˜˜$™‘ZÐ:ˆEÑ:à×Ñ˜U¨G°jÑ,@ÐÓAÐAùò# 9ùó ;s   Á-FÅ/Fc                óÞ  — dd„}d	d„}t        |«      s|g}nt        |«      }| j                  j                  |«      j	                  «       rt        d«      ‚t        |«      s|g}nt        |«      }| |   j                  «       j	                  «       rt        d«      ‚g }g }	|D ]:  }
 || |
||«      }|	j                  |«       |j                   || |
||||«      «       Œ< t        |d¬«      }| j                  j                  |	«      }| |   }t        |«      dk(  r |j                  |«      j                  |«      S |j                  |j                  «       |¬«      j                  ||gz   «      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
    c                óÆ   — dt        j                  |«      › t        j                  |«      › |› d�}| j                  | j                  j                  j	                  |«         S )Nú^ú$)ÚreÚescaper   ÚstrÚmatch)ÚdfÚstubÚsepÚsuffixÚregexs        r   Úget_var_namesz#wide_to_long.<locals>.get_var_namesË  sL   € Ø”R—Y‘Y˜t“_Ð%¤b§i¡i°£nÐ%5°f°X¸QÐ?ˆØ�z‰z˜"Ÿ*™*Ÿ.™.×.Ñ.¨uÓ5Ñ6Ð6r   c                ó6  — t        | |||j                  |«      |¬«      }||   j                  j                  t	        j
                  ||z   «      dd¬«      ||<   	 t        ||   «      ||<   |j                  ||gz   «      S # t        t        t        f$ r Y Œ+w xY w)N)r   r    rK   rJ   Ú T)r�   )r   Úrstripr{   Úreplacery   rz   r   Ú	TypeErrorr   ÚOverflowErrorÚ	set_index)r}   r~   rU   Újr    r   Únewdfs          r   Ú	melt_stubzwide_to_long.<locals>.melt_stubÏ  sŸ   € ÜØØØ!Ø—{‘{ 3Ó'Øô
ˆð ˜‘8—<‘<×'Ñ'¬¯	©	°$¸±*Ó(=¸rÈÐ'ÓNˆˆa‰ð	Ü! %¨¡(Ó+ˆE�!‰Hð
 �‰˜q A 3™wÓ'Ð'øô	 œ:¤}Ð5ò 	áð	ús   ÁB ÂBÂBz,stubname can't be identical to a column namez3the id variables need to uniquely identify each rowr&   )Úaxis)Úon)r~   r{   r   r{   r€   r{   )r~   r{   r   r{   )r   r   r   Úisinr1   r   Ú
duplicatedÚextendrb   r
   rd   r8   r‰   ÚjoinÚmergeÚreset_index)r}   Ú	stubnamesrU   rŠ   r   r€   r‚   rŒ   Ú_meltedÚvalue_vars_flattenedr~   Ú	value_varÚmeltedr   Únews                  r   Úwide_to_longr›   Ý   s[  € ó\7ó(ô& ˜	Ô"Ø�K‰	ä˜“Oˆ	à	‡z�z‡��yÓ!×%Ñ%Ô'ÜÐGÓHÐHä˜Œ?ØˆC‰ä�‹Gˆà	ˆ!�u×ÑÓ×ÑÔÜÐNÓOÐOà€GØÐØò BˆÙ! " d¨C°Ó8ˆ	Ø×#Ñ# IÔ.Ø�‰‘y  T¨1¨a°¸CÓ@ÕAðBô
 �G !Ô$€FØ�j‰j×#Ñ#Ð$8Ó9€GØ
ˆW‰+€Cä
ˆ1ƒv�‚{Ø�}‰}˜QÓ×$Ñ$ VÓ,Ð,à�y‰y˜×+Ñ+Ó-°!ˆyÓ4×>Ñ>¸qÀAÀ3¹wÓGÐGr   )r   r{   Úreturnr   )NNNÚvalueNT)rI   r   rK   r   r#   rf   rœ   r   )T)rh   r   ri   Údictrj   rf   rœ   r   )r„   z\d+)r}   r   r   r{   r€   r{   rœ   r   )'Ú
__future__r   ry   Útypingr   Únumpyr(   Úpandas.util._decoratorsr   Úpandas.core.dtypes.commonr   Úpandas.core.dtypes.concatr   Úpandas.core.dtypes.missingr   Úpandas.core.algorithmsÚcoreÚ
algorithmsr5   Úpandas.core.indexes.apir	   Úpandas.core.reshape.concatr
   Úpandas.core.reshape.utilr   Úpandas.core.shared_docsr   Úpandas.core.tools.numericr   Úcollections.abcr   Úpandas._typingr   Úpandasr   r   r   rt   r›   © r   r   ú<module>r²      sì   ðÝ "ã 	Ý  ã å ,å 2Ý 3Ý ,ç &Ð &Ý .Ý -Ý 0Ý 0Ý 0áÝ(å+å óñ 
ˆ,�vÑ
¨NÐEUÑ!VÑ
VÓWð ØØØ"ØØð^Øð^ð
 ð^ð ð^ð ò^ó Xð^ôBMBðb BHðcHØðcHØ),ðcHØ;>ðcHàôcHr   