U
    f/e                  
   @   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	 d dl
mZ d dlmZ dd Zdd Zejd	d
ddgdd Zdd Zdd Zdd Zdd Zejdeddde	dddde	ddddddgejd d d!gd"d# Zd$d% Zd&d' Zd(d) Zejd*d+d,d-d.gd/d0 Zd1d2 Z d3d4 Z!dS )5    N)UnsupportedFunctionCall)	DataFrameDatetimeIndexSeries
date_range)ExponentialMovingWindowc                  C   s0   t ddddtjdgi} |  | jdd  d S )NBr                  ?com)r   npnanewmmean)df r   @/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/window/test_ewm.pytest_doc_string   s    r   c              
   C   s  | t dj}|dd |dd |dd |dd |dd d	 |dd d
 |dd d d}tjt|d |ddd W 5 Q R X tjt|d |ddd W 5 Q R X tjt|d |ddd W 5 Q R X d}tjt|d |dd W 5 Q R X d}tjt|d |dd W 5 Q R X d}tjt|d |dd W 5 Q R X d}dD ]*}tjt|d ||d W 5 Q R X qPd S )N   r   r         ?)spanalphag      ?halflife)r   r   )r   r   )r   r   z8comass, span, halflife, and alpha are mutually exclusivematch)r   r   )r   r   )r   r   z comass must satisfy: comass >= 0      zspan must satisfy: span >= 1z#halflife must satisfy: halflife > 0r   z"alpha must satisfy: 0 < alpha <= 1)r    r   )ranger   pytestraises
ValueError)Zframe_or_seriescmsgr   r   r   r   test_constructor   s8    



r'   methodstdr   varc              	   C   sv   t tdddgdd}d}tjt|d t|| ddd	 W 5 Q R X tjt|d t|| tjd
 W 5 Q R X d S )Nr
   r      r   r   z2numpy operations are not valid with window objectsr   r	      )Zdtype)r   r   r"   r#   r   getattrr   Zfloat64)r(   er&   r   r   r   test_numpy_compatC   s    r/   c               	   C   s<   d} t jt| d  ttdjtdd W 5 Q R X d S )Nz%times must be datetime64\[ns\] dtype.r   r   times)r"   r#   r$   r   r!   r   r   aranger&   r   r   r   !test_ewma_times_not_datetime_typeP   s    r4   c               	   C   sB   d} t jt| d& ttdjtddd W 5 Q R X d S )Nz,times must be the same length as the object.r   r   r   datetime64[ns]r0   	r"   r#   r$   r   r!   r   r   r2   astyper3   r   r   r   test_ewma_times_not_same_lengthV   s    r8   c               	   C   sD   d} t jt| d( ttdjdtddd W 5 Q R X d S )Nz6halflife must be a string or datetime.timedelta objectr   r   r	   r5   r   r1   r6   r3   r   r   r   #test_ewma_halflife_not_correct_type\   s    r:   c              	   C   s6   d}t jt|d ttdj| d W 5 Q R X d S )NzKhalflife can only be a timedelta convertible argument if times is not None.r   r   r   )r"   r#   r$   r   r!   r   )halflife_with_timesr&   r   r   r    test_ewma_halflife_without_timesb   s    r<   r1   
   zdatetime64[D]r5   2000DfreqZperiodsUTCtime_colmin_periodsr
   c                 C   sl   | }t d}t j|d d d< t|tddddd}|j|||d }|jd	|d
 }t|| d S )N      $@r
   r>   r?   r=   r@   )ArC   )r   rD   r1         ?)r   rD   )	r   r2   r   r   r   r   r   tmassert_frame_equal)r;   r1   rD   r   datar   resultexpectedr   r   r   "test_ewma_with_times_equal_spacingh   s    
rM   c                 C   s^   | }d}t dddg|}td}t|}|j||d }tddd	g}t|| d S )
Nz23 daysz
2020-01-01z2020-01-10T00:04:05z2020-02-23T05:00:23r,   r9   g        gE(?g*M?)	r   tz_localizer   r2   r   r   r   rH   rI   )Ztz_aware_fixturetzr   r1   rJ   r   rK   rL   r   r   r   %test_ewma_with_times_variable_spacing|   s    
rP   c              	   C   sD   t td}tdg}tjtdd |jd| |d W 5 Q R X d S )Nr	   ZNaTz$Cannot convert NaT values to integerr   皙?)r   r   r1   )r   r!   r   r"   r#   r$   r   )r;   serr1   r   r   r   test_ewm_with_nat_raises   s    
rS   c                 C   st   | }t d}t j|d d d< tdddd}t||d}|j||dd	  }|jd
dd	  }t|| d S )NrE   r
   r>   r?   r=   r@   rF   r   r9   rF   rG   r   )	r   r2   r   r   r   r   r   rH   assert_series_equal)r;   r   rJ   r1   r   rK   rL   r   r   r   test_ewm_with_times_getitem   s    
rV   argr   r   r   r   c                    sn   | dd|d|i}t tdtddjf |  fdd jD } d } fdd|jD }||ksjtd S )	Nr	   adjust	ignore_narT   c                    s   i | ]}|t  |qS r   r-   .0attrr   r   r   
<dictcomp>   s      z8test_ewm_getitem_attributes_retained.<locals>.<dictcomp>rF   c                    s   i | ]}|t  |qS r   rZ   r[   r^   r   r   r_      s      )r   r!   r   _attributesAssertionError)rW   rX   rY   kwargsrL   Z	ewm_slicerK   r   r^   r   $test_ewm_getitem_attributes_retained   s    rc   c               	   C   sR   t td} tt | jdd }W 5 Q R X | jdd }t|| d S )Nr	   rQ   r   )	r   r!   rH   Zassert_produces_warningFutureWarningr   Zvolr)   rU   )rR   rK   rL   r   r   r   test_ewm_vol_deprecated   s
    re   c                	   C   s@   t jtdd( ttdjddtddddd	 W 5 Q R X d S )
Nz)times is not supported with adjust=False.r   r	   rQ   Fr>   r?   r@   )rX   r1   )r"   r#   NotImplementedErrorr   r!   r   r   r   r   r   r   #test_ewma_times_adjust_false_raises   s       rg   )"Znumpyr   r"   Zpandas.errorsr   Zpandasr   r   r   r   Zpandas._testingZ_testingrH   Zpandas.core.windowr   r   r'   markZparametrizer/   r4   r8   r:   r<   r2   r7   rN   rM   rP   rS   rV   rc   re   rg   r   r   r   r   <module>   s<   ,
	


