o
    ҷh>                     @   s  d dl Zd dlZd dlm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d0d1d2 d3d2 gd4d5 Zejd0d6d2 d7d2 gd8d9 Z G d:d; d;Z!dS )<    N)IS64)	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   T/var/www/html/venv/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   sd   | }|t jdt| }|jddd|}t|j	d t |dd  |dd  d  d S )Nr	   2      windowmin_periodsr   r   )
nprandomdefault_rngstandard_normallenrollingcovtmassert_almost_equaliloc)seriesABresultr   r   r   test_rolling_cov2   s   2r6   c              	   C   s   | }|t jdt| }|jddd|}t|j	d t 
|dd  |dd  d  t }t }t j|d d< t j|d d	< |jt|d
d|}t|j	d || d S )Nr	   r    r!   r"   r%   r&   r'   r   
   r   )r(   r)   r*   r+   r,   r-   corrr/   r0   r1   corrcoefmakeTimeSeriesnan)r2   r3   r4   r5   abr   r   r   test_rolling_corr:   s   .r>   funcr.   r8   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 )Nr7   r   r"   r   Fcheck_names)getattrr-   locsliceindex	droplevelr/   assert_series_equal)r?   framer5   expectedr   r   r   test_rolling_pairwise_cov_corrK   s
    rJ   methodc                    s    d t jdd }t  jdd} fdd}t|| t||   ttj	d
jjjdt  jdd}t fdd	 D }t|| d S )
Nr   r7   r#   c                    s   t jdd | S )Nr7   rL   rB   r-   x)rK   r2   r   r   <lambda>Z       z(test_flex_binary_frame.<locals>.<lambda>r	   rE   r   c                    s,   i | ]}|t  | jd d| qS )r7   rL   rM   ).0k)rH   frame2rK   r   r   
<dictcomp>h   s   , z*test_flex_binary_frame.<locals>.<dictcomp>)rB   r-   applyr/   assert_frame_equalcopyr   r(   r)   r*   r+   shaperE   r   )rK   rH   resres2expres3r   )rH   rU   rK   r2   r   test_flex_binary_frameT   s"   r_   r#      c                 C   s@   t td}t td}|j| dj|d  sJ d S )N   rL   )other)r   r(   zerosaranger-   r8   isnaall)r#   srb   r   r   r   $test_rolling_corr_with_zero_variancem   s   $rh   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 sVJ 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(   abs
nan_to_numrS   rO   r   r   r   	<genexpr>      " z#test_corr_sanity.<locals>.<genexpr>r	   )   r	   c                 s   rj   rk   rl   ro   r   r   r   rp      rq   )r   r(   arrayr-   r8   rf   r)   r*   )dfr[   r   r   r   test_corr_sanityv   s    ru   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	   rE   r   r   r   r	   r"   )NNr   r   Nr   )r   r-   r.   r/   rG   s1s2r5   rI   s2ar   r   r   test_rolling_cov_diff_length      r   c                  C   rv   )Nrw   rx   ry   r   r   r   r	   r"   )NNr   rz   )r   r-   r8   r/   rG   r{   r   r   r   test_rolling_corr_diff_length   r   r   fc                 C      | j dddj| ddS Nr7   r   r"   Tpairwiser-   r.   rN   r   r   r   rP          rP   c                 C   r   r   r-   r8   rN   r   r   r   rP      r   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   r3   r4   foonamer
   barr   rE   namesfloat64)r   rE   dtype)	r   r   ranger   from_productrE   r   r/   rX   )r   rt   df_expected	df_resultr   r   r   2test_rolling_functions_window_non_shrinkage_binary   s   	r   c                 C   r   r   r   rN   r   r   r   rP      r   c                 C   r   r   r   rN   r   r   r   rP      r   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 )
Nr<   r   r   r   r   r   ry   r   )rE   r   r   )	r   r   astyper   r   rE   r   r/   rX   )r   df1df2df1_expecteddf2_expected
df1_result
df2_resultr   r   r   *test_moment_functions_zero_length_pairwise   s   r   c                   @   s  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 ej	dd ejj
e dddddd d d gd!d" Zejd#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 d0d d1d gd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:S );TestPairwiser   c                 C      |   S N)r.   rN   r   r   r   rP          zTestPairwise.<lambda>c                 C   r   r   )r8   rN   r   r   r   rP      r   c                 C   sX   ||}t |j|j t |j|j ||}| j}| j}t j||dd d S NFcheck_dtyper/   assert_index_equalrE   r   dropnavaluesassert_numpy_array_equalselfr   r   r   r5   rI   r   r   r   test_no_flex   s   

