a
    PfR>                     @   s  d dl Zd dlZd dl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mZmZ d dlmZ dd Zdd	 Zd
d Zdd Zejddedddddddddejg
i fdejdddddddddejg
i fdejdddddddddejg
i fdejddddddddd ejg
d!d"ifd#ej dddd$d%d&ddd'ejg
d!d"ifd(ej!ddddddddd)ejg
i fgd*d+ Z"d,d- Z#ejd.d/dddd0dd1ddejejg
fd2dddd3d4d5ddejejg
fgd6d7 Z$ejd8d9g d:gd;g d<ggd=d> Z%d?d@ Z&dAdB Z'dCdD Z(ejdEd"g dFfdGg dHfgejdId(g fdJd'gfgdKdL Z)ejdMeeegejdNg dOejdPd"d"gd d"gdQd"dRgd d"gdQd"gdS ejd"dRejge*e+dTdS dQgdUdV Z,ejdWd"d"d gd"gfd"dRd d"gd"dRgfdRd"d gd"gfdRdRd d"gdRdRgfdXdYe+dYe*e+dXdYdYgdX  fdYdXe+dXdXgdX fd d e-g e-g fd"d e-g e-g fd d"d gd gfg	dZd[ Z.ejd\eg d]g d^d_dReg d`ej/g d^e+dagdbdgdcddej0defeejd"dRejge*e+dTdf d"gdg dRgdh  e+dfdidYeg djej/d"gdg dRgdh  e+dfgdbdgdcddej0defgdkdl Z1ejdmg dnejdNg dodpdq Z2drds Z3dtdu Z4dS )v    N)	DataFrame
MultiIndexSeriesconcat
date_range)BaseIndexerFixedForwardWindowIndexer)ExpandingIndexerFixedWindowIndexerVariableOffsetWindowIndexer)BusinessDayc                  C   sZ   G dd dt } |  }tjtdd" ttd| W d    n1 sL0    Y  d S )Nc                   @   s   e Zd Zdd ZdS )z8test_bad_get_window_bounds_signature.<locals>.BadIndexerc                 S   s   d S N )selfr   r   ^/var/www/ai-form-bot/venv/lib/python3.9/site-packages/pandas/tests/window/test_base_indexer.pyget_window_bounds   s    zJtest_bad_get_window_bounds_signature.<locals>.BadIndexer.get_window_boundsN__name__
__module____qualname__r   r   r   r   r   
BadIndexer   s   r   zBadIndexer does not implementmatch   )r   pytestraises
ValueErrorr   rangerolling)r   indexerr   r   r   $test_bad_get_window_bounds_signature   s    r    c                  C   s<   t td} t }| | }|   }t|| d S )N
   )r   r   r	   r   meanZ	expandingtmassert_series_equal)sr   resultexpectedr   r   r   test_expanding_indexer#   s
    r(   c                  C   sb   g d} t dtdi}G dd dt}|d| d}|| }t dg di}t|| d S )	NTFTFTvaluesr   c                   @   s   e Zd Zdd ZdS )z3test_indexer_constructor_arg.<locals>.CustomIndexerc           	      S   sl   t j|t jd}t j|t jd}t|D ]:}| j| rLd||< |d ||< q(|||< || j ||< q(||fS Ndtyper      )npemptyint64r   use_expandingwindow_size	r   
num_valuesmin_periodscenterclosedstepstartendir   r   r   r   1   s    
zEtest_indexer_constructor_arg.<locals>.CustomIndexer.get_window_boundsNr   r   r   r   r   CustomIndexer0   s   r=   r.   r3   r2   )              ?      @rA         $@r   r   r   r   sumr#   assert_frame_equal)r2   dfr=   r   r&   r'   r   r   r   test_indexer_constructor_arg+   s    rG   c                  C   sb   t dtdi} G dd dt}|dd}| j|ddddd	 }t dg d
i}t|| d S )Nr*   r   c                   @   s   e Zd Zdd ZdS )z8test_indexer_accepts_rolling_args.<locals>.CustomIndexerc           	      S   s   t j|t jd}t j|t jd}t|D ]P}|rb|dkrb|dkrb|dkrb|dkrbd||< |||< q(|||< || j ||< q(||fS )Nr,   r.   both   r   )r/   r0   r1   r   r3   r4   r   r   r   r   G   s$    
zJtest_indexer_accepts_rolling_args.<locals>.CustomIndexer.get_window_boundsNr   r   r   r   r   r=   F   s   r=   r.   r3   TrH   )r7   r6   r8   r9   )r?   r@   rB   rA         @rC   )rF   r=   r   r&   r'   r   r   r   !test_indexer_accepts_rolling_argsC   s    


rL   zfunc,np_func,expected,np_kwargscountrA          @minr?   r@   rK         @      @       @max      Y@      "@stdgK@gDmK@g!?J@g'e?Zddofr.   varg[{@@g[{@g[{Ħ@g      ?mediang      !@c                    sv  t d}d|d< tdd}d}tjt|d. | |j|dd	}	t|	|  W d    n1 sb0    Y  d
}tjt|d. | |j|dd}	t|	|  W d    n1 s0    Y  | |j|d|d}	t|	| }
| |d d | }t	|
| | |	
 fdd}t	|
| |dkr,dnd }| |j||d}t|| }| |
 fdd}t	|| d S )NrB   rT   r      rJ   z.Forward-looking windows can't have center=Truer   T)windowr7   zAForward-looking windows don't support setting the closed argumentright)rZ   r8   rI   rZ   r6   r9   c                    s    | fi S r   r   xnp_func	np_kwargsr   r   <lambda>       z-test_rolling_forward_window.<locals>.<lambda>rM   r   rZ   r6   c                    s    | fi S r   r   r]   r_   r   r   rb      rc   )r/   aranger   r   r   r   r   getattrr#   assert_equalapply)frame_or_seriesfuncr`   r'   ra   r9   r*   r   r   r   r&   Z	expected2r6   Zrolling3Zresult3Z	expected3r   r_   r   test_rolling_forward_windowa   s,    9

