U
    -e}<                     @   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d8d9dddddd:ddddg
gd;ddddd<dd<dddg
ggd=d> Z%d?d@ Z&ejdAd"dd"d"dBdCgfdDdd"d dBd"gfgejdEd(g fdFd'gfgdGdH Z'ejdIeeegejdJd"dCdKgejdLd"d"gd d"gdMd"dCgd d"gdMd"gdN ejd"dCejge(e)dOdN dMgdPdQ Z*ejdRd"d"d gd"gfd"dCd d"gd"dCgfdCd"d gd"gfdCdCd d"gdCdCgfdSdKe)dKe(e)dSdKdKgdS  fdKdSe)dSdSgdS fd d e+g e+g fd"d e+g e+g fd d"d gd gfg	dTdU Z,ejdVed d"dCgd"dCdCgdWdCed dXdgej-d"dCdCge)dBgdYdgdZd[ej.d\feejd"dCejge(e)dOd] d"gd^ dCgd_  e)d]d`dKedadadbd<d<dcdd:dddedfdgdhdidjdkdldmgej-d"gd^ dCgd_  e)d]gdYdgdZd[ej.d\fgdndo Z/ejdpdqdrdsdtdudvdwdxdyg	ejdJd"dCdBdOdSdzd{gd|d} Z0d~d Z1dd Z2dS )    N)	DataFrame
MultiIndexSeriesconcat
date_range)BaseIndexerFixedForwardWindowIndexer)ExpandingIndexerFixedWindowIndexerVariableOffsetWindowIndexer)BusinessDayc               	   C   sF   G dd dt } |  }tjtdd ttd| W 5 Q R X 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   f/var/www/html/Darija-Ai-Train/env/lib/python3.8/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   sn   dddddg} t dtdi}G dd dt}|d| d}|| }t dd	d
dddgi}t|| d S )NTF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-   r2   r1                 ?      @      $@r   r   r   r   sumr#   assert_frame_equal)r1   dfr<   r   r&   r'   r   r   r   test_indexer_constructor_arg+   s    rF   c                  C   sh   t dtdi} G dd dt}|dd}| j|ddddd	 }t dd
ddddg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.   r/   r0   r   r2   r3   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-   r2   TrG   )r6   r5   r7   r8   r>   r?   rA   r@         @rB   )rE   r<   r   r&   r'   r   r   r   !test_indexer_accepts_rolling_argsC   s    
    
rK   zfunc,np_func,expected,np_kwargscountr@   g       @minr>   r?   rJ   g      @g      @g       @max      Y@g      "@stdgK@gDmK@g!?J@g'e?Zddofr-   varg[{@@g[{@g[{Ħ@g      ?mediang      !@c              	      sN  t d}d|d< tdd}d}tjt|d$ | |j|dd	}	t|	|  W 5 Q R X d
}tjt|d$ | |j|dd}	t|	|  W 5 Q R X | |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 )NrA   rO   r      rI   z.Forward-looking windows can't have center=Truer   T)windowr6   zAForward-looking windows don't support setting the closed argumentright)rT   r7   rH   rT   r5   r8   c                    s    | fS r   r   xnp_func	np_kwargsr   r   <lambda>       z-test_rolling_forward_window.<locals>.<lambda>rL   r   rT   r5   c                    s    | fS r   r   rW   rY   r   r   r\      r]   )r.   aranger   r   r   r   r   getattrr#   assert_equalapply)frame_or_seriesfuncrZ   r'   r[   r8   r)   r   r   r   r&   Z	expected2r5   Zrolling3Zresult3Z	expected3r   rY   r   test_rolling_forward_windowa   s,    9

re   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 )NrA   rO   r   rI   rS   rV   r>   go@g\QJ@g@gv-@gA@)r.   r_   r   r   Zskewnanr#   ra   )rc   r8   r)   r   r   r&   r'   r   r   r   test_rolling_forward_skewness   s*    

rg   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-   rH   d   )r   r   )ZaxisrS   rI   r^   r   T)Zdrop)r.   r_   ZreshapeZconcatenater   r   r   r`   locsliceZreset_indexr   namer#   ra   )rd   r'   Zvalues1Zvalues2r)   r   r   r&   r   r   r   test_rolling_forward_cov_corr   s    
ro   zclosed,expected_datarU   g      (@leftg      @c                 C   s^   t ddd}ttd|d}td}t||d}|j|| d }t||d}t|| d S )NZ2020r!   )Zperiods)indexr-   )rq   offset)r7   )	r   r   r   r   r   r   rC   r#   rD   )r7   Zexpected_datarq   rE   rr   r   r&   r'   r   r   r   &test_non_fixed_variable_window_indexer   s    rs   c                 C   s`   t dd d d dgi}tdd}|j|d| d }t dddddgid d |  }t|| d S )	Nb   rH   rI   r   rV   r>   r?   )r   r   r   rL   r#   rD   )r8   rE   r   r&   r'   r   r   r    test_fixed_forward_indexer_count  s
    
