U
    f/eX                     @   s   d dl Z d dlmZmZ d dlZd dlmZ d dlZd dlZd dl	Z	d dl
m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 G dd	 d	Zd
d Zejdddgdd Zdd Zejdi ddidddgdd ZdS )    N)datetime	timedelta)tzutc)PY310)OutOfBoundsDatetime)Period	Timedelta	Timestampcompat)offsetsc                   @   sD  e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
ejdejddd Zejdeedddddddddedddddddddedddddddddd 	eddddddddd!	eddddddddej	gd"d# Zejd$d%d&gd'd( Zejd)d*d+d,d-d.d/d0d1gd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zejd)d>d?gd@dA ZdBdC ZdDdE ZdFdG ZdHdI Z ejdJd!e!dKgdLdM Z"ejdNdOdPgdQdR Z#ejd)dSdTgdUdV Z$dWdX Z%dYdZ Z&ejd[eegd\d] Z'd^d_ Z(d`da Z)ejj*e+j, dbdcddde Z-d!S )fTestTimestampConstructorsc                 C   s  d}t dddd}d}t| d |ks0t|||fdt dddd	|d
 fdt ddddddd|d fdtd|d fg}ddtjdfdddtddft	j
d ddfg}|D ]\}}}t|t|fD ](}	|	j|kstt|	}	|	j|kstq|D ]\}
}t||
dt||
dfD ]}	||d d  }|	j|ks@tt|	}	|	j|ksXt|
d k	rrt|	d}	nt|	dd}	||d d  }|	j|kstqqqd S )Nz2014-07-01 09:00        	        t  ʚ;z2014-07-01 10:00
        qaz2014-07-01 09:00:00.000008000r      @  z2014-07-01 09:00:00.000000005   Nr   UTCr   
Asia/Tokyor   
US/Easternzdateutil/US/PacificiLPF  tzi  r   )r   calendartimegm	timetupleAssertionErrorr	   pytzutcFixedOffsetdateutilr%   tzoffsetvalue
tz_convert)selfbase_strbase_dtbase_expectedtests	timezonesdate_strdateexpectedresultr%   offsetexpected_tzexpected_utc r>   S/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/scalar/timestamp/test_constructors.pytest_constructor   sV    
z*TestTimestampConstructors.test_constructorc                 C   sl  d}t dddd}d}t| d |ks0t||fd|d	 fd
|d fd|d fg}ddtjdfdddtddftj	
d ddfg}|D ]\}}t|fD ](}|j|kstt|}|j|kstq|D ]^\}	}
t||	d}|}|j|kstt|}|j|kstt|d}|}|j|kstqqtddd}|jtdjksRtd}t||ksht|tt|ks~ttddd}|jtdjkstd}t||kst|tt|ksttdd d}|jtd!jkstd"}t||kst|tt|ksttd#d$d}|jtd!jks<td%}t||ksRt|tt|kshtd S )&Nz2014-07-01 11:00:00+02:00r   r   r   r   r   r   z2014-07-01 12:00:00+02:00r   z#2014-07-01 11:00:00.000008000+02:00r   z#2014-07-01 11:00:00.000000005+02:00r   r   r   r   r   r   r    r!   r"   r#   r$   r   z2013-11-01 00:00:00-0500zAmerica/Chicagoz2013-11-01 05:00z;Timestamp('2013-11-01 00:00:00-0500', tz='America/Chicago')r   z6Timestamp('2013-11-01 14:00:00+0900', tz='Asia/Tokyo')z2015-11-18 15:45:00+05:45zAsia/Katmanduz2015-11-18 10:00z9Timestamp('2015-11-18 15:45:00+0545', tz='Asia/Katmandu')z2015-11-18 15:30:00+05:30zAsia/Kolkataz8Timestamp('2015-11-18 15:30:00+0530', tz='Asia/Kolkata'))r   r&   r'   r(   r)   r*   r+   r,   r-   r%   r.   r	   r/   r0   repreval)r1   r2   r3   r4   r5   r6   r7   r9   r:   r%   r;   r<   r=   r>   r>   r?   "test_constructor_with_stringoffset]   sh    


