o
    d7?                     @   s~  d dl Zd dlZd dlmZmZmZ d dlmZ d dl	m
  mZ d dlmZmZmZmZ d dlmZ ejje o>e p>e ddZejddgd	d
d Zejdi gdi gdi gdi gdi gdi gddd igdi gddd igg	d	dd ZedejdG dd dZedG dd dZeddd Zeddd Z edejj!ejdG d d! d!Z"dS )"    N)is_ci_environmentis_platform_macis_platform_windows)NumbaUtilError)	DataFrameSeriesoption_contextto_datetimez`On GHA CI, Windows can fail with 'Windows fatal exception: stack overflow' and macOS can timeout)reasonsingletable)paramsc                 C      | j S )z3method keyword in rolling/expanding/ewm constructorparamrequest r   R/app/.heroku/python/lib/python3.10/site-packages/pandas/tests/window/test_numba.pymethod   s   r   summeanZmedianmaxminvarZddofstdc                 C   r   Nr   r   r   r   r   $arithmetic_numba_supported_operators#   s   r   numbaignorec                   @   sV  e Zd Zejdddgdd Zejdee	deddddej
ejd	d
d
d
g
ddddej
ejdd
d
d
g
ej
ej
ddddddddg
gjeedddedddejdddd
gedddej
dddd
ggdd Zejdee	deedddgdd Zejdddgdd Zejddd
ddgdi ggdd Zdd  Zd!S )"
TestEnginejitTFc                 C   s   dd }|rdd l }||}|||d}	d}
ttd}|jd||dj||
d	|	d
d}|jd||dj|d|
d
d}t|| d S )Nc                 W   s$   d}|D ]}||7 }qt | | S )Nr   npr   )xargsZarg_sumargr   r   r   f:   s   
z0TestEngine.test_numba_vs_cython_apply.<locals>.fr   nogilparallelnopython   
   r-   )centerstepr   T)r%   engineengine_kwargsrawcython)r1   r%   r3   r   r!   r   rangerollingapplytmassert_series_equal)selfr!   r)   r*   r+   r/   r0   r'   r   r2   r%   sresultexpectedr   r   r   test_numba_vs_cython_apply8   s   

z%TestEngine.test_numba_vs_cython_applydata               foo)name   r.      r-   c                 C   sd   |\}}|||d}	|j d|d}
t|
|dd|	d|}t|
|dddi|}t|| d S )	Nr(   rD   r0   r   r1   r2   r1   r4   r   )r7   getattrr9   assert_equal)r;   r@   r)   r*   r+   r   r0   r   kwargsr2   rollr=   r>   r   r   r   $test_numba_vs_cython_rolling_methodsQ   s   
z/TestEngine.test_numba_vs_cython_rolling_methodsc                 C   sl   |\}}|||d}t td}| }	t|	|dd|d|}
t|	|dddi|}t|
| d S )Nr(   rA   r   rK   r1   r4   r   )r   r#   eye	expandingrL   r9   rM   )r;   r@   r)   r*   r+   r   r   rN   r2   expandr=   r>   r   r   r   &test_numba_vs_cython_expanding_methodsu   s   
z1TestEngine.test_numba_vs_cython_expanding_methodsc                 C   s   dd }dd }|rdd l }||}||}|||d}	ttdjd|d	}
|
j|d
|	dd}|
j|ddd}t|| |
j|d
|	dd}|
j|ddd}t|| |
j|d
|	dd}|
j|ddd}t|| d S )Nc                 S      t | d S )NrC   r"   r$   r   r   r   func_1      z+TestEngine.test_cache_apply.<locals>.func_1c                 S   s   t | d S )NrA   )r#   r   rV   r   r   r   func_2   rX   z+TestEngine.test_cache_apply.<locals>.func_2r   r(   r.   r-   rJ   r   T)r1   r2   r3   r4   r1   r3   r5   )r;   r!   r)   r*   r+   r0   rW   rY   r   r2   rO   r=   r>   r   r   r   test_cache_apply   s.   

zTestEngine.test_cache_applyzwindow,window_kwargsr7   r   )windowmin_periodsrR   c                 C   s   dd }|||d}t dg di}	t|	|dd|i|j|dd|d	d
}
t dg di}t|
| t|	|dd|i|j|dd|dd
}
t dg di}t|
| d S )Nc                 S   s   t | | S r   r#   r   )valuesr$   r   r   r   add   rX   z,TestEngine.test_dont_cache_args.<locals>.addr+   r)   r*   valuer   r   r   r   Tr   )rI   )r3   r1   r2   r%         ?re   re   r,          @rg   rg   r   )r   rL   r8   r9   assert_frame_equal)r;   r\   Zwindow_kwargsr)   r*   r+   r   r`   r2   dfr=   r>   r   r   r   test_dont_cache_args   s   

