3
ฎybrK  ใ               @   sX   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	j
Zd dlmZ G dd dZdS )้    N)ฺ	DataFrameฺIndexฺ
MultiIndexฺSeries)ฺget_groupbyc               @   s8  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zej	j
ddddddgdd Zdd Zdd Zdd Zdd Zej	j
ddddddgdd Zdd Zdd  Zej	j
d!d"d#gd$d%ggd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zej	j
d2d3d4gd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ ZdAdB ZdCS )DฺTestGrouperGroupingc             C   sD   t tjd| _tdgd dgd  dgd  tjdd	| _d S )
N้
   ้   ้   ้   ้   ้   ้   ้(   )ฺAฺB)r   ฺnpฺarangeฺseriesr   ฺframe)ฺselfฺmethodฉ r   ๚D/tmp/pip-install-4cy230dp/pandas/pandas/tests/window/test_grouper.pyฺsetup_method   s    z TestGrouperGrouping.setup_methodc             C   sd   d}t jt|d | jjddd W d Q R X | jjd}|j sFtt| jddd}|js`td S )Nz4groupby\(\) got an unexpected keyword argument 'foo')ฺmatchr   r	   )ฺfooT)ฺbyฺmutated)ฺpytestZraisesฺ	TypeErrorr   ฺgroupbyr   ฺAssertionErrorr   )r   ฺmsgฺgr   r   r   ฺtest_mutated   s    z TestGrouperGrouping.test_mutatedc             C   sฌ   | j jd}t| j ddd}|jjdd }|jdj j}tj|| |jdjj }tj|| |jjdj }tj|| | j jj| j j	jdj }tj|| d S )Nr   T)r   r   c             S   s   | j dj S )Nr   )ฺrollingฺmean)ฺxr   r   r   ฺ<lambda>   s    z2TestGrouperGrouping.test_getitem.<locals>.<lambda>r   )
r   r!   r   r   ฺapplyr&   r'   ฺtmฺassert_series_equalr   )r   r$   ฺ	g_mutatedฺexpectedฺresultr   r   r   ฺtest_getitem   s    z TestGrouperGrouping.test_getitemc             C   sf   | j jd}|jd}t| j ddd}|jjdd }|jj }tj|| |jj }tj|| d S )Nr   r   T)r   r   c             S   s   | j dj S )Nr   )r&   ฺcount)r(   r   r   r   r)   2   s    z;TestGrouperGrouping.test_getitem_multiple.<locals>.<lambda>)	r   r!   r&   r   r   r*   r1   r+   r,   )r   r$   ฺrr-   r.   r/   r   r   r   ฺtest_getitem_multiple,   s    


z)TestGrouperGrouping.test_getitem_multiplec                s   | j jd}|jdd}x6dD ]. t|  }|j fdd}tj|| qW x:dD ]2 t| dd}|j fdd}tj|| qVW d S )Nr   ้   )ฺwindowฺsumr'   ฺminฺmaxr1   ฺkurtฺskewc                s   t | jd  S )Nr4   )ฺgetattrr&   )r(   )ฺfr   r   r)   @   s    z2TestGrouperGrouping.test_rolling.<locals>.<lambda>ฺstdฺvarr	   )ฺddofc                s   t | jd ddS )Nr4   r	   )r?   )r;   r&   )r(   )r<   r   r   r)   E   s    )r6   r'   r7   r8   r1   r9   r:   )r=   r>   )r   r!   r&   r;   r*   r+   ฺassert_frame_equal)r   r$   r2   r/   r.   r   )r<   r   ฺtest_rolling:   s    

z TestGrouperGrouping.test_rollingฺinterpolationZlinearฺlowerZhigherZmidpointZnearestc                sH   | j jd}|jdd}|jd d}|j fdd}tj|| d S )Nr   r4   )r5   gู?)rB   c                s   | j djd dS )Nr4   gู?)rB   )r&   ฺquantile)r(   )rB   r   r   r)   P   s    z;TestGrouperGrouping.test_rolling_quantile.<locals>.<lambda>)r   r!   r&   rD   r*   r+   r@   )r   rB   r$   r2   r/   r.   r   )rB   r   ฺtest_rolling_quantileH   s    z)TestGrouperGrouping.test_rolling_quantilec                s   j jd}|jdd}xtdD ]l t| j } fdd}|j|}tj|| t|j dd	} fd
d}|j|}tj|| qW d S )Nr   r4   )r5   ฺcorrฺcovc                s   t | jd jS )Nr4   )r;   r&   r   )r(   )r<   r   r   r   ฺfunc[   s    z7TestGrouperGrouping.test_rolling_corr_cov.<locals>.funcT)ฺpairwisec                s   t | jjd ddS )Nr4   T)rI   )r;   r   r&   )r(   )r<   r   r   rH   c   s    )rF   rG   )	r   r!   r&   r;   r*   r+   r@   r   r,   )r   r$   r2   r/   rH   r.   r   )r<   r   r   ฺtest_rolling_corr_covT   s    


z)TestGrouperGrouping.test_rolling_corr_covc                sL   | j jd}|jdd}|jdd  d}|j fdd}tj|| d S )Nr   r4   )r5   c             S   s   | j  S )N)r6   )r(   r   r   r   r)   n   s    z8TestGrouperGrouping.test_rolling_apply.<locals>.<lambda>)ฺrawc                s   | j djdd  dS )Nr4   c             S   s   | j  S )N)r6   )ฺyr   r   r   r)   o   s    zJTestGrouperGrouping.test_rolling_apply.<locals>.<lambda>.<locals>.<lambda>)rK   )r&   r*   )r(   )rK   r   r   r)   o   s    )r   r!   r&   r*   r+   r@   )r   rK   r$   r2   r/   r.   r   )rK   r   ฺtest_rolling_applyi   s
    z&TestGrouperGrouping.test_rolling_applyc             C   sด   t jdgd dgd  dgd d}|jd}t jjddddddg}dd g|_t jtjddgd dg|d}|jddj	 }t
j|| |j	  |jddj	 }t
j|| d S )Nr   r   ฺbarr	   ้   )r   r   r   r4   ้   r   r   g       @r   )ฺcolumnsฺindex)r5   )rN   r   )rN   r4   )rN   rP   )r   r   )r   r	   )r   r   )ฺpdr   r!   r   ฺfrom_tuplesฺnamesr   ฺnanr&   r6   r+   r@   )r   ฺdfr$   ฺmir.   r/   r   r   r   ฺtest_rolling_apply_mutabilityr   s    &

z1TestGrouperGrouping.test_rolling_apply_mutabilityc                s   | j jd}|j }x6dD ]. t|  }|j fd	d
}tj|| qW x:dD ]2 t| dd}|j fdd
}tj|| qRW d S )Nr   r6   r'   r7   r8   r1   r9   r:   c                s   t | j   S )N)r;   ฺ	expanding)r(   )r<   r   r   r)      s    z4TestGrouperGrouping.test_expanding.<locals>.<lambda>r=   r>   r   )r?   c                s   t | j  ddS )Nr   )r?   )r;   rZ   )r(   )r<   r   r   r)      s    )r6   r'   r7   r8   r1   r9   r:   )r=   r>   )r   r!   rZ   r;   r*   r+   r@   )r   r$   r2   r/   r.   r   )r<   r   ฺtest_expanding   s    

z"TestGrouperGrouping.test_expandingc                sD   | j jd}|j }|jd d}|j fdd}tj|| d S )Nr   gู?)rB   c                s   | j  jd dS )Ngู?)rB   )rZ   rD   )r(   )rB   r   r   r)       s    z=TestGrouperGrouping.test_expanding_quantile.<locals>.<lambda>)r   r!   rZ   rD   r*   r+   r@   )r   rB   r$   r2   r/   r.   r   )rB   r   ฺtest_expanding_quantile   s    z+TestGrouperGrouping.test_expanding_quantilec                s   j jd}|j }xtd	D ]l t| j } fdd}|j|}tj|| t|j dd} fdd}|j|}tj|| qW d S )
Nr   rF   rG   c                s   t | j  jS )N)r;   rZ   r   )r(   )r<   r   r   r   rH   ซ   s    z9TestGrouperGrouping.test_expanding_corr_cov.<locals>.funcT)rI   c                s   t | jj  ddS )NT)rI   )r;   r   rZ   )r(   )r<   r   r   rH   ณ   s    )rF   rG   )	r   r!   rZ   r;   r*   r+   r@   r   r,   )r   r$   r2   r/   rH   r.   r   )r<   r   r   ฺtest_expanding_corr_covค   s    


z+TestGrouperGrouping.test_expanding_corr_covc                sH   | j jd}|j }|jdd  d}|j fdd}tj|| d S )Nr   c             S   s   | j  S )N)r6   )r(   r   r   r   r)   พ   s    z:TestGrouperGrouping.test_expanding_apply.<locals>.<lambda>)rK   c                s   | j  jdd  dS )Nc             S   s   | j  S )N)r6   )rL   r   r   r   r)   ฟ   s    zLTestGrouperGrouping.test_expanding_apply.<locals>.<lambda>.<locals>.<lambda>)rK   )rZ   r*   )r(   )rK   r   r   r)   ฟ   s    )r   r!   rZ   r*   r+   r@   )r   rK   r$   r2   r/   r.   r   )rK   r   ฺtest_expanding_applyน   s
    z(TestGrouperGrouping.test_expanding_applyzexpected_value,raw_valueg      ๐?Tg        Fc             C   st   dd }t jdddgdddgd}|jdjjdj||d}t|gd t jjddd gd
dd}t	j
|| d S )Nc             S   s   t t| tjS )N)ฺintฺ
isinstancer   Zndarray)r(   r   r   r   r   ฦ   s    z5TestGrouperGrouping.test_groupby_rolling.<locals>.foor	   r   r   )ฺidฺvaluera   )rK   r   )rU   rb   )rR   ฺnameฉr	   r   ฉr	   r	   ฉr	   r   )rd   re   rf   )rS   r   r!   rb   r&   r*   r   r   rT   r+   r,   )r   Zexpected_valueZ	raw_valuer   rW   r/   r.   r   r   r   ฺtest_groupby_rollingย   s    z(TestGrouperGrouping.test_groupby_rollingc             C   sฎ  t tdd}|j|jdddj }t tjgd tjj	dd
}t
j|| t tdd}|j|jdddj }t tjgd	 tjj	dd
}t
j|| tjdgd dgd  tdd}|jdjdddj }tjtjdddtjtjddddtjgtjj	d+dd gddgd}t
j|| tjdgd dgd  tdd}|jdjdddj }tjtjdddtjtjdddtjg
tjj	d6dd gddgd}t
j|| d S )7Nr	   rO   Tr   )ฺcenterr5   rP   r   r   r4   )rR   ฺaฺb้   )ri   rj   ้   r   ้	   r   )rU   )rR   rQ   ฉr	   r   ฉr   r	   ฉr   r   ฉr4   r   ฉrP   r4   )rn   ro   rp   rq   rr   ฉr	   r   ฉr   r	   ฉr   r   ฉr4   r   )rs   rt   ru   rv   ฉri   r   ฉri   r	   ฉri   r   ฉri   r   ฉri   r4   ฉrj   rP   ฉrj   rO   ฉrj   rl   ฉrj   r   ฉrj   rm   ฉrj   r   )rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   ฉri   r   ฉri   r	   ฉri   r   ฉri   r   ฉri   r4   ฉrj   rP   ฉrj   rO   ฉrj   rl   ฉrj   r   ฉrj   rm   )
r   r   r   r   r   r   r   r   r   r   )r   ฺranger!   r&   r'   r   rV   rS   r   rT   r+   r,   r   r@   )r   r   r/   r.   rW   r   r   r   ฺ"test_groupby_rolling_center_centerิ   sb    

$           

$         

z6TestGrouperGrouping.test_groupby_rolling_center_centerc          	   C   sะ   t ddddgddddgdd	d
d	gd}|jddg jdj }t tjtjdtjgdgtjjddd gdd}t	j
|| |jdd jdj }ttjtjdtjgtjjddd gddd}t	j|| d S )Nr	   r   r   g      @g       @g      @g      ๐?r   r
   ้   )ri   rj   ฺcri   rj   r   )rU   )rQ   rR   )rR   rc   ฉ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   r!   r&   r8   r   rV   rS   r   rT   r+   r@   r   r,   )r   rW   r/   r.   r   r   r   ฺtest_groupby_subselect_rolling  s     &z2TestGrouperGrouping.test_groupby_subselect_rollingc             C   s   G dd dt jjj}t jddddddgd	 id
gd dgd  dgd  d}|j|jj|d	dddj }|j|jjd	ddj }t	j
|| d S )Nc               @   s   e Zd ZdddZdS )zNTestGrouperGrouping.test_groupby_rolling_custom_indexer.<locals>.SimpleIndexerr   Nc             S   sH   |d kr| j nd}tj|tjdd }|j | j  }|||dk < ||fS )Nr   )ฺdtyper	   )ฺwindow_sizer   r   ฺint64ฺcopy)r   Z
num_valuesฺmin_periodsrh   ฺclosedฺendฺstartr   r   r   ฺget_window_bounds1  s
    z`TestGrouperGrouping.test_groupby_rolling_custom_indexer.<locals>.SimpleIndexer.get_window_bounds)r   NNN)ฺ__name__ฺ
