U
    sVc                     @   sr   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
mZ d dlmZ dd Zdd Zdd	 ZG d
d dZdS )    N)NpyDatetimeUnit)OutOfBoundsDatetimeOutOfBoundsTimedeltaastype_overflowsafeis_unitlesspy_get_unit_from_dtypepy_td64_to_tdstructc               	   C   s   t d} t| rtt d} t| s,tt d} t| rBtt d} t| sXtd}tjt|d tt t j W 5 Q R X d}tjt|d td W 5 Q R X d S )	NM8[ns]
datetime64m8[ns]Ztimedelta64z'dtype must be datetime64 or timedelta64matchz#Argument 'dtype' has incorrect typeZfoo)	npdtyper   AssertionErrorpytestraises
ValueErrorZint64	TypeError)r   msg r   H/tmp/pip-unpacked-wheel-xj8nt62q/pandas/tests/tslibs/test_np_datetime.pytest_is_unitless   s    



r   c                   C   s  t tdtjjkstt tdtjjks4tt tdtjjksNtt tdtj	jkshtt tdtj
jkstt tdtjjkstt tdtjjkstt tdtjjkstt td	tjjkstt td
tjjkstt tdtjjks"tt tdtjjks>tt tdtjjksZtt tdtjjksvtt tdtjjkstt tdtjjkstt tdtj	jkstt tdtj
jkstt tdtjjkstt tdtjjkstt tdtjjks:tt tdtjjksVtt tdtjjksrtt tdtjjkstt tdtjjkstt tdtjjkstd S )NzM8[Y]zM8[M]zM8[W]M8[D]zM8[h]zM8[m]zM8[s]zM8[ms]M8[us]r	   zM8[ps]zM8[fs]zM8[as]zm8[Y]zm8[M]zm8[W]m8[D]zm8[h]zm8[m]zm8[s]zm8[ms]m8[us]r   zm8[ps]zm8[fs]zm8[as])r   r   r   r   ZNPY_FR_Yvaluer   ZNPY_FR_MZNPY_FR_WZNPY_FR_DZNPY_FR_hZNPY_FR_mNPY_FR_s	NPY_FR_ms	NPY_FR_us	NPY_FR_nsZ	NPY_FR_psZ	NPY_FR_fsZ	NPY_FR_asr   r   r   r   test_get_unit_from_dtype'   s4    r"   c            	      C   s   d} t | tjj}ddddddddddd
}||ks8tt | tjj}dd	d
dddddddd
}||ksltt | tjj}dd	ddddddddd
}||kstt | tjj}ddddddddddd
}||kstd S )Nl   *L r      i  i|     i )
daysZhrsminsecmsusnssecondsmicrosecondsZnanoseconds      "   i0  iJ	    %      i2  i i3    
   i) )r   r   r!   r   r   r    r   r   )	valZres1Zexp1Zres2Zexp2Zres3Zexp3Zres4Zexp4r   r   r   test_td64_to_tdstructI   sj    r6   c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )TestAstypeOverflowSafec              	   C   sl   t d}t d}d}tjt|d t||dd W 5 Q R X tjt|d t||dd W 5 Q R X d S )N   r	   ]astype_overflowsafe values.dtype and dtype must be either both-datetime64 or both-timedelta64r   TcopyF)r   aranger   r   r   r   r   selfarrr   r   r   r   r   test_pass_non_dt64_array   s    

z/TestAstypeOverflowSafe.test_pass_non_dt64_arrayc              	   C   sv   t jdddd}t d}d}tjt|d t||dd	 W 5 Q R X tjt|d t||d
d	 W 5 Q R X d S )Nr8   i8r   r   r   r9   r   Tr:   F)r   r<   viewr   r   r   r   r   r=   r   r   r   test_pass_non_dt64_dtype   s    
z/TestAstypeOverflowSafe.test_pass_non_dt64_dtypec           
   	   C   s   t d}t dd}|t jddd }||}||j}||k rNtd}tjt	|d t
|| W 5 Q R X t d	}t
||}||}	t||	 d S )
Nr	   
2262-04-05Dr4   r   rB   z"Out of bounds nanosecond timestampr   r   )r   r   r
   r<   astypeallr   r   r   r   r   tmassert_numpy_array_equal
r>   r   dtr?   ZwrongZ	roundtripr   Zdtype2resultexpectedr   r   r   test_astype_overflowsafe_dt64   s    




z4TestAstypeOverflowSafe.test_astype_overflowsafe_dt64c           
   	   C   s   t d}t dd}|t jddd }|d}||}||j}||k rXtd}tj	t
|d t|| W 5 Q R X t d	}t||}||}	t||	 d S )
Nr   rE   rF   r4   r   rB   z@Cannot convert 106752 days to timedelta64\[ns\] without overflowr   r   )r   r   r
   r<   rC   rG   rH   r   r   r   r   r   rI   rJ   rK   r   r   r   test_astype_overflowsafe_td64   s    





z4TestAstypeOverflowSafe.test_astype_overflowsafe_td64c              	   C   sp   t jddgdd}t d}d}tjt|d t||dd	 W 5 Q R X t||d
d	}||}t	|| d S )Ni$i  r	   rB   r   z'Cannot losslessly cast '-1500 ns' to usr   F)Zround_okT)
r   arrayr   r   r   r   r   rG   rI   rJ   )r>   r?   r   r   rM   rN   r   r   r   *test_astype_overflowsafe_disallow_rounding   s    

zATestAstypeOverflowSafe.test_astype_overflowsafe_disallow_roundingN)__name__
__module____qualname__r@   rD   rO   rP   rR   r   r   r   r   r7      s
   r7   )Znumpyr   r   Zpandas._libs.tslibs.dtypesr   Zpandas._libs.tslibs.np_datetimer   r   r   r   r   r   Zpandas._testingZ_testingrI   r   r"   r6   r7   r   r   r   r   <module>   s    	"A