a
    ï½„gÆV  ã                   @   sŠ  d dl Z d dlZd dlZd dlZd dlmZmZmZm	Z	 d dl
mZ d dlmZ ejddgddgedd�dd„ g d	¢e g d	¢¡eeed
ƒg d	¢ƒƒe	g d¢ƒe	g d¢ƒe	g d¢ƒgg
d�dd„ ƒZejddgd�dd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZejdd„ ƒZd>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¡ej d'ejej g¡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¡ej d'ejej g¡d.d/„ ƒƒZ$ej d0g d1¢¡d2d3„ ƒZ%ej dg d ¢¡d4d5„ ƒZ&ej dd#d$g¡d6d7„ ƒZ'ej dd#d$g¡ej d'ejej g¡d8d9„ ƒƒZ(ej dg d ¢¡d:d;„ ƒZ)d<d=„ Z*dS )?é    N)ÚCategoricalÚ	DataFrameÚGrouperÚSeries)Úget_groupby_method_argsÚaÚb)Úkeyc                 C   s   | d S )Né   © ©Úxr   r   úc/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pandas/tests/groupby/test_raises.pyÚ<lambda>   ó    r   )	r   r   r   é   r
   r
   r
   é   r   é	   ©	r   r   r   r   r   r
   r
   r
   r
   ©	r   r   é   r   r   r   r   r   r   )Úparamsc                 C   s   | j S ©N©Úparam©Úrequestr   r   r   Úby   s    r   TFc                 C   s   | j S r   r   r   r   r   r   Úgroupby_series'   s    r   c                  C   s&   t g d¢g d¢tdƒtdƒdœƒ} | S )Nr   r   r   Z	xyzwtyuio©r   r   ÚcÚd)r   ÚrangeÚlist©Údfr   r   r   Údf_with_string_col,   s    üÿr&   c                  C   s4   t g d¢g d¢tdƒt ddddddd	¡d
