zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
U
¬ý°dã@s’ddlmZddlZddlmZmZddlmZddl    m
Z
m Z m Z m Z ddlmZddd    d
d œd d „Zdddddœdd„Zddddœdd„ZdS)é)Ú annotationsN)ÚDictÚ
IndexLabel©Úremove_na_arraylike)Ú    DataFrameÚ
MultiIndexÚSeriesÚconcat)Úunpack_single_str_listÚhistrÚstrzDict[str, DataFrame | Series])ÚdataÚkindÚreturncs>|dkrd‰nd‰tˆjtƒs"t‚‡‡fdd„ˆjjˆ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écs,i|]$}|ˆjdd…ˆj ˆ¡|kf“qS)N)ÚlocÚcolumnsZget_level_values©Ú.0Úcol©rÚlevel©úZd:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\pandas/plotting/_matplotlib/groupby.pyÚ
<dictcomp>Msÿz-create_iter_data_given_by.<locals>.<dictcomp>)Ú
isinstancerrÚAssertionErrorÚlevels)rrrrrÚcreate_iter_data_given_bys/ 
þrr)rÚbyÚcolsrc
Cs\t|ƒ}| |¡}g}|D]0\}}t |g|g¡}||}    ||    _| |    ¡qt|dd}|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)Zaxis)r ÚgroupbyrZ from_productrÚappendr
)
rr r!Z by_modifiedZgroupedZ    data_listÚkeyÚgrouprZ    sub_grouprrrÚreconstruct_data_with_bySs
   r&zSeries | np.ndarrayzIndexLabel | None)Úyr rcCs6|dk    r.t|jƒdkr.t dd„|jDƒ¡jSt|ƒ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.
    NrcSsg|] }t|ƒ‘qSrrrrrrÚ
<listcomp>Šsz,reformat_hist_y_given_by.<locals>.<listcomp>)ÚlenÚshapeÚnpÚarrayÚTr)r'r rrrÚreformat_hist_y_given_bysr.)r )Ú
__future__rÚnumpyr+Zpandas._typingrrZpandas.core.dtypes.missingrZpandasrrr    r
Z pandas.plotting._matplotlib.miscr rr&r.rrrrÚ<module>s   ÿ=.