o
    ds                     @   s   d 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 ddlmZ ddlmZmZ ejdd ZejG dd deZejG d	d
 d
eZejG dd deZdS )z Test cases for .hist method     N)	DataFrameIndexSeriesto_datetime)TestPlotBase_check_plot_worksc                   C   s   t jddS )Ntsname)tmZmakeTimeSeries r   r   Z/app/.heroku/python/lib/python3.10/site-packages/pandas/tests/plotting/test_hist_method.pyr      s   r   c                   @   s   e Zd Zdd Zdd Zdd Zejjdd Z	d	d
 Z
dd Zdd Zejdg ddd Zejdddgdd Zejdddgdd Zdd Zejjdd ejd!d" Zejd#d$ ZdS )%TestSeriesPlotsc                 C   sv  t |j t |jdd t |jdd tjtdd t |j|jjd W d    n1 s.w   Y  tjtdd t |j|jjdd W d    n1 sOw   Y  | jd	d	\}}t |j|d
d t |j||d
d t |j|d
d t	  | jd	d\}\}}t |j||d
d t |j||d
d d}t
jt|d |j|j|d W d    d S 1 sw   Y  d S )NFgrid   
   figsizeZcheck_stacklevelby   )r   bins   T)axdefault_axes)r   figurer   )r   r      )r   r   r   z`Cannot pass 'figure' when using the 'by' argument, since a new 'Figure' instance will be createdmatch)r   r   )r   histr   assert_produces_warningUserWarningindexmonthpltsubplotsclosepytestraises
ValueError)selfr   figr   ax1ax2msgr   r   r   test_hist_legacy   s,   
"z TestSeriesPlots.test_hist_legacyc                 C   s<   t tjdd}|jddd d }t|jdksJ d S )Nr   r   r   r   )r   nprandomrandnr"   lenpatches)r-   dfr   r   r   r   test_hist_bins_legacy8   s   z%TestSeriesPlots.test_hist_bins_legacyc                 C   s   |}d}t jt|d |jjdd W d    n1 sw   Y  t jt|d |jjddgd W d    d S 1 s>w   Y  d S )Nz7The 'layout' keyword is not supported when 'by' is Noner    r   r   layoutr   )r*   r+   r,   heightr"   )r-   hist_dfr9   r1   r   r   r   test_hist_layout=   s   "z TestSeriesPlots.test_hist_layoutc                 C   sn  |}t jtdd t|jj|jdd}W d    n1 sw   Y  | j|ddd t jtdd t|jj|jdd}W d    n1 sGw   Y  | j|ddd t jtdd t|jj|jd	d}W d    n1 spw   Y  | j|d
d	d t jtdd t|jj|jdd}W d    n1 sw   Y  | j|d
dd t jtdd t|jj|jdd}W d    n1 sw   Y  | j|d
dd t jtdd t|jj|jdd}W d    n1 sw   Y  | j|d
dd t jtdd t|jj|j	dd}W d    n	1 sw   Y  | j|ddd |jj|jddd}| j|d
ddd d S )NFr   r   r   r   r=   r   axes_numr=   )   )rE   r      r   rH   r   rF   r   r   )rE   r   rF   rH   r   rH   rE   rH   r   )      )r   r=   r   rD   r=   r   )
r   r#   r$   r   r>   r"   gender_check_axes_shapecategory	classroomr-   r?   r9   axesr   r   r   test_hist_layout_with_byF   s>   z(TestSeriesPlots.test_hist_layout_with_byc                 C   sp   ddl m}m} ttjd}ttjd}|d |  |d |  | }|j}t	|dks6J d S )Nr   )gcfsubplotr   y   z   )
Zmatplotlib.pyplotrX   rY   r   r4   r5   r6   r"   rV   r7   )r-   rX   rY   xyr.   rV   r   r   r   test_hist_no_overlapl   s   z$TestSeriesPlots.test_hist_no_overlapc                 C   s.   |}|j j|jd}t| j dksJ d S )Nr   r   )r>   r"   rQ   r7   r'   get_fignumsrU   r   r   r   test_hist_by_no_extra_plots|   s   z+TestSeriesPlots.test_hist_by_no_extra_plotsc                 C   sh   ddl m} | }| }|d}d}tjt|d |j||d W d    d S 1 s-w   Y  d S )Nr   )r   o   z&passed axis not bound to passed figurer    )r   r   )Zpylabr   Zadd_subplotr*   r+   AssertionErrorr"   )r-   r   r   Zfig1Zfig2r/   r1   r   r   r   +test_plot_fails_when_ax_differs_from_figure   s   
"z;TestSeriesPlots.test_plot_fails_when_ax_differs_from_figurehisttype, expected)barT)Z
barstackedT)stepF)Z
stepfilledTc                 C   s0   t tjdd}|j|d}| j||d d S )Nr   r   histtypeZfilled)r   r4   r5   randintr"   _check_patches_all_filled)r-   ri   expectedserr   r   r   r   test_histtype_argument   s   z&TestSeriesPlots.test_histtype_argumentz&by, expected_axes_num, expected_layout)Nr   r;   )br   )r   r   c                 C   sd   ddg ddg  }t tjd|dd}d|j_t|jdd|d	}| j|||d
 | 	|d d S )N   12   ar%   r
   rp   T)r   legendr   rC   )
r   r4   r5   r6   r%   r
   r   r"   rR   _check_legend_labels)r-   r   expected_axes_numexpected_layoutr%   srV   r   r   r   test_hist_with_legend   s   z%TestSeriesPlots.test_hist_with_legendr   Nrp   c                 C   sv   ddg ddg  }t tjd|dd}d|j_tjtdd	 |j	d
|dd W d    d S 1 s4w   Y  d S )Nrq   rr   rs   rt   ru   rv   rp    Cannot use both legend and labelr    Tc)rw   r   label)
r   r4   r5   r6   r%   r
   r*   r+   r,   r"   )r-   r   r%   r{   r   r   r   test_hist_with_legend_raises   s   "z,TestSeriesPlots.test_hist_with_legend_raisesc                 C   s   | j  \}}|jjd|d}t|jdksJ | |j d t	
  | j  \}}|jjd|d}| |j d t	
  | j  \}}|jjdd|d}t	
  d S )	Nr   r   r   Z	Frequency
horizontal)Zorientationr   leftT)alignZstackedr   )r'   r(   plotr"   r7   r8   _check_text_labelsyaxisZ	get_labelr   r)   Zxaxis)r-   r   _r   r   r   r   test_hist_kwargs   s   z TestSeriesPlots.test_hist_kwargszApi changed in 3.6.0)reasonc                 C   s   | j  \}}|jjd|d}| j|dd | }| |dgt|  | }| |dgt|  t	|jj
 t	|jj | j  \}}|jj
d|d}| j|dd | }| |dgt|  | }| |dgt|  d S )NT)logyr   logr    )r'   r(   r   r"   _check_ax_scalesZget_xticklabelsr   r7   Zget_yticklabelsr   kdedensity)r-   r   r   r   ZxlabelsZylabelsr   r   r   test_hist_kde   s    zTestSeriesPlots.test_hist_kdec                 C   s   | j  \}}|jjddd|d}| j|dd t|jdks!J | j|jdgd d | j  \}}|jjdd|d	}| j|dd |	 }t|d
ksOJ | |dg d S )NTr   rp   )r   r   colorr   r   r   )Z
facecolorsr)r   r   r   r   )
r'   r(   r   r"   r   r7   r8   Z_check_colorsr   	get_lines)r-   r   r   r   linesr   r   r   test_hist_kde_color   s   z#TestSeriesPlots.test_hist_kde_color)__name__
__module____qualname__r2   r:   r@   r*   markslowrW   r^   r`   rc   parametrizero   r|   r   r   ZxfailtdZskip_if_no_scipyr   r   r   r   r   r   r      s4    	
%

	

	r   c                   @   s  e Zd Zejjdd Zdd Zdd Zdd Z	d	d
 Z