zTestPairwise.test_no_flexc                 C      |   jddS NTr   	expandingr.   rN   r   r   r   rP          c                 C   r   r   r   r8   rN   r   r   r   rP      r   c                 C      | j ddjddS Nr   rL   Tr   r   rN   r   r   r   rP          c                 C   r   r   r   rN   r   r   r   rP      r   c                 C   r   Nr   comTr   ewmr.   rN   r   r   r   rP      r   c                 C   r   r   r   r8   rN   r   r   r   rP      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   rE   levelsr   r   uniquer   r   r   r   r   r   r   test_pairwise_with_self   s   

z$TestPairwise.test_pairwise_with_selfc                 C   r   NFr   r   rN   r   r   r   rP     r   c                 C   r   r   r   rN   r   r   r   rP     r   c                 C   r   Nr   rL   Fr   r   rN   r   r   r   rP     r   c                 C   r   r   r   rN   r   r   r   rP     r   c                 C   r   Nr   r   Fr   r   rN   r   r   r   rP     r   c                 C   r   r   r   rN   r   r   r   rP     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   rO   yr   r   r   rP   $      c                 C   r   r   r   r   r   r   r   rP   %  r   c                 C      | j ddj|ddS r   r   r   r   r   r   rP   &  rQ   c                 C   r   r   r   r   r   r   r   rP   )  rQ   zPrecision issues on 32 bitF)reasonstrict)marksc                 C   r   r   r   r   r   r   r   rP   .  rQ   c                 C   r   r   r   r   r   r   r   rP   /  rQ   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   r   r5   rI   r   r   r   test_pairwise_with_other!  s   



z%TestPairwise.test_pairwise_with_otherzignore:RuntimeWarningc                 C   r   r   r   r   r   r   r   rP   J  r   c                 C   r   r   r   r   r   r   r   rP   K  r   c                 C   r   r   r   r   r   r   r   rP   L  rQ   c                 C   r   r   r   r   r   r   r   rP   M  rQ   c                 C   r   r   r   r   r   r   r   rP   N  rQ   c                 C   r   r   r   r   r   r   r   rP   O  rQ   c                 C   s   |j jr	|||nd }|d ur-|j|j}|j |j }t|j| t|j | d S tjtdd ||| W d    n1 sDw   Y  tjtdd ||| W d    d S 1 saw   Y  d S )Nz'arg1' columns are not unique)matchz'arg2' columns are not unique)	r   	is_uniquerE   unionr/   r   pytestraises
ValueError)r   r   r   r   r5   expected_indexexpected_columnsr   r   r   test_no_pairwise_with_otherF  s"   "z(TestPairwise.test_no_pairwise_with_otherc                 C      |   |S r   r   r   r   r   r   rP   j      c                 C   r   r   r   r   r   r   r   rP   k  r   c                 C      | j dd|S Nr   rL   r   r   r   r   r   rP   l  r   c                 C   r   r   r   r   r   r   r   rP   m  r   c                 C   r   Nr   r   r   r   r   r   r   rP   n  r   c                 C   r   r   r   r   r   r   r   rP   o  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   rE   r   r   r   r   r   r   r   r   test_pairwise_with_seriesg  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   2020)periodsry   12H)	r   r   r   r-   r8   r(   r;   r/   rG   )r   rg   r5   rI   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abxyABr      r   rR   g?)alpha)r   r   g      @@gK\ǸO@)r   r   listr   r   r(   rd   reshaper   r.   vstackfullr;   r/   rX   )r   r   rE   rt   r5   rI   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Qr<   r=   r   )r   r	   r   r   r   )r   r	   )Nr<   r=   rR   )r   from_arraysr   r(   onesr-   r8   r;   repeatrd   int64r/   rX   )r   r   rt   r5   rI   r   r   r   $test_multindex_columns_pairwise_func  s   

z1TestPairwise.test_multindex_columns_pairwise_funcN)__name__
__module____qualname__r   markparametrizer   r   r   r   xfailr   r   filterwarningsr   r   r   r   r   r   r   r   r   r      s    





r   )"numpyr(   r   pandas.compatr   pandasr   r   r   r   r   pandas._testing_testingr/   pandas.core.algorithmsr   fixturer   r   r   r6   r>   r   r   rJ   r_   r   rh   ru   r   r   r   r   r   r   r   r   r   <module>   sb    &&&&&&&&&