__module__ฺ__qualname__rก   r   r   r   r   ฺSimpleIndexer0  s   rฅ   ri   g      ๐?g       @g      @g      @g      @r   r   rP   r	   r   )rR   )r   )r   )r5   r   )rS   ฺapiZindexersZBaseIndexerr   r!   rR   r&   r6   r+   r@   )r   rฅ   rW   r/   r.   r   r   r   ฺ#test_groupby_rolling_custom_indexer.  s    
6z7TestGrouperGrouping.test_groupby_rolling_custom_indexerc             C   sฐ   t jtdtddddg t jdgd d}|jdjdd	d
dd j }ttj	ddtj	ddgt j
jdt jdfgd dt jdfgd  dd	gddd}tj|| d S )NrO   r   r   r   z
2019-01-01)ฺcolumn1ฺcolumn2ฺgroupฺdaterช   ฺ1Drซ   ฺleft)ฺonr   rจ   g        g       @g      ๐?g      @)rU   )rR   rc   )rS   r   r   ฺ	Timestampr!   r&   r6   r   r   rV   r   rT   r+   r,   )r   rW   r/   r.   r   r   r   ฺ'test_groupby_rolling_subset_with_closedE  s    

z;TestGrouperGrouping.test_groupby_rolling_subset_with_closedc             C   sธ   t jtdtddddg t jdgd d}|jddd	g jd
d	ddd j }ttj	ddtj	ddgt j
jdt jdfgd dt jdfgd  dd	gddd}tj|| d S )NrO   r   r   r   z
2019-01-01)rจ   rฉ   rช   rซ   rช   rจ   rซ   rฌ   rญ   )rฎ   r   g        g       @g      ๐?g      @)rU   )rR   rc   )rS   r   r   rฏ   r!   r&   r6   r   r   rV   r   rT   r+   r,   )r   rW   r/   r.   r   r   r   ฺ.test_groupby_subset_rolling_subset_with_closed]  s    