ejddg dfg dg dfgdd Zejdg ddd Zejdddgejdddgdd Zejdddgejdddgdd Zdd Zdd  Zd!d" Zejd#d$ ZdS )%TestDataFramePlotsc                    sN  ddl m  tjtdd t|j W d    n1 sw   Y  ttj	
dd}ttj	jdddtjd	|d< tjtdd t|jdd
}W d    n1 sSw   Y  | j|ddd |d  rhJ t|dg j ttj	
dd}t|j ttj	
dd}ttj	jdddtjd	|d< tjtdd t|jdd}W d    n1 sw   Y  | j|ddd tjtdd t|jddd W d    n1 sw   Y  tjtdd t|jdd W d    n1 sw   Y  tjtdd t|jdd W d    n	1 sw   Y  |d }d\}}d\}}|j||||d}| j|||||d d\}}d\}}|j||||d}| j|||||d t  |jdddd}	 fdd|	 D }
t|
d   d! t  |jdd"}	| j|	d#d$ t  tt |jd%d& W d    d S 1 sw   Y  d S )'Nr   	RectangleFr   d   r        j+2Z     ReZsizedtyper   rE   rJ   rC   r;   r   r   rM   r<      T)sharexshareyr   r   r3         rt   (   
xlabelsizexrot
ylabelsizeyrotrH   )
cumulativer   r   c                       g | ]	}t | r|qS r   
isinstance.0r\   r   r   r   
<listcomp>2      z:TestDataFramePlots.test_hist_df_legacy.<locals>.<listcomp>rF         ?)r   r   r   rf   )foo)matplotlib.patchesr   r   r#   r$   r   r"   r   r4   r5   r6   r   rk   int64rR   get_visible_check_ticks_propsr)   get_childrenassert_almost_equal
get_heightr   external_error_raisedAttributeError)r-   r?   r9   rV   rn   xfyfr   r   r   rectsr   r   r   test_hist_df_legacy   s   