zTestEngine.test_dont_cache_argsc                    s   d dd fdd} d}t dg di}|dj|dd	|d
}t dg di}t|| d d}|dj|dd	|d
}t dg di}t|| d S )NFTc                    s      S r   r   rV   r)   r+   r*   r   r   func   s   z6TestEngine.test_dont_cache_engine_kwargs.<locals>.funcra   rb   rc   rI   r   )r3   r1   r2   rf   rd   r   r7   r8   r9   rh   )r;   rl   r2   ri   r=   r>   r   rk   r   test_dont_cache_engine_kwargs   s$   

z(TestEngine.test_dont_cache_engine_kwargsN)__name__
__module____qualname__pytestmarkparametrizer?   r   r#   rQ   naninfTr   r6   rP   rT   r[   rj   rn   r   r   r   r   r    4   sB    



#
r    c                   @   s   e Zd Zejjddd dd gddgdejdd	d
gdd Zejjddd dd gddgdejdd	d
gdd Zejdddgejdd	d
gdd Zejdddgdd Z	dS )TestEWMgrouperc                 C      | S r   r   rV   r   r   r   <lambda>       zTestEWM.<lambda>c                 C   
   |  dS NAgroupbyrV   r   r   r   r{         
 Noner   )Zidsr   r   r   c                 C   sf   t g dtdd}tjtdd t||jdd|dd	 W d    d S 1 s,w   Y  d S )
Nabr   r   rC   r   Bzengine must be eithermatchre   comrF   r1   r   r6   rr   raises
ValueErrorrL   ewmr;   ry   r   ri   r   r   r   test_invalid_engine   s   "zTestEWM.test_invalid_enginec                 C   rz   r   r   rV   r   r   r   r{      r|   c                 C   r}   r~   r   rV   r   r   r   r{      r   c                 C   sl   t g dtdd}tjtdd t||jdd|dd	d
id W d    d S 1 s/w   Y  d S )Nr   rC   r   zcython engine does notr   re   r   r4   r+   TrK   r   r   r   r   r   test_invalid_engine_kwargs   s   "z"TestEWM.test_invalid_engine_kwargsc                 C   s   t dtdi}|dkrdd }n
g d|d< dd }|d	kr!d
}||jd||d}	|||d}
t|	|d|
d}t|	|dd}t|| d S )Nr   rC   r   c                 S   rz   r   r   rV   r   r   r   r{      r|   z.TestEWM.test_cython_vs_numba.<locals>.<lambda>r   r   c                 S   r}   r~   r   rV   r   r   r   r{     r   r   Tre   )r   adjust	ignore_nar(   r   rK   r4   r   )r   r6   r   rL   r9   rh   )r;   ry   r   r)   r*   r+   r   r   ri   r   r2   r=   r>   r   r   r   test_cython_vs_numba   s   
zTestEWM.test_cython_vs_numbac                 C   s   t dg di}|dkrdd }n
dd }g d|d< d	}tg d
}||j|d||d}	|||d}
|	jd|
d}|	jdd}t|| d S )Nr   )r   r   rI   rI   r-   r-   r   c                 S   rz   r   r   rV   r   r   r   r{     r|   z4TestEWM.test_cython_vs_numba_times.<locals>.<lambda>c                 S   r}   r~   r   rV   r   r   r   r{     r   )r   r   r   r   r   r   r   z23 days)
2020-01-01r   z
2020-01-02z
2020-01-10z
2020-02-23z
2020-01-03T)halflifer   r   timesr(   r   rK   r4   r   )r   r	   r   r   r9   rh   )r;   ry   r)   r*   r+   r   ri   r   r   r   r2   r=   r>   r   r   r   test_cython_vs_numba_times  s    

z"TestEWM.test_cython_vs_numba_timesN)
ro   rp   rq   rr   rs   rt   r   r   r   r   r   r   r   r   rx      s     rx   c                  C   sz   dd } t td}tdd |dj| d dd}W d    n1 s%w   Y  |dj| ddd}t|| d S )	Nc                 S   rU   )Nr-   r"   rV   r   r   r   r'   2  rX   z!test_use_global_config.<locals>.fr.   zcompute.use_numbaTr-   rZ   r   )r   r6   r   r7   r8   r9   r:   )r'   r<   r=   r>   r   r   r   test_use_global_config0  s   r   c                   C   s\   t jtdd ttddjdd ddiddd	 W d    d S 1 s'w   Y  d S )
Nz"numba does not support kwargs withr   rI   c                 S   rz   r   r   rV   r   r   r   r{   @  r|   z.test_invalid_kwargs_nopython.<locals>.<lambda>r   r   T)rN   r1   r3   )rr   r   r   r   r6   r7   r8   r   r   r   r   test_invalid_kwargs_nopython<  s
   "r   c                   @   s|   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Ze	j