œƒ} | S )Nr   r   r   iÕ  r   é
   é   é   i`= r   )r   r"   Údatetimer$   r   r   r   Údf_with_datetime_col9   s    üÿr+   c                  C   s*   t g d¢g d¢tdƒtjdd�dœƒ} | S )Nr   r   r   r   )Údaysr   )r   r"   r*   Ú	timedeltar$   r   r   r   Údf_with_timedelta_colF   s    
üÿr.   c               	   C   s4   t g d¢g d¢tdƒtg d¢g d¢dd�dœƒ} | S )Nr   r   r   ©	r   r   r   r   r   r   r   r   r    r   T©Ú
categoriesZordered)r   r"   r   r$   r   r   r   Údf_with_cat_colS   s    ýüÿr2   Ú c              	   C   s  |dkrd nt }tj||d��Ô | d u rp|dkr@t||ƒ|Ž  qä|dkr\|j|g|¢R Ž  qä|j|g|¢R Ž  nttj| |d��V |dkr˜t||ƒ|Ž  n.|dkr´|j|g|¢R Ž  n|j|g|¢R Ž  W d   ƒ n1 sÚ0    Y  W d   ƒ n1 sø0    Y  d S )Nr3   ©ÚmatchÚmethodÚagg)ÚFutureWarningÚtmÚassert_produces_warningÚgetattrr7   Ú	transformÚpytestÚraises)ÚklassÚmsgÚhowÚgbÚgroupby_funcÚargsÚwarn_msgZ
warn_klassr   r   r   Ú_call_and_checkd   s    rF   rA   )r6   r7   r<   c              %   C   s6  |}t ||ƒ}|j|d�}|r@|d }|dkr@t|dƒr<J ‚d S dddtdfddttfdfttfdfttfdfttfd	ftd
fdddddddtt d¡ftt d¡fdddtd
ftt d¡ftdfdtdfddtdftdfdtt d¡fdœ"| \}}	|dk�r|�r
dnd}
|
› d�}nd}t||	| ||||ƒ d S )N©r   r!   Úcorrwith©Nr3   zCould not convertzH(function|cummax) is not (implemented|supported) for (this|object) dtypezH(function|cummin) is not (implemented|supported) for (this|object) dtypezI(function|cumprod) is not (implemented|supported) for (this|object) dtypezH(function|cumsum) is not (implemented|supported) for (this|object) dtypeúunsupported operand typeú-agg function failed [how->mean,dtype->object]z/agg function failed [how->median,dtype->object]z-agg function failed [how->prod,dtype->object]z,cannot be performed against 'object' dtypes!z!could not convert string to floatz%agg function failed [how->var,dtype->©"ÚallÚanyZbfillrH   ÚcountZcumcountZcummaxZcumminÚcumprodZcumsumÚdiffZffillÚfillnaÚfirstZidxmaxZidxminÚlastÚmaxÚmeanZmedianÚminZngroupZnuniqueZ
pct_changeÚprodZquantileZrankZsemÚshiftÚsizeÚskewZstdÚsumÚvarrR   r   r   úGroupBy.fillna is deprecatedr3   )	r   ÚgroupbyÚhasattrÚ	TypeErrorÚNotImplementedErrorÚreÚescapeÚ
ValueErrorrF   )rA   r   r   rC   r&   r%   rD   rB   r?   r@   ÚkindrE   r   r   r   Útest_groupby_raises_stringx   s„    
þþþþþþþþÉ;Å=
rg   r7   r<   c                 C   sd   |}|j |d�}|r|d }dd„ }tjtdd�� t|| ƒ|ƒ W d   ƒ n1 sV0    Y  d S )NrG   r!   c                 S   s   t dƒ‚d S ©NúTest error message©ra   r   r   r   r   ÚfuncÔ   s    z,test_groupby_raises_string_udf.<locals>.funcri   r4   ©r_   r=   r>   ra   r;   )rA   r   r   r&   r%   rB   rk   r   r   r   Útest_groupby_raises_string_udfÌ   s    rm   Úgroupby_func_npc           
   	   C   sf   |}|j |d�}|r|d }tjdtjtt d¡fi| \}}|rHd}	nd}	t||| ||d|	d� d S )	NrG   r!   rI   rK   úusing SeriesGroupBy.[sum|mean]ú!using DataFrameGroupBy.[sum|mean]r   ©rE   )r_   Únpr\   rV   ra   rc   rd   rF   )
rA   r   r   rn   r&   r%   rB   r?   r@   rE   r   r   r   Útest_groupby_raises_string_npÛ   s     þþúrs   c              #   C   s  |}t ||ƒ}|j|d�}|r@|d }|dkr@t|dƒr<J ‚d S dddtdfddddtdftdfdddddddddddddtdftd	fdddddtd
 ddg¡fdtdftdfdœ"| \}}	|dv rÒd|› d�}
n$|dkrò|râdnd}|› d�}
nd}
t||	| ||||
d� d S )NrG   r!   rH   rI   z+cannot perform __mul__ with this index typez3datetime64 type does not support cumprod operationsz2datetime64 type does not support cumsum operationsz/cannot perform __truediv__ with this index typez%datetime64 type does not support prodú|z1dtype datetime64\[ns\] does not support reductionz0datetime64 type does not support skew operationsú/datetime64 type does not support sum operationsz/datetime64 type does not support var operationsrL   )rN   rM   ú'z&' with datetime64 dtypes is deprecatedrR   r   r   r^   r3   rq   )r   r_   r`   ra   ÚjoinrF   )rA   r   r   rC   r+   r%   rD   rB   r?   r@   rE   rf   r   r   r   Útest_groupby_raises_datetimeö   st    
þÿþ	Ö+Õ-rx   c                 C   sd   |}|j |d�}|r|d }dd„ }tjtdd�� t|| ƒ|ƒ W d   ƒ n1 sV0    Y  d S )NrG   r!   c                 S   s   t dƒ‚d S rh   rj   r   r   r   r   rk   D  s    z.test_groupby_raises_datetime_udf.<locals>.funcri   r4   rl   )rA   r   r   r+   r%   rB   rk   r   r   r   Ú test_groupby_raises_datetime_udf<  s    ry   c           
   	   C   s`   |}|j |d�}|r|d }tjtdftjdi| \}}|rBd}	nd}	t||| ||d|	d� d S )	NrG   r!   ru   rI   ro   rp   r   rq   ©r_   rr   r\   ra   rV   rF   )
rA   r   r   rn   r+   r%   rB   r?   r@   rE   r   r   r   Útest_groupby_raises_datetime_npK  s    
þýr{   rk   )rX   rP   r[   r]   c                 C   s&   |}|j dd�}ttdd|| g ƒ d S )Nr   rG   z/timedelta64 type does not support .* operationsr6   )r_   rF   ra   )rk   r.   r%   rB   r   r   r   Útest_groupby_raises_timedeltac  s    úr|   c              &   C   sj  |}t ||ƒ}|j|d�}|r@|d }|dkr@t|dƒr<J ‚d S dddtdfddttfdfttfdfttfdfttfd	ftd
fd|sŒtdfnddddddtd ddg¡ftd ddg¡fdddtdftdftdfdtd ddg¡fddtd ddg¡ftd ddg¡ftdftd ddg¡fdœ"| \}	}
|dk�rN|�r>dnd }|› d!�}nd"}t|	|
| ||||ƒ d S )#NrG   r!   rH   rI   ú=unsupported operand type\(s\) for \*: 'Categorical' and 'int'zz(category type does not support cummax operations|category dtype not supported|cummax is not supported for category dtype)zz(category type does not support cummin operations|category dtype not supported|cummin is not supported for category dtype)z|(category type does not support cumprod operations|category dtype not supported|cumprod is not supported for category dtype)zz(category type does not support cumsum operations|category dtype not supported|cumsum is not supported for category dtype)zDunsupported operand type\(s\) for -: 'Categorical' and 'Categorical'úSCannot setitem on a Categorical with a new category \(0\), set the categories firstrt   z2'Categorical' .* does not support reduction 'mean'ú2category dtype does not support aggregation 'mean'z4'Categorical' .* does not support reduction 'median'ú4category dtype does not support aggregation 'median'zDunsupported operand type\(s\) for /: 'Categorical' and 'Categorical'ú.category type does not support prod operationszNo matching signature foundú1'Categorical' .* does not support reduction 'sem'ú1category dtype does not support aggregation 'sem'ú0dtype category does not support reduction 'skew'ú.category type does not support skew operationsú1'Categorical' .* does not support reduction 'std'ú1category dtype does not support aggregation 'std'ú-category type does not support sum operationsú1'Categorical' .* does not support reduction 'var'ú1category dtype does not support aggregation 'var'rL   rR   r   r   r^   r3   )r   r_   r`   ra   rb   rw   rF   )rA   r   r   rC   Úusing_copy_on_writer2   r%   rD   rB   r?   r@   rf   rE   r   r   r   Útest_groupby_raises_categoryr  sÒ    
þþþþþþûþþÿþ
þÿþ	þþÿþ	þÿþ
þÿþ	þÿþ“vŠx
rŒ   c                 C   sd   |}|j |d�}|r|d }dd„ }tjtdd�� t|| ƒ|ƒ W d   ƒ n1 sV0    Y  d S )NrG   r!   c                 S   s   t dƒ‚d S rh   rj   r   r   r   r   rk     s    z.test_groupby_raises_category_udf.<locals>.funcri   r4   rl   )rA   r   r   r2   r%   rB   rk   r   r   r   Ú test_groupby_raises_category_udf  s    r�   c           
   	   C   sd   |}|j |d�}|r|d }tjtdftjtdfi| \}}|rFd}	nd}	t||| ||d|	d� d S )	NrG   r!   rˆ   r   ro   rp   r   rq   rz   )
