o
    d`                     @   s   d Z ddlZddlZddlZddlZddlm  mZ	 ddl
mZmZmZmZmZmZ ddlmZ ddlmZmZ ddlmZ e	jG dd deZe	jG dd	 d	eZdS )
z  Test cases for .boxplot method     N)	DataFrame
MultiIndexSeries
date_rangeplottingtimedelta_range)TestPlotBase_check_plot_works)pprint_thingc                   @   sr  e Zd Zdd Zejjdd Zdd Zdd Z	d	d
 Z
dd Zdd Zdd Zdd Zdd Zejdddddddddddfddiddifddddddfgdd Zejddd d d!d dfd"d#d#d$d#dfgd%d& Zejd'ddd(d)fgd*d+ Zejd,g d-d.d/ Zejd0d1d2gd3d4 Zejd0d1d2gd5d6 Zejd0d1d2gd7d8 Zd9S ):TestDataFramePlotsc                 C   s   dd l m} d}ttjg d|tjg d|tjg d|dtd|d}|jddd}d	d
 | D |j	
 ksBJ |tddd |  dd
 | D ttdddksdJ d S )Nr   P   )r            )ZClinicalZ	ConfirmedZ	DiscardedindexbarT)kindZstackedc                 S      g | ]}t | qS  intget_text.0xr   r   ]/app/.heroku/python/lib/python3.10/site-packages/pandas/tests/plotting/test_boxplot_method.py
<listcomp>,       zDTestDataFramePlots.test_stacked_boxplot_set_axis.<locals>.<listcomp>
   c                 S   r   r   r   r   r   r   r   r   /   r   )matplotlib.pyplotpyplotr   nprandomchoiceZarangeplotget_xticklabelsr   Zto_listZ
set_xticksZdrawlist)selfpltndfaxr   r   r   test_stacked_boxplot_set_axis   s    
 z0TestDataFramePlots.test_stacked_boxplot_set_axisc                 C   s  t tjddttjd d g dd}ddgd |d< g d	d
 |d< t|jdd t|jddgdd t	j
tdd t|jddgdd W d    n1 sRw   Y  t|jdddgd t	j
tdd t|jdd W d    n1 szw   Y  t	j
tdd t|jddgd W d    n1 sw   Y  ttjj|d dd t|jddd t	j
tdd t|jddd W d    d S 1 sw   Y  d S )N      onetwothreefourr   columnsfoor   r   Zindic)r7   r   r7   r   Zindic2dictreturn_typer1   r2   columnr:   FZcheck_stacklevel)r<   byr>   )datar:   r   )notchr:   )r>   rA   )r   r"   r#   randnr'   stringascii_lettersr	   boxplottmassert_produces_warningUserWarningr   Z_corer(   r+   r   r   r   test_boxplot_legacy13   s0   "z'TestDataFramePlots.test_boxplot_legacy1c                 C   s  t tjddddgd}tg d|d< tdgd |d	< tjtd
d t|j	dd W d    n1 s7w   Y  | j
 \}}|j	dd|d}|j}||u sTJ | j
 \}}|d	j	|dd}|j}||d u spJ | j
 \}}tt |j	ddgd|dd}W d    n1 sw   Y  |d  |u sJ | j
 \}}|j	|dd}ttj| }t| t|ksJ d S )Nr   r   ZCol1ZCol2r6   )
ArL   rL   rL   rL   BrM   rM   rM   rM   XrL   YFr=   r?   )r>   r,   axes)r,   r:   )r<   r>   r,   r:   r8   )r   r"   r#   randr   rF   rG   rH   r	   rE   r)   subplotsrP   groupbyZ
get_figurer'   	itertoolschainfrom_iterablevalueslen	get_lines)r(   r+   figr,   rP   Zax_axesdlinesr   r   r   test_boxplot_legacy2L   s2   z'TestDataFramePlots.test_boxplot_legacy2c                 C   s   |  }t|| jjsJ d S N)rE   
isinstancer)   ZAxes)r(   hist_dfresultr   r   r   test_boxplot_return_type_nonen   s   z0TestDataFramePlots.test_boxplot_return_type_nonec                 C   sF  t tjddttjd d g dd}d}tjt	|d |j
dd W d    n1 s/w   Y  |
 }| |d	 td
 |j
dd}W d    n1 sTw   Y  | |d td
 |j
d	d}W d    n1 suw   Y  | |d	 td
 |j
dd}W d    n1 sw   Y  | |d d S )Nr.   r/   r0   r5   z,return_type must be {'axes', 'dict', 'both'}matchZ
NOT_A_TYPEr9   rP   Fr8   both)r   r"   r#   rB   r'   rC   rD   pytestraises
ValueErrorrE   _check_box_return_typerF   rG   )r(   r+   msgra   r   r   r   test_boxplot_return_type_legacys   s.   z2TestDataFramePlots.test_boxplot_return_type_legacyc           	      C   s   dd }|  }tjdd|jd |d< |jddgd	d
\}}||d | ||d | |j|ks5J |jg dd	d
}|d |d |d }}}|d }||d | ||d | ||d | |j|kslJ |j|kssJ |jd u szJ d S )Nc                 S   s0   |  \}}||  ksJ ||  ksJ d S r^   )Zget_ylimminmax)colr,   Zy_minZy_maxr   r   r   _check_ax_limits   s   zETestDataFramePlots.test_boxplot_axis_limits.<locals>._check_ax_limitsr      r   ageheightweightcategoryr?   )rr   rs   rq   )r   r   )r   r   )r   r   r   r   )copyr"   r#   randintshaperE   Z_sharey)	r(   r`   ro   r+   Z	height_axZ	weight_axpZage_axZdummy_axr   r   r   test_boxplot_axis_limits   s    z+TestDataFramePlots.test_boxplot_axis_limitsc                 C   s8   t tjdd}tj|jd d df< t|jdd d S )Nrp   r/   r   rP   r9   )r   r"   r#   rB   nanlocr	   rE   rI   r   r   r   test_boxplot_empty_column   s   z,TestDataFramePlots.test_boxplot_empty_columnc                 C   sP   t tjddg dd}|jddd}|jjjdksJ |jjjd	ks&J d S )
Nr      )rL   rM   CDErK   rP   )      )r:   Zfigsizer   r   )	r   r"   r#   rQ   rE   figureZbbox_incheswidthrr   )r(   r+   ra   r   r   r   test_figsize   s   zTestDataFramePlots.test_figsizec                 C   s.   t dg di}| j|jdddddd d S )Nar   r   r   r/   r~   r.      )fontsizeZ
xlabelsizeZ
ylabelsizer   Z_check_ticks_propsrE   rI   r   r   r   test_fontsize   s   
z TestDataFramePlots.test_fontsizec              
   C   s   t tdddtjdtjdd tdddttddddtdddd}|jd	d
}dd |	 D ddgks>J d S )Nz
2012-01-01d   )periodsr   UTC)r   tzz1 days)r   bcr[   efbox)r   c                 S      g | ]}|  qS r   r   r   r   r   r   r          z@TestDataFramePlots.test_boxplot_numeric_data.<locals>.<listcomp>r   r   )
r   r   r"   r#   rB   astypestrr   r%   r&   )r(   r+   r,   r   r   r   test_boxplot_numeric_data   s   