dedededge	j
dddgdd ZdS )TestTableMethodc                 C   s^   dd }t jtdd ttdjdddj|dd	d
 W d    d S 1 s(w   Y  d S )Nc                 S      t j| ddd S Nr   axisrI   r^   rV   r   r   r   r'   J     z7TestTableMethod.test_table_series_valueerror.<locals>.fz1method='table' not applicable for Series objects.r   rI   r   )r   r   TrZ   )rr   r   r   r   r6   r7   r8   )r;   r'   r   r   r   test_table_series_valueerrorI  s   "z,TestTableMethod.test_table_series_valueerrorc                 C   s   |\}}|||d}	t td}
|
jdd|d|d}|dv rItjt| dd	 t||d|	d
d| W d    d S 1 sBw   Y  d S |
jdd|d|d}t||d|	d
d|}t||d|	d
d|}t	|| d S )Nr(   rD   r-   r   r   r   r   r]   r0   r   r    not supportedr   r   r2   r1   r   r   )
r   r#   rQ   r7   rr   r   NotImplementedErrorrL   r9   rh   )r;   r   r)   r*   r+   r   r0   r   rN   r2   ri   Z
roll_tableZroll_singler=   r>   r   r   r   !test_table_method_rolling_methodsT  s4   	
"


z1TestTableMethod.test_table_method_rolling_methodsc                 C   sv   |||d}dd }t td}|jdd|d|dj|d	|d
d}	|jdd|d|dj|d	|d
d}
t|	|
 d S )Nr(   c                 S   r   r   r^   rV   r   r   r   r'   w  r   z:TestTableMethod.test_table_method_rolling_apply.<locals>.frD   r-   r   r   r   Tr   r3   r2   r1   r   )r   r#   rQ   r7   r8   r9   rh   )r;   r   r)   r*   r+   r0   r2   r'   ri   r=   r>   r   r   r   test_table_method_rolling_applyt  s   



z/TestTableMethod.test_table_method_rolling_applyc                 C   s   dd }t g dg dg dg dg}|jddd	|d
j|ddd}t g dg dg dg dgd d | }t|| d S )Nc                 S   sh   t d| jd f}| d d d df | d d df  jdd| d d df   |d d d df< |S )NrI   r-   r   r   )r#   onesshaper   )r$   Zarrr   r   r   weighted_mean  s   PzNTestTableMethod.test_table_method_rolling_weighted_mean.<locals>.weighted_mean)rI   r-   g333333?)r-   rD   g?)rD   rC   g?)rC   rA   gffffff?r-   r   r   )r   r]   r0   Tr   )r3   r1   )re   rg   re   )g?rg   re   )gk}
@gk}@re   )g1?rB   re   rm   )r;   r0   r   ri   r=   r>   r   r   r   'test_table_method_rolling_weighted_mean  s    z7TestTableMethod.test_table_method_rolling_weighted_meanc           
      C   sj   |||d}dd }t td}|jd|dj|d|dd	}|jd
|dj|d|dd	}	t||	 d S )Nr(   c                 S   r   r   r^   rV   r   r   r   r'     r   z<TestTableMethod.test_table_method_expanding_apply.<locals>.frD   r   r   r   Tr   r   r   )r   r#   rQ   rR   r8   r9   rh   )
r;   r   r)   r*   r+   r2   r'   ri   r=   r>   r   r   r   !test_table_method_expanding_apply  s   z1TestTableMethod.test_table_method_expanding_applyc                 C   s   |\}}|||d}t td}	|	jd|d}
|dv rFtjt| dd t|
|d|dd	| W d    d S 1 s?w   Y  d S |	jd
|d}t|
|d|dd	|}t||d|dd	|}t	|| d S )Nr(   rD   r   r   r   r   r   r   r   r   r   )
r   r#   rQ   rR   rr   r   r   rL   r9   rh   )r;   r   r)   r*   r+   r   r   rN   r2   ri   Zexpand_tableZexpand_singler=   r>   r   r   r   #test_table_method_expanding_methods  s0   
"

z3TestTableMethod.test_table_method_expanding_methodsr@   rD   )r-   rD   )rD   r-   r   r   r   c                 C   s`   |||d}t |}t|jdd|d||dd}	t|jdd|d||dd}
t|	|
 d S )Nr(   rI   r   )r   r   r   r   r   r   )r   rL   r   r9   rh   )r;   r@   r   r   r)   r*   r+   r2   ri   r=   r>   r   r   r   test_table_method_ewm  s   z%TestTableMethod.test_table_method_ewmN)ro   rp   rq   r   r   r   r   r   r   rr   rs   rt   r#   rQ   r   r   r   r   r   r   r   D  s     $r   )#numpyr#   rr   Zpandas.compatr   r   r   Zpandas.errorsr   Zpandas.util._test_decoratorsutilZ_test_decoratorstdZpandasr   r   r   r	   Zpandas._testingZ_testingr9   rs   ZskipifZ
pytestmarkZfixturer   r   Z
skip_if_nofilterwarningsr    rx   r   r   Zslowr   r   r   r   r   <module>   sP    




 -L