$z&TestDataFramePlots.test_hist_df_legacyc                 C   s   t tjdtjdddttjjdddtjdttjjdddtjdddd}|t}d	}t	j
t|d
 |  W d    d S 1 sIw   Y  d S )Nr   r   l     p/ l    @spc/ )r   T)utc)ru   rp   r~   dzDhist method requires numerical or datetime columns, nothing to plot.r    )r   r4   r5   Zrandrk   r   r   Zastypeobjectr*   r+   r,   r"   )r-   r9   Zdf_or1   r   r   r   *test_hist_non_numerical_or_datetime_raises@  s*   




"z=TestDataFramePlots.test_hist_non_numerical_or_datetime_raisesc                 C   st  t tjdd}ttjjdddtjd|d< d ddddddddd	d	dd
d
ddd	ddddddddddf	}|D ]}|j|d d}|d }| j|d|d q@d}t	j
t|d |jdd W d    n1 sqw   Y  td}t	j
t|d |jdd W d    n1 sw   Y  d}t	j
t|d |jdd W d    d S 1 sw   Y  d S )Nr   r   r   r   r   rJ   )r=   expected_sizerG   rL   )rE   rE   rK   )rH   rF   )rF   r   rI   r=   r<   r   rE   rC   z1Layout of 1x1 must be larger than required size 3r    r;   z)Layout must be a tuple of (rows, columns))r   1At least one dimension of layout must be positiverF   rF   )r   r4   r5   r6   r   rk   r   r"   rR   r*   r+   r,   reescape)r-   r9   Zlayout_to_expected_sizeZlayout_testrV   rm   r1   r   r   r   r@   Y  sF   

"z#TestDataFramePlots.test_hist_layoutc                 C   sT   t tjdd}ttjjdddtjd|d< t|jdd | j	
  t  d S )Nr   r   r   r   r   T)r   )r   r4   r5   r6   r   rk   r   r   r"   r'   Ztight_layoutr   r)   )r-   r9   r   r   r   test_tight_layout  s   	