zBTestGrouperGrouping.test_groupby_subset_rolling_subset_with_closedrH   r8   r7   c             C   s   t dddgtjjdddgdd	gd
dd}t|j|jd| }t tjtjdgtjjdddgddd	gd
dd}t	j
|| d S )Nr	   r   ri   r(   rL   r   ฺzฺ1ฺ2)rU   )rR   rc   g       @)ri   r(   )ri   rL   )r   rฒ   )r	   ri   r(   )r   ri   rL   )r   r   rฒ   )r   rS   r   rT   r;   r!   r&   r   rV   r+   r,   )r   rH   Zdsr/   r.   r   r   r   ฺ"test_groupby_rolling_index_changedx  s    z6TestGrouperGrouping.test_groupby_rolling_index_changedc             C   sย   t dg i}|jdjddj }tjtg ddtg ddgdd gd|_tj	|| t g g d}|jdd	gjddj }tjtg ddtg ddtg ddgdd	d gd|_tj	|| d S )
Nฺs1r	   )r5   Zfloat64)r   r   )rU   )rถ   ฺs2rท   )
r   r!   r&   r6   r   Zfrom_productr   rR   r+   r@   )r   r.   r/   r   r   r   ฺ test_groupby_rolling_empty_frame  s    $

z4TestGrouperGrouping.test_groupby_rolling_empty_framec             C   sN  t jddt jddddgddt jddddgdd	t jddd
dgddt jddddgdd	t jddddggdddgdjd}|jd}|j |d< |jddd}|jdd }t jddt jdddddgddt jdddddgddt jdddddgdd	t jddd
ddgdd	t jdddddggddddgdjddg}tj	|| d S )Nr   Zgroup_1iใ  r	   rm   r   r   ฺZZgroup_2r   ฺHrO   ฺEr
   rR   rช   Z	eventTime)rQ   Zcount_to_dateZ10d)rฎ   c             S   s
   | j d S )Nr   )ฺshape)rW   r   r   r   r)   ด  s    zGTestGrouperGrouping.test_groupby_rolling_string_index.<locals>.<lambda>g      ๐?g       @g      @)
