a
    j=ic=                     @   s  d dl Z 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 ejeddgddgddgddggdd gd	eddgddgddgddggddgd	eddgddgddgddggd
d
gd	eddgddgddgddggdd gd	eddgddgddgddggddgd	eddgddgddgddggd
dgd	eddgddgddgddggddgd	eddgddgddgddggd dgd	eddgddgddgddggddgd	g	ddd Zejdd Zejdd Zdd Zdd Zejdddgd d! Zejd"ddgd#d$ Zejd%ed&d'd( Zd)d* Zd+d, Zd-d. Zejd/d0d1 d2d1 gd3d4 Zejd/d5d1 d6d1 gd7d8 ZG d9d: d:Z dS );    N)	DataFrameIndex
MultiIndexSeries
date_range)	safe_sort               columnsC      ?g               @g      @g      @g       @X)paramsc                 C   s   | j S )zPairwise frames test_pairwise)param)request r   r/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/pandas/tests/window/test_pairwise.pypairwise_frames   s    r   c                   C   s(   t ddgddgddgddggddgdS )z'Pairwise target frame for test_pairwiser   r	   r
   r   r   r   r   r   r   r   r   r   pairwise_target_frame#   s    r   c                   C   s(   t g dg dg dg dgg ddS )z&Pairwise other frame for test_pairwise)Nr
   r
   )Nr
   r   )N   r   )Nr   r
   )YZr   r   r   r   r   r   r   pairwise_other_frame)   s    r   c              	   C   s\   | }|t jt| }|jddd|}t|d t |dd  |dd  d  d S )N2      windowZmin_periodsr   r
   )nprandomrandnlenrollingcovtmassert_almost_equal)seriesABresultr   r   r   test_rolling_cov2   s    r2   c              	   C   s   | }|t jt| }|jddd|}t|d t |dd  |dd  d  t	 }t	 }t j
|d d< t j
|d d< |jt|d	d|}t|d || d S )
Nr   r    r!   r#   r$   r%   r   
   r
   )r&   r'   r(   r)   r*   corrr,   r-   ZcorrcoefZmakeTimeSeriesnan)r.   r/   r0   r1   abr   r   r   test_rolling_corr:   s    ,r8   funcr+   r4   c                 C   sn   t |jddd|  }|jtd dfdf }|jd|_t |d jddd| |d }tj||dd d S )Nr3   r   r!   r
   FZcheck_names)getattrr*   locsliceindexZ	droplevelr,   assert_series_equal)r9   framer1   expectedr   r   r   test_rolling_pairwise_cov_corrK   s
     rB   methodc                    s    d t jdd }t  jdd} fdd}t|| t||   tjjj	 j
d d < t  jdd}t fdd D }t|| d S )Nr
   r3   r"   c                    s   t jdd | S )Nr3   rD   r;   r*   x)rC   r.   r   r   <lambda>Z       z(test_flex_binary_frame.<locals>.<lambda>c                    s,   i | ]$}|t  | jd d| qS )r3   rD   rE   ).0k)r@   frame2rC   r   r   
<dictcomp>d   rI   z*test_flex_binary_frame.<locals>.<dictcomp>)r;   r*   applyr,   assert_frame_equalcopyr&   r'   r(   shapevaluesr   )rC   r@   resZres2expZres3r   )r@   rL   rC   r.   r   test_flex_binary_frameT   s    rU   r"      c                 C   s@   t td}t td}|j| dj|d  s<J d S )N   rD   )other)r   r&   Zzerosaranger*   r4   Zisnaall)r"   srX   r   r   r   $test_rolling_corr_with_zero_variancei   s    r\   c               
   C   s   t tddgddgddgddgd	d
gddgg} | d jddd| d }tdd |D sdJ t tjdd} | d jddd| d }tdd |D sJ d S )NgF$?g)?gT5?g?'N?gFr1?g+	C-?gR	`?g1:b%?g9T!X4!?gXS_?gS= ?g\+Lt?r   r   T)centerr
   c                 s   s"   | ]}t t |d kV  qdS r
   Nr&   absZ
nan_to_numrJ   rG   r   r   r   	<genexpr>   rI   z#test_corr_sanity.<locals>.<genexpr>   r   c                 s   s"   | ]}t t |d kV  qdS r^   r_   ra   r   r   r   rb      rI   )r   r&   arrayr*   r4   rZ   r'   Zrand)dfrS   r   r   r   test_corr_sanityr   s     rf   c                  C   s   t g dg dd} t ddgddgd}| jddd|}t g d	}t|| t g d
g dd}| jddd|}t|| d S )Nr
   r   r   r   r
   r   r>   r
   r   r   r   r!   )NNr   r
   Nr   )r   r*   r+   r,   r?   s1s2r1   rA   Zs2ar   r   r   test_rolling_cov_diff_length   s    rn   c                  C   s   t g dg dd} t ddgddgd}| jddd|}t g d	}t|| t g d