"z,TestDataFramePlots.test_boxplot_numeric_datazcolors_kwd, expectedrr   gr   )boxeswhiskersmedianscapsr   c                 C   sN   t tjdd}|j|dd}| D ]\}}|| d  |ks$J qd S )Nr   r   r8   colorr:   r   )r   r"   r#   rQ   rE   items	get_color)r(   Z
colors_kwdexpectedr+   ra   kvr   r   r   test_color_kwd   s
   z!TestDataFramePlots.test_color_kwdzscheme,expectedZdark_backgroundz#8dd3c7z#bfbbd9defaultz#1f77b4z#2ca02cc                 C   sf   t tjdd}dd lm} |j| |jj	dd}|
 D ]\}}|| d  |ks0J q d S )Nr   r   r   r8   r9   )r   r"   r#   rQ   r    r!   styleZuser%   r   r   r   )r(   schemer   r+   r)   ra   r   r   r   r   r   test_colors_in_theme   s   z'TestDataFramePlots.test_colors_in_themezdict_colors, msg)r   Zinvalid_keyzinvalid key 'invalid_key'c                 C   sT   t tjdd}tjt|d |j|dd W d    d S 1 s#w   Y  d S )Nr   r   rc   r8   r   )r   r"   r#   rQ   rf   rg   rh   rE   )r(   Zdict_colorsrj   r+   r   r   r   test_color_kwd_errors   s   "z(TestDataFramePlots.test_color_kwd_errorszprops, expected))Zboxpropsr   )Zwhiskerpropsr   )Zcappropsr   )Zmedianpropsr   c                 C   sN   t dd dD }|ddii}|jd	ddi|}|| d  dks%J d S )