rv   )	end_valuer)   rS   rH   rj   )rd   argsZquantilec           
         sn   G  fdddt }dddddg}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.   r/   r0   r   r1   rN   r2   r3   rw   r   r   r     s    
zBtest_indexer_quantile_sum.<locals>.CustomIndexer.get_window_boundsNr   r   ry   r   r   r<     s   r<   TFr)   r   r-   r=   )r   r   r   r`   r   r#   rD   )
rw   r)   rd   rx   r<   r1   rE   r   r&   r'   r   ry   r   test_indexer_quantile_sum  s    rz   indexer_classr2      df_data)art         c                 C   sX   t |}d}| |d}|j}t|D ].}|dd j|dd  |j|ks$tq$d S )NrS   rI   r~   rt   r-   r^   )r   r2   r   groupbyr   r"   AssertionError)r{   r2   r}   rE   Z
num_trialsr   Zoriginal_window_sizer;   r   r   r   0test_indexers_are_reusable_after_groupby_rolling4  s    
r   z5window_size, num_values, expected_start, expected_endr   c                 C   st   t | d}|j||d\}}tj|t|d d | dd tj|t|d d | dd t|t|ksptd S )NrI   )r4   r8   F)Zcheck_dtype)r   r   r#   Zassert_numpy_array_equalr.   arraylenr   )r2   r4   Zexpected_startZexpected_endr8   r   r9   r:   r   r   r   !test_fixed_forward_indexer_boundsM  s    
   r   zdf, window_size, expected)rt   r~   g      ?r~   )namesrt   )rq   rn   r,      ru      )rt   r~   cg@g      @g      @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 )NrI   r~   rt   r-   r^   )r   r   r   r"   r#   r$   )rE   r2   r'   r   r&   r   r   r   0test_rolling_groupby_with_fixed_forward_specifici  s    6
r   
group_keys)r-   )r-   rH   )rH   r-   )r-   r-   rH   )r-   rH   r-   )r-   r-   rH   rH   )r-   rH   rS   rH   rS   )r-   r-   rH   r-   r-   rH   r-   r-   rH   r-   r-   rH   )r-   rH   rS   r-   rH   rS   r-   rH   rS   r-   rH   rS   r-   rH   rS         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ddd	g }t fd
d|D }|dd	gd }t|| d S )Nr+      )r~   rt   r   rI   r~   rt   r-   r^   r   c                    s4   g | ],\}  j  fd dtt D dqS )c                    s*   g | ]"} d  j ||  jddqS )rt   r-   )Z	min_count)ZilocrC   ).0r;   )gr2   r   r   
<listcomp>  s   zKtest_rolling_groupby_with_fixed_forward_many.<locals>.<listcomp>.<listcomp>)rt   )Zassignr   r   )r   _rI   )r   r   r     s   
z@test_rolling_groupby_with_fixed_forward_many.<locals>.<listcomp>)r   r.   r   listr_   r   float64r0   r   r   r   rC   rq   r   r   Z	set_indexr#   r$   )r   r2   rE   r   r&   groupsZmanualr   rI   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 5 Q R X tjt|d tt| W 5 Q R X tjt|d |j	dd W 5 Q R X tjt|d |j
dd W 5 Q R X 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-   rH   r.   r   r   r4   r5   r6   r7   r8   r   r   r   r     s    zFtest_unequal_start_end_bounds.<locals>.CustomIndexer.get_window_boundsNr   r   r   r   r   r<     s   r<   r-   r9   r   Tpairwiser   r   r   r   r   r   r"   nextiterri   rh   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 5 Q R X tjt|d tt| W 5 Q R X tjt|d |j	dd W 5 Q R X tjt|d |j
dd W 5 Q R X 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   )3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(   rF   rK   markZparametrizer   rf   rM   rN   rP   rQ   rR   re   rg   ro   rs   rv   rz   r   r   r   r   r   Zfrom_arraysr   r   r   r   r   r   r   r   r   <module>   sp  
 "5
*


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