g dd}| jddd|}t|| d S )Nrg   rh   ri   r
   r   r   r   r!   )NNr   rj   )r   r*   r4   r,   r?   rk   r   r   r   test_rolling_corr_diff_length   s    ro   fc                 C   s   | j dddj| ddS Nr3   r   r!   Tpairwiser*   r+   rF   r   r   r   rH      rI   rH   c                 C   s   | j dddj| ddS rq   r*   r4   rF   r   r   r   rH      rI   c                 C   s   t ddgddgddgddggtdd	gd
dttdddd}t tdd	gd
dtj|j|jgdd
gddd}| |}t|| d S )Nr
   r   r   r   	   r#   r   r/   r0   foonamer	   barr   r>   namesfloat64)r   r>   dtype)	r   r   ranger   from_productr>   r   r,   rO   )rp   re   Zdf_expectedZ	df_resultr   r   r   2test_rolling_functions_window_non_shrinkage_binary   s    
r   c                 C   s   | j dddj| ddS rq   rt   rF   r   r   r   rH      rI   c                 C   s   | j dddj| ddS rq   ru   rF   r   r   r   rH      rI   c                 C   s   t  }t tdgddtg ddd}|d d|d< t t|j|jgtg d}t tj|j|jgddgdtdgdddd	}| |}t|| | |}t|| d S )
Nr6   rw   rx   rz   r{   r~   r>   r   r|   )r>   r   r   )	r   r   Zastyper   r   r>   r   r,   rO   )rp   Zdf1Zdf2Zdf1_expectedZdf2_expectedZ
df1_resultZ
df2_resultr   r   r   *test_moment_functions_zero_length_pairwise   s    	r   c                
   @   sh  e Zd Zejddd dd gdd Zejddd dd d	d d
d dd dd gdd Zejddd dd dd dd dd dd gdd Zejddd dd dd dd dd dd gdd Z	ejddd d d d!d d"d d#d d$d gd%d& Z
ejdd'd d(d d)d d*d d+d d,d gd-d. Zd/d0 Zd1d2 Zd3d4 Zd5S )6TestPairwiserp   c                 C   s   |   S N)r+   rF   r   r   r   rH      rI   zTestPairwise.<lambda>c                 C   s   |   S r   )r4   rF   r   r   r   rH      rI   c                 C   sX   ||}t |j|j t |j|j ||}| j}| j}t j||dd d S NFZcheck_dtyper,   assert_index_equalr>   r   dropnarR   assert_numpy_array_equalselfr   r   rp   r1   rA   r   r   r   test_no_flex   s    

zTestPairwise.test_no_flexc                 C   s   |   jddS NTrr   	expandingr+   rF   r   r   r   rH      rI   c                 C   s   |   jddS r   r   r4   rF   r   r   r   rH      rI   c                 C   s   | j ddjddS Nr   rD   Trr   rt   rF   r   r   r   rH      rI   c                 C   s   | j ddjddS r   ru   rF   r   r   r   rH      rI   c                 C   s   | j ddjddS Nr   comTrr   ewmr+   rF   r   r   r   rH      rI   c                 C   s   | j ddjddS r   r   r4   rF   r   r   r   rH      rI   c                 C   s   ||}t j|jjd |jdd t t|jjd t|j  t |j|j ||}| j	}| j	}t j||dd d S Nr   Fr:   r
   r   
r,   r   r>   levelsr   r   r   uniquer   rR   r   r   r   r   test_pairwise_with_self   s    

z$TestPairwise.test_pairwise_with_selfc                 C   s   |   jddS NFrr   r   rF   r   r   r   rH     rI   c                 C   s   |   jddS r   r   rF   r   r   r   rH     rI   c                 C   s   | j ddjddS Nr   rD   Frr   rt   rF   r   r   r   rH     rI   c                 C   s   | j ddjddS r   ru   rF   r   r   r   rH     rI   c                 C   s   | j ddjddS Nr   r   Frr   r   rF   r   r   r   rH     rI   c                 C   s   | j ddjddS r   r   rF   r   r   r   rH     rI   c                 C   sX   ||}t |j|j t |j|j ||}| j}| j}t j||dd d S r   r   r   r   r   r   test_no_pairwise_with_self  s    