Nc                 S   s   i | ]	}|t jd qS )r   )r"   r#   )r   r   r   r   r   
<dictcomp>  s    z?TestDataFramePlots.test_specified_props_kwd.<locals>.<dictcomp>ABCr   ZC1r:   r8   r   r   )r   rE   r   )r(   propsr   r+   Zkwdra   r   r   r   test_specified_props_kwd  s   z+TestDataFramePlots.test_specified_props_kwdvertTFc                 C   sl   t tjdtjdtjddgdd}d\}}|jd|||d}| |ks,J | |ks4J d S )Nr   group1group2r   r   groupr   yr   )r   r   xlabelylabel)r   r"   r#   rB   r$   r%   
get_xlabel
get_ylabelr(   r   r+   r   r   r,   r   r   r   test_plot_xlabel_ylabel  s   

z*TestDataFramePlots.test_plot_xlabel_ylabelc                 C   sj   t tjdtjdtjddgdd}d\}}|j|||d}| |ks+J | |ks3J d S )Nr   r   r   r   r   )r   r   r   )r   r"   r#   rB   r$   rE   r   r   r   r   r   r   test_boxplot_xlabel_ylabel$  s   

z-TestDataFramePlots.test_boxplot_xlabel_ylabelc                 C   s   t tjdtjdtjddgdd}d\}}|jd|||d}|D ]}| |ks0J | |ks8J q&| j	  |jd|d}|D ]}|rO| n| }|t
dgks\J qG| j	  d S )	Nr   r   r   r   r   r   )r>   r   r   r   )r>   r   )r   r"   r#   rB   r$   rE   r   r   r)   closer
   )r(   r   r+   r   r   r,   ZsubplotZtarget_labelr   r   r    test_boxplot_group_xlabel_ylabel2  s"   


z3TestDataFramePlots.test_boxplot_group_xlabel_ylabelN)__name__
__module____qualname__r-   rf   markslowrJ   r]   rb   rk   rz   r}   r   r   r   parametrizer   r   r   r   r   r   r   r   r   r   r   r      sr    
"





	

r   c                
   @   s   e Zd Zdd Zejjdd Zdd Zdd Z	ejjd	d
 Z