**rk   c                 C   sr   t d}d|d< tdd}| |j|d|d}| }| ddd	d
ddddt jt jg
d d | }t|| d S )NrB   rT   r   rJ   rY   r\   r?   go@g\QJ@g@gv-@gA@)r/   re   r   r   Zskewnanr#   rg   )ri   r9   r*   r   r   r&   r'   r   r   r   test_rolling_forward_skewness   s*    

rm   zfunc,expectedcovg     @X@g     @Wcorrg?gh"?g1=c                 C   s   t ddd}|d }d|d< t j||gdd}tdd	}t|j|dd
}t||  jt	d dfdf }|j
dd}t|j
dd}|j|_t|| d S )Nr!   r.   rI   d   )r   r   )ZaxisrY   rJ   rd   r   T)Zdrop)r/   re   ZreshapeZconcatenater   r   r   rf   locsliceZreset_indexr   namer#   rg   )rj   r'   Zvalues1Zvalues2r*   r   r   r&   r   r   r   test_rolling_forward_cov_corr   s    
ru   zclosed,expected_datar[   )
r?   r@   rN   rA   rQ         (@rP   rQ   rR   rU   left)
r?   r?   r@   rN         @rU   rx   rP   rQ   rR   c                 C   s^   t ddd}ttd|d}td}t||d}|j|| d }t||d}t|| d S )N2020r!   Zperiods)indexr.   r{   offset)r8   )	r   r   r   r   r   r   rD   r#   rE   )r8   Zexpected_datar{   rF   r}   r   r&   r'   r   r   r   &test_non_fixed_variable_window_indexer   s    r~   c                   C   sB   t jtdd  tdtdd W d    n1 s40    Y  d S )Nzindex must be a DatetimeIndex.r   foor.   r|   )r   r   r   r   r   r   r   r   r   (test_variableoffsetwindowindexer_not_dti  s    r   c                  C   sJ   t ddd} tjtdd t| dd W d    n1 s<0    Y  d S )Nry   r!   rz   z(offset must be a DateOffset-like object.r   r   r|   )r   r   r   r   r   )idxr   r   r   +test_variableoffsetwindowindexer_not_offset  s    r   c                 C   sX   t dg di}tdd}|j|d| d }t dg did d |  }t|| d S )Nb)NNN   rI   rJ   r   r\   )r?   r?   r@   r@   )r   r   r   rM   r#   rE   )r9   rF   r   r&   r'   r   r   r    test_fixed_forward_indexer_count  s
    