z$TestDataFramePlots.test_tight_layoutc                 C   s>   t g dg dd}t|jdddddd	}| j|dd
 d S )Ng      ?      ?g333333??rE   )pigrabbitr   r   r   )lengthanimalTr   r   r   r   )r   columnr   r   r   r   )r   r   r"   r   )r-   r9   rV   r   r   r   test_hist_subplot_xrot  s   z)TestDataFramePlots.test_hist_subplot_xrotzcolumn, expectedNwidthr   r>   )r   r   r>   c                    s\   t g dg dg ddg dd}t|jd|dd	  fd
dtdD }||ks,J d S )N)ffffff?皙?g333333?r   g?r   )rE   r   g333333@r   r   r   )r   r   duckchickenhorse)r%   Tr   rE   )r   r   r=   c                    s   g | ]
} d |f   qS )r   )Z	get_title)r   irV   r   r   r     s    zGTestDataFramePlots.test_hist_column_order_unchanged.<locals>.<listcomp>rE   )r   r   r"   range)r-   r   rm   r9   resultr   r   r    test_hist_column_order_unchanged  s   

z3TestDataFramePlots.test_hist_column_order_unchangedrd   re   c                 C   s<   t tjjddddddgd}|j|d}| j||d	 d S )
Nr   r   r   r   r   ru   rp   columnsrh   rj   r   r4   r5   rk   r"   rl   r-   ri   rm   r9   r   r   r   r   ro     s   z)TestDataFramePlots.test_histtype_argumentr   r~   r   rp   c                 C   s   |d u r
|d ur
dnd}d|f}|pddg}|d ur|gd }t ddg ddg  dd	}ttjd
d|ddgd}t|jdd||d}| j|||d |d u rY|d u rY|d }t||D ]
\}	}
| 	|
|	 q^d S )Nr   r   ru   rp   rq   rr   rs   r~   r	   rt   r%   r   T)r   rw   r   r   rC   r   )
r   r   r4   r5   r6   r   r"   rR   ziprx   )r-   r   r   ry   rz   Zexpected_labelsr%   r9   rV   Zexpected_labelr   r   r   r   r|     s(   
z(TestDataFramePlots.test_hist_with_legendc                 C   s~   t ddg ddg  dd}ttjdd|dd	gd
}tjtdd |jd||dd W d    d S 1 s8w   Y  d S )Nrq   rr   rs   r~   r	   rt   r   ru   rp   r   r}   r    Tr   )rw   r   r   r   )	r   r   r4   r5   r6   r*   r+   r,   r"   )r-   r   r   r%   r9   r   r   r   r     s
   "z/TestDataFramePlots.test_hist_with_legend_raisesc                 C   sF   t tjdd}| j \}}|jjd|d}t|j	dks!J d S )Nr   r   r   r   )
r   r4   r5   r6   r'   r(   r   r"   r7   r8   r-   r9   r   r   r   r   r   test_hist_df_kwargs  s   z&TestDataFramePlots.test_hist_df_kwargsc                 C   s   t tjdddg dd}ddgd |d	< | j \}}|jjd|d
}t	|j
dks0J | j \}}|jj|d}t	|j
dksGJ d S )N*   r   rH   )ABCDr   r\   r]   r   Er   r   )r   r   )r   r4   r5   RandomStater6   r'   r(   r   r"   r7   r8   r   r   r   r   test_hist_df_with_nonnumerics  s   z0TestDataFramePlots.test_hist_df_with_nonnumericsc                 C   s  t tjddtdd}| j \}}|d jjd|d}|d jj|ddd	 | j	|dd
gd |
  s:J |j
  sCJ t  | j \}}|d jjdd|d}|d jj|ddd	 | j	|jdd
