3
สb_  ใ               @   sP   d dl Zd dlZd dlZd dlmZmZ d dljZ	d dl
mZ G dd dZdS )้    N)ฺ	DataFrameฺSeries)ฺget_groupbyc               @   sฦ   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(S ))ฺ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   ฺnpZarangeZseriesr   ฺframe)ฺselfฺmethodฉ r   ๚B/tmp/pip-build-jkwsxyiy/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&   r-   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ฺmaxr-   ฺkurtฺskewc                s   t | jd  S )Nr0   )ฺgetattrr"   )r$   )ฺfr   r   r%   @   s    z2TestGrouperGrouping.test_rolling.<locals>.<lambda>ฺstdฺvarr   )ฺddofc                s   t | jd ddS )Nr0   r   )r;   )r7   r"   )r$   )r8   r   r   r%   E   s    )r2   r#   r3   r4   r-   r5   r6   )r9   r:   )r   r   r"   r7   r&   r'   ฺassert_frame_equal)r   r    r.   r+   r*   r   )r8   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   r0   )r1   gู?)r>   c                s   | j djd dS )Nr0   gู?)r>   )r"   ฺquantile)r$   )r>   r   r   r%   P   s    z;TestGrouperGrouping.test_rolling_quantile.<locals>.<lambda>)r   r   r"   r@   r&   r'   r<   )r   r>   r    r.   r+   r*   r   )r>   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   r0   )r1   ฺcorrฺcovc                s   t | jd jS )Nr0   )r7   r"   r   )r$   )r8   r   r   r   ฺfunc[   s    z7TestGrouperGrouping.test_rolling_corr_cov.<locals>.funcT)ฺpairwisec                s   t | jjd ddS )Nr0   T)rE   )r7   r   r"   )r$   )r8   r   r   rD   c   s    )rB   rC   )	r   r   r"   r7   r&   r'   r<   r   r(   )r   r    r.   r+   rD   r*   r   )r8   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   r0   )r1   c             S   s   | j  S )N)r2   )r$   r   r   r   r%   n   s    z8TestGrouperGrouping.test_rolling_apply.<locals>.<lambda>)ฺrawc                s   | j djdd  dS )Nr0   c             S   s   | j  S )N)r2   )ฺyr   r   r   r%   o   s    zJTestGrouperGrouping.test_rolling_apply.<locals>.<lambda>.<locals>.<lambda>)rG   )r"   r&   )r$   )rG   r   r   r%   o   s    )r   r   r"   r&   r'   r<   )r   rG   r    r.   r+   r*   r   )rG   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   r0   ้   r   r	   g       @r   )ฺcolumnsฺindex)r1   )rJ   r   )rJ   r0   )rJ   rL   )r   r   )r   r   )r   r	   )ฺpdr   r   ฺ
MultiIndexฺfrom_tuplesฺnamesr   ฺnanr"   r2   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   r2   r#   r3   r4   r-   r5   r6   c                s   t | j   S )N)r7   ฺ	expanding)r$   )r8   r   r   r%      s    z4TestGrouperGrouping.test_expanding.<locals>.<lambda>r9   r:   r   )r;   c                s   t | j  ddS )Nr   )r;   )r7   rW   )r$   )r8   r   r   r%      s    )r2   r#   r3   r4   r-   r5   r6   )r9   r:   )r   r   rW   r7   r&   r'   r<   )r   r    r.   r+   r*   r   )r8   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ู?)r>   c                s   | j  jd dS )Ngู?)r>   )rW   r@   )r$   )r>   r   r   r%       s    z=TestGrouperGrouping.test_expanding_quantile.<locals>.<lambda>)r   r   rW   r@   r&   r'   r<   )r   r>   r    r.   r+   r*   r   )r>   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   rB   rC   c                s   t | j  jS )N)r7   rW   r   )r$   )r8   r   r   r   rD   ซ   s    z9TestGrouperGrouping.test_expanding_corr_cov.<locals>.funcT)rE   c                s   t | jj  ddS )NT)rE   )r7   r   rW   )r$   )r8   r   r   rD   ณ   s    )rB   rC   )	r   r   rW   r7   r&   r'   r<   r   r(   )r   r    r.   r+   rD   r*   r   )r8   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)r2   )r$   r   r   r   r%   พ   s    z:TestGrouperGrouping.test_expanding_apply.<locals>.<lambda>)rG   c                s   | j  jdd  dS )Nc             S   s   | j  S )N)r2   )rH   r   r   r   r%   ฟ   s    zLTestGrouperGrouping.test_expanding_apply.<locals>.<lambda>.<locals>.<lambda>)rG   )rW   r&   )r$   )rG   r   r   r%   ฟ   s    )r   r   rW   r&   r'   r<   )r   rG   r    r.   r+   r*   r   )rG   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ฺvaluer^   )rG   r   )rR   r_   )rN   ฺnameฉr   r   ฉr   r   ฉr   r	   )ra   rb   rc   )rO   r   r   r_   r"   r&   r   rP   rQ   r'   r(   )r   Zexpected_valueZ	raw_valuer   rT   r+   r*   r   r   r   ฺtest_groupby_rollingย   s    z(TestGrouperGrouping.test_groupby_rollingN)ฺ__name__ฺ
__module__ฺ__qualname__r   r!   r,   r/   r=   r   ฺmarkZparametrizerA   rF   rI   rV   rX   rY   rZ   r[   rd   r   r   r   r   r   
   s    		r   )Znumpyr   r   ZpandasrO   r   r   Zpandas._testingZ_testingr'   Zpandas.core.groupby.groupbyr   r   r   r   r   r   ฺ<module>   s   