z<TestTimestampConstructors.test_constructor_with_stringoffsetc              	   C   sX   d}t jt|d ttd W 5 Q R X d}t jt|d ttd W 5 Q R X d S )NzCannot convert inputmatch   zCannot convert Periodz
1000-01-01)pytestraises	TypeErrorr	   slice
ValueErrorr   r1   msgr>   r>   r?   test_constructor_invalid   s    z2TestTimestampConstructors.test_constructor_invalidc              
   C   s   d}t jt|d tddd W 5 Q R X d}t jt|d tdtjdd W 5 Q R X d	}d
}t jt|d* tjt	|d tdd W 5 Q R X W 5 Q R X d S )NzJArgument 'tzinfo' has incorrect type \(expected datetime.tzinfo, got str\)rD   z
2017-10-22r   tzinfozat most one ofr   )rP   r%   Invalid frequency:The 'freq' argument
2012-01-01
US/Pacific)
rG   rH   rI   r	   rK   r*   r+   tmassert_produces_warningFutureWarningr1   rM   msg2r>   r>   r?   test_constructor_invalid_tz   s    z5TestTimestampConstructors.test_constructor_invalid_tzc              	   C   s6   d}d}d}t jt|d t|| W 5 Q R X d S )Nz%Y%m%d-%H%M%S-%f%zz20190129-235348-000001+0000z)Timestamp.strptime\(\) is not implementedrD   )rG   rH   NotImplementedErrorr	   strptime)r1   fmttsrM   r>   r>   r?   test_constructor_strptime   s
    z3TestTimestampConstructors.test_constructor_strptimec              
      s   t dddddt dddtjdt dddtjdt tdddtjdt tdddddt tdddtjdg t fd	d
 D std S )Ni  r      r   )yearmonthdayr%   ra   rb   rc   rP   rO   r$   c                 3   s   | ]}| d  kV  qdS )r   Nr>   ).0r^   Zstampsr>   r?   	<genexpr>   s     zJTestTimestampConstructors.test_constructor_tz_or_tzinfo.<locals>.<genexpr>)r	   r*   r+   r   allr)   r1   r>   rf   r?   test_constructor_tz_or_tzinfo   s    z7TestTimestampConstructors.test_constructor_tz_or_tzinfoc              	   C   s  t rdnd}tjt|d tdd W 5 Q R X d}tjt|d tddd W 5 Q R X tjt|d tddd W 5 Q R X d	}tjt|d tddd W 5 Q R X tjt|d tddd
 W 5 Q R X ttdddttdkstttdddddddttdkstd S )Nz5'NoneType' object cannot be interpreted as an integerzan integer is requiredrD     r   month must be in 1..12r      day is out of range for month            20151112rF      ?B 2015-11-12 01:02:03.999999)r   rG   rH   rI   r	   rK   rA   r)   rL   r>   r>   r?   test_constructor_positional   s(     z5TestTimestampConstructors.test_constructor_positionalc              
   C   s$  d}t jt|d tddd W 5 Q R X d}t jt|d tdddd W 5 Q R X t jt|d tdd	dd W 5 Q R X d
}t jt|d tdddd W 5 Q R X t jt|d tdddd W 5 Q R X ttddddttdkstttddddddddttdks td S )Nz@function missing required argument 'day'|Required argument 'day'rD   rk   r   ra   rb   rl   r   )ra   rb   rc   rm   rn   ro   rp   rq   rr   rs   rF   rt   ru   )ra   rb   rc   hourminutesecondmicrosecondrv   )rG   rH   rI   r	   rK   rA   r)   rL   r>   r>   r?   test_constructor_keyword   s<    

