o
    dq=                     @   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   U/app/.heroku/python/lib/python3.10/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   0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tjj	j
 jj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>r>   r   c                    s,   i | ]}|t  | jd d| qS )r3   rD   rE   ).0k)r@   frame2rC   r   r   
<dictcomp>f   s   , z*test_flex_binary_frame.<locals>.<dictcomp>)r;   r*   applyr,   assert_frame_equalcopyr   r&   r'   r(   shaper>   r   )rC   r@   resZres2expZres3r   )r@   rM   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_variancek   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 s2J t tjdd} | d jddd| d }tdd |D sTJ 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   $    | ]}t t |d kV  qdS r
   Nr&   absZ
nan_to_numrK   rG   r   r   r   	<genexpr>      " z#test_corr_sanity.<locals>.<genexpr>   r   c                 s   r^   r_   r`   rb   r   r   r   rc      rd   )r   r&   arrayr*   r4   rZ   r'   Zrand)dfrS   r   r   r   test_corr_sanityt   s    rh   c                  C      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      rq   c                  C   ri   )Nrj   rk   rl   r
   r   r   r   r!   )NNr   rm   )r   r*   r4   r,   r?   rn   r   r   r   test_rolling_corr_diff_length   rr   rs   fc                 C      | j dddj| ddS Nr3   r   r!   Tpairwiser*   r+   rF   r   r   r   rH          rH   c                 C   ru   rv   r*   r4   rF   r   r   r   rH      rz   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,   rP   )rt   rg   Zdf_expectedZ	df_resultr   r   r   2test_rolling_functions_window_non_shrinkage_binary   s   	r   c                 C   ru   rv   ry   rF   r   r   r   rH      rz   c                 C   ru   rv   r{   rF   r   r   r   rH      rz   c                 C   s   t  }t tdgddtg ddd}|d d|d< t t|j|jgd}t tj|j|jgddgdtdgdddd	}| |}t|| | |}t|| d S )
Nr6   r}   r~   r   r   r   rl   r   )r>   r   r   )	r   r   Zastyper   r   r>   r   r,   rP   )rt   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TestPairwisert   c                 C      |   S N)r+   rF   r   r   r   rH          zTestPairwise.<lambda>c                 C   r   r   )r4   rF   r   r   r   rH      r   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   dropnavaluesassert_numpy_array_equalselfr   r   rt   r1   rA   r   r   r   test_no_flex   s   

zTestPairwise.test_no_flexc                 C      |   jddS NTrw   	expandingr+   rF   r   r   r   rH          c                 C   r   r   r   r4   rF   r   r   r   rH      r   c                 C      | j ddjddS Nr   rD   Trw   ry   rF   r   r   r   rH          c                 C   r   r   r{   rF   r   r   r   rH      r   c                 C   r   Nr   comTrw   ewmr+   rF   r   r   r   rH      r   c                 C   r   r   r   r4   rF   r   r   r   rH      r   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>   Zlevelsr   r   uniquer   r   r   r   r   r   r   test_pairwise_with_self   s   

z$TestPairwise.test_pairwise_with_selfc                 C   r   NFrw   r   rF   r   r   r   rH   
  r   c                 C   r   r   r   rF   r   r   r   rH     r   c                 C   r   Nr   rD   Frw   ry   rF   r   r   r   rH     r   c                 C   r   r   r{   rF   r   r   r   rH     r   c                 C   r   Nr   r   Frw   r   rF   r   r   r   rH     r   c                 C   r   r   r   rF   r   r   r   rH     r   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      |   j|ddS r   r   rG   yr   r   r   rH   "      c                 C   r   r   r   r   r   r   r   rH   #  r   c                 C      | j ddj|ddS r   ry   r   r   r   r   rH   $  rI   c                 C   r   r   r{   r   r   r   r   rH   %  rI   c                 C   r   r   r   r   r   r   r   rH   &  rI   c                 C   r   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   rt   r1   rA   r   r   r   test_pairwise_with_other  s   



z%TestPairwise.test_pairwise_with_otherc                 C   r   r   r   r   r   r   r   rH   A  r   c                 C   r   r   r   r   r   r   r   rH   B  r   c                 C   r   r   ry   r   r   r   r   rH   C  rI   c                 C   r   r   r{   r   r   r   r   rH   D  rI   c                 C   r   r   r   r   r   r   r   rH   E  rI   c                 C   r   r   r   r   r   r   r   rH   F  rI   c                 C   s  |j jr	|||nd }|d urItjdd tdt |j|j}|j |j }W d    n1 s4w   Y  t	|j| t	|j | d S t
jtdd ||| W d    n1 s`w   Y  t
jtdd ||| W d    d S 1 s}w   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   rt   r1   Zexpected_indexZexpected_columnsr   r   r   test_no_pairwise_with_other>  s(   "z(TestPairwise.test_no_pairwise_with_otherc                 C      |   |S r   r   r   r   r   r   rH   c      c                 C   r   r   r   r   r   r   r   rH   d  r   c                 C      | j dd|S Nr   rD   ry   r   r   r   r   rH   e  r   c                 C   r   r   r{   r   r   r   r   rH   f  r   c                 C   r   Nr   r   r   r   r   r   r   rH   g  r   c                 C   r   r   r   r   r   r   r   rH   h  r   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   r   r   r   r   r   r   test_pairwise_with_series`  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periodsrl   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   rJ   g?)alpha)r   r   g      @@gK\ǸO@)r   r   listr   r   r&   rY   Zreshaper   r+   ZvstackfullNaNr,   rP   )r   r   r>   rg   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j	dtj
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   r   )Nr6   r7   rJ   )r   Zfrom_arraysr   r&   Zonesr*   r4   r5   repeatrY   Zint64r,   rP   )r   r   rg   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      sz    





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\   rh   rq   rs   r   r   r   r   r   r   r   <module>   sb    &&&&&&&&&