ejjdd Zejjdd Zdd Zejddg dfdgg dfdg dfddgg dfdg dfgdd Zdd Zdd ZdS )TestDataFrameGroupByPlotsc                 C   s   |j dd}tjtdd t|jdd}W d    n1 sw   Y  | jt|jddd	 t|jddd
}| j|ddd	 d S )Ngenderr?   Fr=   rP   r9   r   r   r   Zaxes_numlayoutrR   r:   r   ru   )	rS   rF   rG   rH   r	   rE   _check_axes_shaper'   rW   )r(   r`   groupedrP   r   r   r   rJ   K  s   z.TestDataFrameGroupByPlots.test_boxplot_legacy1c                 C   s   t tjd d td}ttjddt	|d}|j
dd}tjtdd t|jdd	}W d    n1 s9w   Y  | jt|jdd
d t|jddd}| j|ddd d S )Nr   r   r   r   levelFr=   rP   r9   )r/   r   r   r   ru   )ziprC   rD   ranger   r"   r#   rQ   r   from_tuplesrS   rF   rG   rH   r	   rE   r   r'   rW   r(   tuplesr+   r   rP   r   r   r   r]   S  s   z.TestDataFrameGroupByPlots.test_boxplot_legacy2c                 C   s   t tjd d td}ttjddt	|d}|j
ddjddd}tjtdd	 t|jd
d}W d    n1 s>w   Y  | jt|jddd t|jdd
d}| j|ddd d S )Nr   r   r   r   r   r   )r   ZaxisFr=   rP   r9   r   r   r   r   ru   )r   rC   rD   r   r   r"   r#   rQ   r   r   ZunstackrS   rF   rG   rH   r	   rE   r   r'   rW   r   r   r   r   test_boxplot_legacy3_  s   z.TestDataFrameGroupByPlots.test_boxplot_legacy3c                 C   s   d}t tjjdd|d}t tjjdd|d}tjdjddg|d}t|||d	}|d
}| }t	| j
 dksAJ t	|dksIJ t  |jdd}t	| j
 dks^J t	|dksfJ t  |d
 }t  d S )Nr      rp   )size<   *   ZmaleZfemale)rr   rs   r   r   r   rP   r9   r   )r   r"   r#   normalZRandomStater$   r   rS   r%   rX   r)   Zget_fignumsrF   r   rE   hist)r(   r*   rs   rr   r   r+   gbresr   r   r   test_grouped_plot_fignumsi  s    
z3TestDataFrameGroupByPlots.test_grouped_plot_fignumsc           	      C   s"  |}|j dd}t|tjsJ | j|d g dd |dj dd}| j|dddgd d	 }ttj	d
d|d}d }|d |d< dD ]F}|dj |d}| j||g dd |j d|d}| j||g dd |dj |d}| j|||d |j d|d}| j|||d qHd S )Nr   r?   rr   rs   rt   )Zexpected_keysr8   r9   ZMaleZFemalezX B C D A G Y N Q O2   r   rK   zA B C D E F G H I Jr~   rt   )r8   rP   re   	classroomrL   rM   r   )r>   r:   )
rE   r_   r"   Zndarrayri   rS   splitr   r#   rB   )	r(   r`   r+   ra   Zcolumns2Zdf2Zcategories2treturnedr   r   r   test_grouped_box_return_type  s0   