z2TestTimestampConstructors.test_constructor_keywordz&ignore:Timestamp.freq is:FutureWarningz(ignore:The 'freq' argument:FutureWarningc                 C   s   t ddd}tj| dd}||ks*t|jdks8t| | ksLttj| dd}tddd|ksrt| | kstt dd	d
dd}t| }| |ksttddd}| }tj| dd}| |kstd S )Nrk   r   Dfreqr   r$   z
2000-01-01i        r   z	2011-4-16)r   r	   fromordinal	toordinalr)   r   Zto_pydatetime)r1   baser^   dtZstampZdt_tzr>   r>   r?   test_constructor_fromordinal%  s    z6TestTimestampConstructors.test_constructor_fromordinalr:   rk   r   rF   rt   r   r      )
nanosecond)ra   rb   rc   ry   rz   r{   r|   r   r   )	ra   rb   rc   ry   rz   r{   r|   r   r%   Nc              	   C   s<   t tddddddd|jd}|tdd	 }||ks8td S )
Nrk   r   rF   rt   r   r   r   r$   )Znanoseconds)r	   r   r%   r   r)   r1   r:   r9   r>   r>   r?   test_constructor_nanosecond>  s    !z5TestTimestampConstructors.test_constructor_nanosecondzZZ0ZZ00c              	   C   s0   d}t jt|d td|  W 5 Q R X d S )Nz%could not convert string to TimestamprD   z2014-11-02 01:00rG   rH   rK   r	   )r1   r   rM   r>   r>   r?   %test_constructor_invalid_Z0_isostringc  s    z?TestTimestampConstructors.test_constructor_invalid_Z0_isostringargra   rb   rc   ry   rz   r{   r|   r   c              	   C   s4   |di}d}t jt|d td| W 5 Q R X d S )Nr   z-Cannot pass a date attribute keyword argumentrD   2010-10-10 12:59:59.999999999)r   r   )r1   r   kwargrM   r>   r>   r?   )test_invalid_date_kwarg_with_string_inputj  s    zCTestTimestampConstructors.test_invalid_date_kwarg_with_string_inputc              	   C   sx   t tjjd }tjt|d ttjjd  W 5 Q R X t tjjd }tjt|d ttjjd  W 5 Q R X d S )NrF   rD   )strr	   maxr/   rG   rH   r   minrL   r>   r>   r?    test_out_of_bounds_integer_value}  s    z:TestTimestampConstructors.test_out_of_bounds_integer_valuec              	   C   s   t dd}t tjd| }t tjd}t| t| d}tjt	|d t||  W 5 Q R X tjt	|d t||  W 5 Q R X d S )Nr   ztimedelta64[us]zM8[us]Out of boundsrD   )
npZtimedelta64Zastype
datetime64r	   r   r   rG   rH   rK   )r1   Zone_usZ	min_ts_usZ	max_ts_usrM   r>   r>   r?   test_out_of_bounds_value  s    z2TestTimestampConstructors.test_out_of_bounds_valuec              	   C   sL   d}t jt|d td W 5 Q R X t jt|d td W 5 Q R X d S )Nr   rD   z
1676-01-01z
2263-01-01r   rL   r>   r>   r?   test_out_of_bounds_string  s
    z3TestTimestampConstructors.test_out_of_bounds_stringc              	   C   s*   d}t jt|d td W 5 Q R X d S )Nz7Out of bounds nanosecond timestamp: 2262-04-11 23:47:16rD   z2262-04-11 23:47:16.854775808rG   rH   r   r	   rL   r>   r>   r?   test_barely_out_of_bounds  s    z3TestTimestampConstructors.test_barely_out_of_boundsc                 C   s~   d}d}|D ]@}|D ]6}t ||}d}tjt|d t| W 5 Q R X qqd}|D ]"}|D ]}t ||}t| q^qVd S )N)z
1677-09-21z
2262-04-12)r~   hmsmsusr   rD   )z
1677-09-23z
2262-04-11)r   r   rG   rH   r   r	   )r1   Zout_of_bounds_datesZ
time_unitsZdate_stringunitZdt64rM   Zin_bounds_datesr>   r>   r?    test_bounds_with_different_units  s    z:TestTimestampConstructors.test_bounds_with_different_unitsz	001-01-01z
0001-01-01c              	   C   s*   d}t jt|d t| W 5 Q R X d S )Nr   rD   r   )r1   r   rM   r>   r>   r?   %test_out_of_bounds_string_consistency  s    z?TestTimestampConstructors.test_out_of_bounds_string_consistencyc                 C   s   t t j d S N)r	   r   ri   r>   r>   r?   test_min_valid  s    z(TestTimestampConstructors.test_min_validc                 C   s   t t j d S r   )r	   r   ri   r>   r>   r?   test_max_valid  s    z(TestTimestampConstructors.test_max_validc                 C   s   t d}t  }t }t ddd}t jdd}tdd}t|| |k sNtt|| |k sbtt|| |k svtt|d |d  |k std S )Nnowr   r$   r   seconds)r	   r   r   r   absr)   tz_localizer1   Zts_from_stringZts_from_methodZts_datetimeZts_from_string_tzZts_from_method_tzdeltar>   r>   r?   test_now  s"    
z"TestTimestampConstructors.test_nowc                 C   s   t d}t  }t }t ddd}t jdd}tdd}t|| |k sNtt|| |k sbtt|| |k svtt|d |d  |k std S )Ntodayr   r$   r   r   )r	   r   r   r   r   r)   r   r   r>   r>   r?   
test_today  s"    
z$TestTimestampConstructors.test_todayr%   rT   c              	   C   s0   t d}d}tjt|d ||_W 5 Q R X d S )NZ2010zCannot directly set timezonerD   )r	   rG   rH   AttributeErrorr%   )r1   r%   r^   rM   r>   r>   r?   test_disallow_setting_tz  s    z2TestTimestampConstructors.test_disallow_setting_tzr;   z+0300z+0200c                 C   s0   t d| dd}t |d}||ks,td S )Nz2016-10-30 03:00:00zEurope/Helsinkir$   )r	   r0   r)   )r1   r;   r9   r:   r>   r>   r?   !test_construct_timestamp_near_dst  s    z;TestTimestampConstructors.test_construct_timestamp_near_dstz2013/01/01 00:00:00+09:00z2013-01-01 00:00:00+09:00c                 C   s2   t |}t tdddtdd}||ks.td S )Ni  r   i  r$   )r	   r   r*   r,   r)   )r1   r   r:   r9   r>   r>   r?   +test_construct_with_different_string_format  s    zETestTimestampConstructors.test_construct_with_different_string_formatc              	   C   sD   t jtdd ttdddj}W 5 Q R X t }||ks@td S )NrR   rD   z
2010-08-08r~   r   )rU   rV   rW   r	   r   r   ZDayr)   r   r>   r>   r?   4test_construct_timestamp_preserve_original_frequency  s    zNTestTimestampConstructors.test_construct_timestamp_preserve_original_frequencyc              
   C   sL   d}d}t jt|d, tjt|d tdg d W 5 Q R X W 5 Q R X d S )NrQ   rR   rD   rS   r   )rG   rH   rK   rU   rV   rW   r	   rX   r>   r>   r?   "test_constructor_invalid_frequency  s
    z<TestTimestampConstructors.test_constructor_invalid_frequencyboxc              	   C   sz   dddt jd}d}tjt|d t|f |dd W 5 Q R X d}tjt|d t|f |t dd W 5 Q R X d S )	N  r   rd   z#Cannot pass a datetime or TimestamprD   rT   r$   rO   )r*   r+   rG   rH   rK   r	   timezone)r1   r   kwargsrM   r>   r>   r?   *test_raise_tz_and_tzinfo_in_datetime_input   s    zDTestTimestampConstructors.test_raise_tz_and_tzinfo_in_datetime_inputc                 C   s>   t tdddt d}t tdddt }||ks:td S )Nr   r   r$   )r	   r   r   r   r)   r   r>   r>   r?   *test_dont_convert_dateutil_utc_to_pytz_utc+  s    zDTestTimestampConstructors.test_dont_convert_dateutil_utc_to_pytz_utcc                 C   s@   G dd dt }|ddd}t|}tddd}||ks<td S )Nc                   @   s   e Zd ZdS )zSTestTimestampConstructors.test_constructor_subclassed_datetime.<locals>.SubDatetimeN)__name__