rA   r   r   rn   r2   r%   rB   r?   r@   rE   r   r   r   Útest_groupby_raises_category_np  s     
þþúrŽ   c              &   C   sö  |}t g d¢g d¢dd�|d< t||ƒ}|j||d�}	|r\|	d }	|dkr\t|	dƒrXJ ‚d S | oxtd	d
„ |	j ¡ D ƒƒ}
|s¶| dkr¶t|tƒr¶t|d t	ƒr¶|ddgkr¶|
r²J ‚d}
| dkrÂd}
dddt
dfddtt
fdftt
fdftt
fdftt
fdft
dfd|�st
dfndd|
�r"tdfnd|
�r2tdfndddt
dft
dfdddt
dft
dft
dfdt
d ddg¡fddt
d dd g¡ft
d d!d"g¡ft
d#ft
d d$d%g¡fd&œ"| \}}|d'k�rÚ|�rÊd(nd)}|› d*�}nd}t||| |	|||ƒ d S )+Nr/   r   Tr0   r   )r   Úobservedr!   rH   c                 s   s   | ]}|j V  qd S r   )Úempty)Ú.0Úgroupr   r   r   Ú	<genexpr>H  r   z;test_groupby_raises_category_on_category.<locals>.<genexpr>r<   r   r   FrI   r}   zz(cummax is not supported for category dtype|category dtype not supported|category type does not support cummax operations)zz(cummin is not supported for category dtype|category dtype not supported|category type does not support cummin operations)z|(cumprod is not supported for category dtype|category dtype not supported|category type does not support cumprod operations)zz(cumsum is not supported for category dtype|category dtype not supported|category type does not support cumsum operations)rJ   r~   z(empty group due to unobserved categoriesr   r€   r�   r3   rt   r‚   rƒ   r…   r„   r†   r‡   rˆ   r‰   rŠ   rL   rR   r   r   r^   )r   r   r_   r`   rN   ÚgroupsÚvaluesÚ
isinstancer#   Ústrra   rb   re   rw   rF   )rA   r   r   rC   r�   r‹   r2   r%   rD   rB   Zempty_groupsr?   r@   rf   rE   r   r   r   Ú(test_groupby_raises_category_on_category-  sâ    ý

ÿþýü
ûþþþþþûþÿÿþÿþ	þÿþ
þÿþ	þÿþ¥dœf
r˜   c                  C   s�   t dgdgdgdœƒ} d}tjt|d�� | jddd�}W d   ƒ n1 sL0    Y  tjtd	d�� |d
  W d   ƒ n1 s‚0    Y  d S )Nr   r
   r   )r   r   r    z+DataFrame.groupby with axis=1 is deprecatedr4   r   )Zaxisz'Cannot subset columns when using axis=1r   )r   r9   r:   r8   r_   r=   r>   re   )r%   r@   rB   r   r   r   Ú%test_subsetting_columns_axis_1_raisesÅ  s    ,r™   )r3   )+r*   rc   Únumpyrr   r=   Zpandasr   r   r   r   Zpandas._testingZ_testingr9   Zpandas.tests.groupbyr   ZfixtureÚarrayÚdictÚzipr"   r   r   r&   r+   r.   r2   rF   ÚmarkZparametrizerg   rm   r\   rV   rs   rx   ry   r{   r|   rŒ   r�   rŽ   r˜   r™   r   r   r   r   Ú<module>   sv   
öÿ







S

E


 

 