rS   r   rฏ   ฺ	set_indexr!   Zcumcountr&   r*   r+   r@   )r   rW   ฺgroupsZrolling_groupsr/   r.   r   r   r   ฺ!test_groupby_rolling_string_indexค  s*    

z5TestGrouperGrouping.test_groupby_rolling_string_indexc             C   sv   t jddgddgdjdddjdj }t jtjddgddggdd	gt jjddgdd gdd}t	j
|| d S )Nr   r	   )r   rN   r   F)ฺsortg       @g      ๐?rN   r   )rU   )rQ   rR   )r   r   )r	   r	   )rS   r   r!   r&   r7   r   ฺarrayr   rT   r+   r@   )r   r/   r.   r   r   r   ฺtest_groupby_rolling_no_sortม  s    

z0TestGrouperGrouping.test_groupby_rolling_no_sortc             C   s   dddgdddgg}t j|dd}tdddg|d	}|jddgd
djdj }tdddgt jdddgddgdd	}tj|| d S )Nฺval1ฺval2ฺidx1ฺidx2)rU   r	   r   r   )rR   F)Z
group_keysg      ๐?g       @g      @)rล   rฦ   )rร   rร   )rร   rร   )rฤ   rฤ   )	r   ฺfrom_arraysr   r!   r&   r'   rT   r+   r,   )r   ฺarraysrR   ฺsr/   r.   r   r   r   ฺtest_groupby_rolling_group_keysะ  s    z3TestGrouperGrouping.test_groupby_rolling_group_keysc             C   s   dddgdddgg}t j|dd}tdddgtdd	|d
}|jddgjdj }tddddgit jdddgddgdd
}tj	|| d S )Nrร   rฤ   rล   rฦ   )rU   r	   r   r   )r   r   )rR   r   r   g        g      ๐?g       @)rล   rฦ   )rร   r	   )rร   r	   )rฤ   r   )
r   rว   r   r   r!   r&   r'   rT   r+   r@   )r   rศ   rR   rW   r/   r.   r   r   r   ฺ1test_groupby_rolling_index_level_and_column_labelเ  s    zETestGrouperGrouping.test_groupby_rolling_index_level_and_column_labelc             C   s^  t tjdddgd d}|jdjdj }tjddddddddgdd gd}tj	|j
| t tjdddgd	 ddddgd d}|jddgjdj }tjddd d!d"d#d$d%d&d'd(d)gddd gd}tj	|j
| t tjdddgd ddddgd d}|jddd}|jdjdj }tjd*d+d,d-d.d/d0d1gdd dgd}tj	|j
| d S )2Ng       @r	   r   r4   )ri   rj   rj   r   r   rO   rP   rl   )rU   g      (@)ri   rj   r   r   r   r   rm   rk   T)ฺappend)r	   r   )r	   r   )r	   r4   )r	   rO   )r   r	   )r   r   )r   rP   )r   rl   )r	   r	   r   )r	   r	   r4   )r	   r	   r   )r	   r   r   )r	   r   rO   )r	   r   r   )r   r   r	   )r   r   rP   )r   r   rm   )r   r4   r   )r   r4   rl   )r   r4   rk   )r	   r   r	   )r	   r   r   )r	   r4   r	   )r	   rO   r   )r   r	   r   )r   r   r4   )r   rP   r   )r   rl   r4   )r   r   r   r!   r&   r'   r   rT   r+   Zassert_index_equalrR   r6   rฝ   )r   rW   r/   Zexpected_indexr   r   r   ฺ)test_groupby_rolling_resulting_multiindex๎  sJ    **z=TestGrouperGrouping.test_groupby_rolling_resulting_multiindexN) rข   rฃ   rค   r   r%   r0   r3   rA   r   ฺmarkZparametrizerE   rJ   rM   rY   r[   r\   r]   r^   rg   r   r   rง   rฐ   rฑ   rต   rธ   rฟ   rย   rส   rห   rอ   r   r   r   r   r   
   s8   		"Ar   )Znumpyr   r   ZpandasrS   r   r   r   r   Zpandas._testingZ_testingr+   Zpandas.core.groupby.groupbyr   r   r   r   r   r   ฺ<module>   s   