__module____qualname__r>   r>   r>   r?   SubDatetime4  s   r   rk   r   )r   r	   r)   )r1   r   datar:   r9   r>   r>   r?   $test_constructor_subclassed_datetime0  s
    z>TestTimestampConstructors.test_constructor_subclassed_datetimez8datetime.fromisocalendar was added in Python version 3.8)reasonc                 C   sN   t d}tddd}t ddd}||ks0t||ks<tt|t sJtd S )Nz2000-01-03 00:00:00rk   r   )r	   r   fromisocalendarr)   
isinstance)r1   Zexpected_timestampZexpected_stdlibr:   r>   r>   r?    test_constructor_fromisocalendar<  s    z:TestTimestampConstructors.test_constructor_fromisocalendar).r   r   r   r@   rC   rN   rZ   r_   rj   rw   r}   rG   markfilterwarningsr   parametrizer	   r   r*   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zskipifr
   ZPY38r   r>   r>   r>   r?   r      s   AT	%





	


 


r   c                  C   s,   t ddd} | j}t | j}||ks(td S )Nl     !F zdateutil/Europe/Londonr$   )r	   r/   r)   )r^   r9   r:   r>   r>   r?   test_constructor_ambigous_dstJ  s    
r   epochl   ?2\BT, l   ?2\BT, c                 C   sD   t | dd}|j|}tdd}t |j| ks4t||ks@td S )Nzdateutil/America/Los_Angelesr$   r   r   )r	   r%   dstr   r/   r)   )r   r^   r:   r9   r>   r>   r?   "test_constructor_before_dst_switchU  s
    
r   c                  C   s    t d} t | }|| kstd S )Nz2017-01-01T12)r	   r)   )r9   r:   r>   r>   r?   #test_timestamp_constructor_identityb  s    r   r   ra   i  r   rx   c              	   C   s>   d}d}d ||g}tjt|d tf |  W 5 Q R X d S )NzCfunction missing required argument '(year|month|day)' \(pos [123]\)z<Required argument '(year|month|day)' \(pos [123]\) not found|rD   )joinrG   rH   rI   r	   )r   Zmsg1rY   rM   r>   r>   r?    test_constructor_missing_keywordi  s
    r   )r&   r   r   Zdateutil.tzr-   r   Znumpyr   rG   r*   Zpandas.compatr   Zpandas.errorsr   Zpandasr   r   r	   r
   Zpandas._testingZ_testingrU   Zpandas.tseriesr   r   r   r   r   r   r   r   r>   r>   r>   r?   <module>   s,       3