z6TestDataFrameGroupByPlots.test_grouped_box_return_typec                 C   s<  |}d}t jt|d |jddg|jdd W d    n1 s!w   Y  d}t jt|d |jg dd	d
d W d    n1 sDw   Y  d}t jt|d |jddg|jdd W d    n1 shw   Y  tjtdd t|	djdd
d}W d    n1 sw   Y  | j
| j jddd tjtdd t|	djdd
d}W d    n1 sw   Y  | j
| j jddd tjtdd t|	djdd
d}W d    n1 sw   Y  | j
| j jddd |jg dddd}| j
| j jddd |d fD ]}| j| dd | j|j gdd q|d |d fD ]}| |  | |j g q2|	djg dd
d}| j
| j jddd tjtdd t|	djddd
d}W d    n	1 s}w   Y  | j
| j jddd tjtdd t|	djddd
d}W d    n	1 sw   Y  | j
| j jddd |jg dddd}| j
| j jddd |jg ddd d}| j
| j jdd!d |	djg dd"d
d}| j
| j jdd"d |	djg dd#d
d}| j
| j jdd$d d S )%Nz1Layout of 1x1 must be larger than required size 2rc   rs   rr   ru   )r<   r>   r   z7The 'layout' keyword is not supported when 'by' is Noner   )r   r   r8   )r<   r   r:   z1At least one dimension of layout must be positive)r   Fr=   r   r;   r   r   r   rt   r/   r   r   r   rP   )r<   r>   r:   )visible)r   r   )r   r   )r/   r   )r   r   )r   r   )r   r/   )r   r   r   r   )rf   rg   rh   rE   r   rF   rG   rH   r	   rS   r   r)   gcfrP   Z_check_visibler&   ZxaxisZ	get_label)r(   r`   r+   rj   r   rP   r,   r   r   r   test_grouped_box_layout  s   









z1TestDataFrameGroupByPlots.test_grouped_box_layoutc              	   C   s(  |}t t( | jdd\}}|djdd|d | j| j j	ddd W d    n1 s2w   Y  | jdd	\}}t t |jg d
dd|d d}W d    n1 s]w   Y  t
t|j}| j|d	dd t ||d  |d j|u sJ t t |djg d
d|d d}W d    n1 sw   Y  t
t|j}| j|d	dd t ||d  |d j|u sJ d}tjt|d8 | jdd	\}}t t |dj|d}W d    n1 sw   Y  W d    d S W d    d S 1 sw   Y  d S )Nr   rt   rr   rP   )r<   r:   r,   r/   r   r   r   r   r   r   )r<   r>   r:   r,   r   r   r   z@The number of passed axes must be 3, the same as the output plotrc   )r,   )rF   rG   rH   r)   rR   rS   rE   r   r   rP   r"   arrayr'   rW   Zassert_numpy_array_equalr   rf   rg   rh   )r(   r`   r+   rZ   rP   r   rj   r   r   r   test_grouped_box_multiple_axes   sJ   
$z8TestDataFrameGroupByPlots.test_grouped_box_multiple_axesc                 C   s6   t g dg dd}| j|jddddddd d S )	Nr   )r   r   r   r   r   r   )r   r   r   r   r   )r>   r   r   r   rI   r   r   r   r   -  s   
z'TestDataFrameGroupByPlots.test_fontsizezcol, expected_xticklabelr   )(a, v)(b, v)(c, v)(d, v)(e, v)v1)(a, v1)(b, v1)(c, v1)(d, v1)(e, v1))
r   r   r   r   r   r   r   r   r   r   Nc                 C   sl   t tjtddtjdtjdd}|d}t|jd|dd}dd	 |	 D }||ks4J d S )
NZabcder   )catr   r   r   FrP   )rR   r<   r:   c                 S   r   r   r   r   r   r   r   r   h  r   zQTestDataFrameGroupByPlots.test_groupby_boxplot_subplots_false.<locals>.<listcomp>)
r   r"   r#   r$   r'   rQ   rS   r	   rE   r&   )r(   rn   expected_xticklabelr+   r   rP   result_xticklabelr   r   r   #test_groupby_boxplot_subplots_false3  s   (



z=TestDataFrameGroupByPlots.test_groupby_boxplot_subplots_falsec                 C   sZ   | d}|d}d}tjt|d t|jdd W d    d S 1 s&w   Y  d S )Nobjectr   z:boxplot method requires numerical columns, nothing to plotrc   F)rR   )r   rS   rf   rg   rh   r	   rE   )r(   r`   r+   r   rj   r   r   r   test_groupby_boxplot_objectk  s   

"z5TestDataFrameGroupByPlots.test_groupby_boxplot_objectc           	      C   s   g dg dg}t t| }tj|ddgd}ttjddg d|d	}d
dg}t|j	|dd}ddg}dd |
 D }||ksDJ d S )N)r   r   bazr  r7   r7   quxr  )r1   r2   r1   r2   r1   r2   r1   r2   firstsecond)namesr   r   r   r5   )r   r1   )r   r2   rP   r;   z
(bar, one)z
(bar, two)c                 S   r   r   r   r   r   r   r   r     r   zLTestDataFrameGroupByPlots.test_boxplot_multiindex_column.<locals>.<listcomp>)r'   r   r   r   r   r"   r#   rB   r	   rE   r&   )	r(   Zarraysr   r   r+   rn   rP   r   r   r   r   r   test_boxplot_multiindex_columns  s   z8TestDataFrameGroupByPlots.test_boxplot_multiindex_column)r   r   r   rJ   rf   r   r   r]   r   r   r   r   r   r   r   r  r  r	  r   r   r   r   r   I  s:    


"
]
,


&r   )__doc__rT   rC   numpyr"   rf   Zpandas.util._test_decoratorsutilZ_test_decoratorstdZpandasr   r   r   r   r   r   Zpandas._testingZ_testingrF   Zpandas.tests.plotting.commonr   r	   Zpandas.io.formats.printingr
   Zskip_if_no_mplr   r   r   r   r   r   <module>   s        .