r   )	end_valuer*   )r?   r.   r.   rY   rI   rp   )r?   r.   r   rY   r.   )rj   argsZquantilec           
         sh   G  fdddt }g d}tdtdi}|d|d}t|||| }td|i}	t||	 d S )Nc                       s   e Zd Z fddZdS )z0test_indexer_quantile_sum.<locals>.CustomIndexerc           	         sr   t j|t jd}t j|t jd}t|D ]@}| j| rRd||< t|  d||< q(|||< || j ||< q(||fS r+   )r/   r0   r1   r   r2   rS   r3   r4   r   r   r   r   ,  s    
zBtest_indexer_quantile_sum.<locals>.CustomIndexer.get_window_boundsNr   r   r   r   r   r=   +  s   r=   r)   r*   r   r.   r>   )r   r   r   rf   r   r#   rE   )
r   r*   rj   r   r=   r2   rF   r   r&   r'   r   r   r   test_indexer_quantile_sum%  s    r   indexer_classr3   )r.   rI      df_data)ar   rI         c                 C   sX   t |}d}| |d}|j}t|D ].}|dd j|dd  |j|ks$J q$d S )NrY   rJ   r   r   r.   rd   )r   r3   r   groupbyr   r"   )r   r3   r   rF   Z
num_trialsr   Zoriginal_window_sizer<   r   r   r   0test_indexers_are_reusable_after_groupby_rollingA  s    
r   z5window_size, num_values, expected_start, expected_endr   r   c                 C   st   t | d}|j||d\}}tj|t|d d | dd tj|t|d d | dd t|t|kspJ d S )NrJ   )r5   r9   F)Zcheck_dtype)r   r   r#   Zassert_numpy_array_equalr/   arraylen)r3   r5   Zexpected_startZexpected_endr9   r   r:   r;   r   r   r   !test_fixed_forward_indexer_boundsZ  s    
 r   zdf, window_size, expected)r   r.   rI   )r.   rI   rI   )r   r   )r   g      ?rN   rY   r   )namesr   )r{   rt   r-      r      )r   r   c)@r   g      @rx   rx   g      @rP   rv   g      )@g      *@g      +@g      ,@g      -@g      .@g      /@g      0@g     0@g      1@c                 C   s6   t |d}| dd j|dd }t|| d S )NrJ   r   r   r.   rd   )r   r   r   r"   r#   r$   )rF   r3   r'   r   r&   r   r   r   0test_rolling_groupby_with_fixed_forward_specificv  s    6
r   
group_keys)	)r.   )r.   rI   )rI   r.   )r.   r.   rI   )r.   rI   r.   )r.   r.   rI   rI   )r.   rI   rY   rI   rY   )r.   r.   rI   r.   r.   rI   r.   r.   rI   r.   r.   rI   )r.   rI   rY   r.   rI   rY   r.   rI   rY   r.   rI   rY   r.   rI   rY   )r.   rI   rY   r   r         c                    s   t tt| tjt| tjdd tjt| tjdd}t d}|	dd j
|dd }dd	g|j_|	dg d }t fd
d|D }|dd	gd }t|| d S )Nr,      )r   r   r   rJ   r   r   r.   rd   r   c                    s4   g | ],\}  j  fd dtt D dqS )c                    s*   g | ]"} d  j ||  jddqS )r   r.   )Z	min_count)ZilocrD   ).0r<   )gr3   r   r   
<listcomp>  s   zKtest_rolling_groupby_with_fixed_forward_many.<locals>.<listcomp>.<listcomp>)r   )Zassignr   r   )r   _rJ   )r   r   r     s   
z@test_rolling_groupby_with_fixed_forward_many.<locals>.<listcomp>)r   r/   r   listre   r   float64r1   r   r   r   rD   r{   r   r   Z	set_indexr#   r$   )r   r3   rF   r   r&   groupsZmanualr   rJ   r   ,test_rolling_groupby_with_fixed_forward_many  s"    

r   c                  C   s  G dd dt } |  }td|}d}tjt|d |  W d    n1 sT0    Y  tjt|d tt| W d    n1 s0    Y  tjt|d |j	dd W d    n1 s0    Y  tjt|d |j
dd W d    n1 s0    Y  d S )Nc                   @   s   e Zd Zdd ZdS )z4test_unequal_start_end_bounds.<locals>.CustomIndexerc                 S   s   t dgt ddgfS Nr.   rI   r/   r   r   r5   r6   r7   r8   r9   r   r   r   r     s    zFtest_unequal_start_end_bounds.<locals>.CustomIndexer.get_window_boundsNr   r   r   r   r   r=     s   r=   r.   r:   r   Tpairwiser   r   r   r   r   r   r"   nextiterro   rn   r=   r   Zrollr   r   r   r   test_unequal_start_end_bounds  s    &**r   c                  C   s  G dd dt } |  }tddg|}d}tjt|d |  W d    n1 sX0    Y  tjt|d tt| W d    n1 s0    Y  tjt|d |j	dd W d    n1 s0    Y  tjt|d |j
dd W d    n1 s0    Y  d S )Nc                   @   s   e Zd Zdd ZdS )z4test_unequal_bounds_to_object.<locals>.CustomIndexerc                 S   s   t dgt dgfS r   r   r   r   r   r   r     s    zFtest_unequal_bounds_to_object.<locals>.CustomIndexer.get_window_boundsNr   r   r   r   r   r=     s   r=   r.   zstart and endr   Tr   r   r   r   r   r   test_unequal_bounds_to_object  s    &**r   )5numpyr/   r   Zpandasr   r   r   r   r   Zpandas._testingZ_testingr#   Zpandas.api.indexersr   r   Zpandas.core.indexers.objectsr	   r
   r   Zpandas.tseries.offsetsr   r    r(   rG   rL   markZparametrizer   rl   rO   rS   rV   rW   rX   rk   rm   ru   r~   r   r   r   r   r   r   r   r   r   Zfrom_arraysr   r   r   r   r   r   r   r   r   <module>   s8  
 "5
*




	
("
 4