gd |j
  rwJ |
  sJ t  | j \}}|d jjdd|d}|d jj|dd | j	|jddgd |j
  sJ |
  sJ t  d S )Nrt   rH   abcdr   ru   T)rw   r   rp   )r   rw   secondary_yz	b (right))labels)rw   r   r   z	a (right))r   rw   )r   r4   r5   r6   listr'   r(   r   r"   rx   Z	get_yaxisr   Zright_axr   r)   Zleft_axr   r   r   r   test_hist_secondary_legend  s,   z-TestDataFramePlots.test_hist_secondary_legendc                    s  t tjddgdtjtjgg dgtdd}tg d}t g dg dgtdd}tg d	g d
g}ddlm  | j \}}|j	j
||d  fdd| D }dd |D }| j \}}	|j	j
|	|d  fdd|	 D }
dd |
D }tdd t||D sJ tddgddgg}d}tjt|d | j \}}|j	j
||d W d    d S 1 sw   Y  d S )Nr   333333?皙?)r   g?r   abcr   )      ?r  ?)r  r   r  )r  r  r  )r  r  r  r   r   )r   weightsc                    r   r   r   r   r   r   r   r   A  r   zFTestDataFramePlots.test_hist_with_nans_and_weights.<locals>.<listcomp>c                 S      g | ]}|  qS r   r   r   rectr   r   r   r   B      c                    r   r   r   r   r   r   r   r   E  r   c                 S   r
  r   r  r  r   r   r   r   F  r  c                 s   s    | ]	\}}||kV  qd S )Nr   )r   Zh0Zh1r   r   r   	<genexpr>G  s    zETestDataFramePlots.test_hist_with_nans_and_weights.<locals>.<genexpr>r  r  z?weights must have the same shape as data, or be a single columnr    )r   r4   nanr  arrayr   r   r'   r(   r   r"   r   allr   r*   r+   r,   )r-   r9   r	  Z	no_nan_dfZno_nan_weightsr   Zax0r   Zheightsr/   Zno_nan_rectsZno_nan_heightsZidxerror_weightsr1   r0   r   r   r   test_hist_with_nans_and_weights2  s.   "z2TestDataFramePlots.test_hist_with_nans_and_weights)r   r   r   r*   r   r   r   r   r@   r   r   r   r   ro   r|   r   r   r   r  r   skip_if_no_mplr  r   r   r   r   r      s<    
U*


	&r   c                   @   sf   e Zd Zdd Zdd Zejjdd Zdd Z	d	d
 Z
dd Zdd Zejdg ddd ZdS )TestDataFrameGroupByPlotsc                    s$  ddl m  ddlm} ttjdddgd}ttjj	dd	dtj
d
|d< tj	ddd|d< dgd |d< ||j|jd}| j|ddd t  |j|jd}| j|ddd t  |jddd}| j|ddd | j|dd t  d\}}d\}}||j|jdd||||dd	}| D ]} fdd| D }	|	d  }
t|
d q| j|||||d t  ||j|jdd }| j|d!d" t  tt ||j|jd#d$ W d    n1 sw   Y  d%}tjt|d& |jdd'd( W d    d S 1 sw   Y  d S ))Nr   r   )_grouped_histi  r   r   r   r   r   r   r   rH   r   Xr   r   rJ   rC   rt   )r   Zrotr;   r   r   r   T)r   r   r   r   r   r   r   r   c                    r   r   r   r   r   r   r   r     r   zFTestDataFrameGroupByPlots.test_grouped_hist_legacy.<locals>.<listcomp>rF   r   r   )r   r   r   r   rf   )r   r   z$Specify figure size by tuple insteadr    default)r   r   )r   r   Z pandas.plotting._matplotlib.histr  r   r4   r5   r6   r   rk   r   r   r   rR   r   r)   r"   r   Zravelr   r   r   r   r   r   r*   r+   r,   )r-   r  r9   rV   r   r   r   r   r   r   r>   r1   r   r   r   test_grouped_hist_legacyS  sl   
$z2TestDataFrameGroupByPlots.test_grouped_hist_legacyc                 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	|dks>J t	| j
 dksIJ t  d S )Nr      r   r   <   r   r   r   )r>   weightrQ   rQ   r   )r   r4   r5   normalr   choicer   groupbyr"   r7   r'   r_   r   r)   )r-   nr  r>   Z
