a
    oÝEbr  ã                   @  s†   d dl mZ d dlZd dlmZmZ d dlmZ d dl	m
Z
mZmZmZ dddd	d
œdd„Zdddddœdd„Zddddœdd„ZdS )é    )ÚannotationsN)ÚDictÚ
IndexLabel©Úremove_na_arraylike)Ú	DataFrameÚ
MultiIndexÚSeriesÚconcatÚhistr   ÚstrzDict[str, DataFrame | Series])ÚdataÚkindÚreturnc                   s>   |dkrd‰nd‰t ˆ jtƒs"J ‚‡ ‡fdd„ˆ jjˆ D ƒS )ax  
    Create data for iteration given `by` is assigned or not, and it is only
    used in both hist and boxplot.

    If `by` is assigned, return a dictionary of DataFrames in which the key of
    dictionary is the values in groups.
    If `by` is not assigned, return input as is, and this preserves current
    status of iter_data.

    Parameters
    ----------
    data : reformatted grouped data from `_compute_plot_data` method.
    kind : str, plot kind. This function is only used for `hist` and `box` plots.

    Returns
    -------
    iter_data : DataFrame or Dictionary of DataFrames

    Examples
    --------
    If `by` is assigned:

    >>> import numpy as np
    >>> tuples = [('h1', 'a'), ('h1', 'b'), ('h2', 'a'), ('h2', 'b')]
    >>> mi = MultiIndex.from_tuples(tuples)
    >>> value = [[1, 3, np.nan, np.nan],
    ...          [3, 4, np.nan, np.nan], [np.nan, np.nan, 5, 6]]
    >>> data = DataFrame(value, columns=mi)
    >>> create_iter_data_given_by(data)
    {'h1':     h1
         a    b
    0  1.0  3.0
    1  3.0  4.0
    2  NaN  NaN, 'h2':     h2
         a    b
    0  NaN  NaN
    1  NaN  NaN
    2  5.0  6.0}
    r   r   é   c                   s,   i | ]$}|ˆ j d d …ˆ j ˆ¡|kf “qS )N)ÚlocÚcolumnsZget_level_values©Ú.0Úcol©r   Úlevel© úc/home/ja/django-apps/lartica_env/lib/python3.9/site-packages/pandas/plotting/_matplotlib/groupby.pyÚ
<dictcomp>K   s   ÿz-create_iter_data_given_by.<locals>.<dictcomp>)Ú
isinstancer   r   Zlevels)r   r   r   r   r   Úcreate_iter_data_given_by   s    /
þr   r   )r   ÚbyÚcolsr   c           	      C  sT   |   |¡}g }|D ]0\}}t |g|g¡}|| }||_| |¡ qt|dd�} | S )ai  
    Internal function to group data, and reassign multiindex column names onto the
    result in order to let grouped data be used in _compute_plot_data method.

    Parameters
    ----------
    data : Original DataFrame to plot
    by : grouped `by` parameter selected by users
    cols : columns of data set (excluding columns used in `by`)

    Returns
    -------
    Output is the reconstructed DataFrame with MultiIndex columns. The first level
    of MI is unique values of groups, and second level of MI is the columns
    selected by users.

    Examples
    --------
    >>> d = {'h': ['h1', 'h1', 'h2'], 'a': [1, 3, 5], 'b': [3, 4, 6]}
    >>> df = DataFrame(d)
    >>> reconstruct_data_with_by(df, by='h', cols=['a', 'b'])
       h1      h2
       a     b     a     b
    0  1.0   3.0   NaN   NaN
    1  3.0   4.0   NaN   NaN
    2  NaN   NaN   5.0   6.0
    r   )Zaxis)Úgroupbyr   Zfrom_productr   Úappendr
   )	r   r   r   ÚgroupedZ	data_listÚkeyÚgroupr   Z	sub_groupr   r   r   Úreconstruct_data_with_byQ   s    
r$   zSeries | np.ndarrayzIndexLabel | None)Úyr   r   c                 C  s6   |dur.t | jƒdkr.t dd„ | jD ƒ¡jS t| ƒS )zàInternal function to reformat y given `by` is applied or not for hist plot.

    If by is None, input y is 1-d with NaN removed; and if by is not None, groupby
    will take place and input y is multi-dimensional array.
    Nr   c                 S  s   g | ]}t |ƒ‘qS r   r   r   r   r   r   Ú
<listcomp>…   ó    z,reformat_hist_y_given_by.<locals>.<listcomp>)ÚlenÚshapeÚnpÚarrayÚTr   )r%   r   r   r   r   Úreformat_hist_y_given_by|   s    r-   )r   )Ú
__future__r   Únumpyr*   Zpandas._typingr   r   Zpandas.core.dtypes.missingr   Zpandasr   r   r	   r
   r   r$   r-   r   r   r   r   Ú<module>   s   	 ÿ=+