z'TestPairwise.test_no_pairwise_with_selfc                 C   s   |   j|ddS r   r   rG   yr   r   r   rH   (  rI   c                 C   s   |   j|ddS r   r   r   r   r   r   rH   )  rI   c                 C   s   | j ddj|ddS r   rt   r   r   r   r   rH   *  rI   c                 C   s   | j ddj|ddS r   ru   r   r   r   r   rH   +  rI   c                 C   s   | j ddj|ddS r   r   r   r   r   r   rH   ,  rI   c                 C   s   | j ddj|ddS r   r   r   r   r   r   rH   -  rI   c                 C   sx   |||}t j|jjd |jdd t t|jjd t|j  |||}| j	}| j	}t j||dd d S r   r   )r   r   r   r   rp   r1   rA   r   r   r   test_pairwise_with_other%  s    



z%TestPairwise.test_pairwise_with_otherc                 C   s   |   j|ddS r   r   r   r   r   r   rH   H  rI   c                 C   s   |   j|ddS r   r   r   r   r   r   rH   I  rI   c                 C   s   | j ddj|ddS r   rt   r   r   r   r   rH   J  rI   c                 C   s   | j ddj|ddS r   ru   r   r   r   r   rH   K  rI   c                 C   s   | j ddj|ddS r   r   r   r   r   r   rH   L  rI   c                 C   s   | j ddj|ddS r   r   r   r   r   r   rH   M  rI   c                 C   s  |j jr|||nd }|d urtjdd8 tdt |j|j}|j |j }W d    n1 sh0    Y  t	|j| t	|j | npt
jtdd ||| W d    n1 s0    Y  t
jtdd ||| W d    n1 s0    Y  d S )NT)recordignorez'arg1' columns are not unique)matchz'arg2' columns are not unique)r   Z	is_uniquewarningscatch_warningssimplefilterRuntimeWarningr>   unionr,   r   pytestZraises
ValueError)r   r   r   rp   r1   Zexpected_indexZexpected_columnsr   r   r   test_no_pairwise_with_otherE  s"    "(z(TestPairwise.test_no_pairwise_with_otherc                 C   s   |   |S r   r   r   r   r   r   rH   k  rI   c                 C   s   |   |S r   r   r   r   r   r   rH   l  rI   c                 C   s   | j dd|S Nr   rD   rt   r   r   r   r   rH   m  rI   c                 C   s   | j dd|S r   ru   r   r   r   r   rH   n  rI   c                 C   s   | j dd|S Nr   r   r   r   r   r   r   rH   o  rI   c                 C   s   | j dd|S r   r   r   r   r   r   rH   p  rI   c                 C   s   ||t g d}t|j|j t|j|j ||t g d}| j}| j}tj||dd |t g d|}t|j|j t|j|j |t g d|}| j}| j}tj||dd d S )N)r
   r
   r   r   Fr   )r   r,   r   r>   r   r   rR   r   r   r   r   r   test_pairwise_with_seriesh  s    



z&TestPairwise.test_pairwise_with_seriesc                 C   sT   t tdtdddd}|d|}t tjgd tdddd}t|| d S )Nr   Z2020)Zperiodsri   Z12H)	r   r   r   r*   r4   r&   r5   r,   r?   )r   r[   r1   rA   r   r   r   test_corr_freq_memory_error  s    z(TestPairwise.test_corr_freq_memory_errorc              	   C   s   t tdtdtdg}td}ttddd||d}|jdd		 }t tdtdtdtdg}t tdtdtdg}tt
td
tjtd
dtd
df||d}t|| d S )NabZxyZABr      r   r   g?)alpha)r   r   g      @@gK\ǸO@)r   r   listr   r   r&   rY   Zreshaper   r+   ZvstackfullNaNr,   rO   )r   r   r>   re   r1   rA   r   r   r   test_cov_mulittindex  s"    "

z!TestPairwise.test_cov_mulittindexc                 C   s   t jddgddggddgd}ttd|d	}|d
 }ttjt jtt	ddddgd ddgd gg dd|d}t
|| d S )NMNPQr6   r7   r|   )r   r   r   r   r   r   )Nr6   r7   r   )r   Zfrom_arraysr   r&   Zonesr*   r4   r5   repeatrY   r,   rO   )r   r   re   r1   rA   r   r   r   $test_multindex_columns_pairwise_func  s    &z1TestPairwise.test_multindex_columns_pairwise_funcN)__name__
__module____qualname__r   markparametrizer   r   r   r   r   r   r   r   r   r   r   r   r   r      sx   





r   )!r   numpyr&   r   Zpandasr   r   r   r   r   Zpandas._testingZ_testingr,   Zpandas.core.algorithmsr   Zfixturer   r   r   r2   r8   r   r   rB   rU   r   r\   rf   rn   ro   r   r   r   r   r   r   r   <module>   s`   &&&&&&&&&