gender_intZdf_intgbrV   r   r   r   test_grouped_hist_legacy2  s   
z3TestDataFrameGroupByPlots.test_grouped_hist_legacy2c                 C   s  |}d}t jt|d |jd|jdd W d    n1 sw   Y  d}t jt|d |jd|jdd W d    n1 sAw   Y  d	}t jt|d |jd|jd
d W d    n1 scw   Y  tjtdd t	|jd|jdd}W d    n1 sw   Y  | j
|ddd tjtdd t	|jd|jdd}W d    n1 sw   Y  | j
|ddd |jd|jdd}| j
|ddd |jd|jdd}| j
|ddd |jd|jddd}| j
|dddd t  tjtdd t	|jdddd}W d    n	1 sw   Y  | j
|ddd tjtdd t	|jdd}W d    n	1 s6w   Y  | j
|ddd |jddd}| j
|ddd |jg dd }| j
|ddd d S )!Nz1Layout of 1x1 must be larger than required size 2r    r  r;   )r   r   r=   z1Layout of 1x3 must be larger than required size 4r>   r   r   r   Fr   rA   r   rC   rI   rG   rH   )rF   r   rM   )rN   r   )r   r   r=   r   rP   rT   rJ   rE   r   rQ   )rE   r   rB   r>   r  rS   )r   )r*   r+   r,   r"   rQ   rS   r   r#   r$   r   rR   r)   )r-   r?   r9   r1   rV   r   r   r   test_grouped_hist_layout  s\   
z2TestDataFrameGroupByPlots.test_grouped_hist_layoutc                 C   s   |}| j dd\}}|jg d|d d}| j|ddd t||d  |d j|u s/J |jd|d	 d
}| j|ddd t||d	  |d j|u sQJ | j dd\}}d}tjt	|d |jd|d}W d    d S 1 svw   Y  d S )Nr   rE   r#  r   )r   r   r   rC   rT   r   )r   r   z@The number of passed axes must be 1, the same as the output plotr    r>   )
r'   r(   r"   rR   r   Zassert_numpy_array_equalr   r*   r+   r,   )r-   r?   r9   r.   rV   returnedr1   r   r   r   test_grouped_hist_multiple_axes  s   "z9TestDataFrameGroupByPlots.test_grouped_hist_multiple_axesc                 C   v   |}|j d|jdd\}}| |||sJ | |||s#J | |||r.J | |||r9J d S )Nr>   T)r   r   r   r"   rQ   
get_x_axisjoined
get_y_axisr-   r?   r9   r/   r0   r   r   r   test_axis_share_x  s   z+TestDataFrameGroupByPlots.test_axis_share_xc                 C   r'  )Nr>   T)r   r   r   )r"   rQ   r+  r*  r)  r,  r   r   r   test_axis_share_y  s   z+TestDataFrameGroupByPlots.test_axis_share_yc                 C   sx   |}|j d|jddd\}}| |||sJ | |||s$J | |||s/J | |||s:J d S )Nr>   T)r   r   r   r   r(  r,  r   r   r   test_axis_share_xy  s   z,TestDataFrameGroupByPlots.test_axis_share_xyrd   re   c                 C   s>   t tjjddddddgd}|jd|d}| j||d	 d S )
Nr   r   r   r   ru   rp   r   )r   ri   rj   r   r   r   r   r   ro     s   z0TestDataFrameGroupByPlots.test_histtype_argumentN)r   r   r   r  r"  r*   r   r   r$  r&  r-  r.  r/  r   ro   r   r   r   r   r  Q  s    E
6	r  )__doc__r   numpyr4   r*   Zpandas.util._test_decoratorsutilZ_test_decoratorsr   Zpandasr   r   r   r   Zpandas._testingZ_testingr   Zpandas.tests.plotting.commonr   r   Zfixturer   r  r   r   r  r   r   r   r   <module>   s&    
 M  j