U
    c_                     @   s   d dl Z d dlmZ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 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timezone)tzutc)PY310)OutOfBoundsDatetime)Period	Timedelta	Timestamp)offsetsc                   @   s  e Zd Zejdeegd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dd Zejjddejdddddg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)d0ed(d)d*d+d,d-d.d)d1d2	ed(d)d*d+d,d-d.d)d3	ed(d)d*d+d,d-d.d)ej	gd4d5 Zejd6d7d8gd9d: Zejd;d<d=d>d?ddddgd@dA ZdBdC ZdDdE Z dFdG Z!dHdI Z"dJdK Z#ejd;dLdMgdNdO Z$dPdQ Z%dRdS Z&dTdU Z'dVdW Z(ejdXd3e)dYgdZd[ Z*ejd\d]d^gd_d` Z+ejd;dadbgdcdd Z,dedf Z-dgdh Z.ejdieegdjdk Z/dldm Z0dndo Z1dpdq Z2d3S )rTestTimestampConstructorstypc                 C   sL   |d}t |dd}t d}||ks(tt |dd}t d}||ksHtd S )N   Yunitz
2120-01-01Mz
1982-07-01r
   AssertionError)selfr   valtsexpected r   S/tmp/pip-unpacked-wheel-g7fro6k3/pandas/tests/scalar/timestamp/test_constructors.py'test_constructor_int_float_with_YM_unit   s    zATestTimestampConstructors.test_constructor_int_float_with_YM_unitc              	   C   sP   t jtdd tddd W 5 Q R X t jtdd tddd W 5 Q R X d S )NZ	ambiguousmatchg     b@r   r   r   )tmassert_produces_warningFutureWarningr
   r   r   r   r   8test_constructor_float_not_round_with_YM_unit_deprecated)   s    zRTestTimestampConstructors.test_constructor_float_not_round_with_YM_unit_deprecatedc              	   C   s   t d}d}d}tjt|d t||d}W 5 Q R X t|d|}||ksXt|j	dksftt||}||kstd S )Nz1970-01-01 05:00:00z	UTC+05:00zinterpreted as a wall timer   tzUTC   )
np
datetime64r   r   r    r
   tz_localize
tz_convertr   hour)r   dtZtzstrmsgr   ZaltZexpected_futurer   r   r   #test_constructor_datetime64_with_tz2   s    
z=TestTimestampConstructors.test_constructor_datetime64_with_tzc                 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.000000005r&   Nr   r%   r   
Asia/Tokyor2   
US/Easternzdateutil/US/PacificiLPF  r#   i  r%   )r   calendartimegm	timetupler   r
   pytzutcFixedOffsetdateutilr$   tzoffsetvaluer*   )r   base_strbase_dtbase_expectedtests	timezonesdate_strdater   resultr$   offsetexpected_tzexpected_utcr   r   r   test_constructorD   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/   r0   r1   r2   r3   r4   z2014-07-01 12:00:00+02:00r6   z#2014-07-01 11:00:00.000008000+02:00r8   z#2014-07-01 11:00:00.000000005+02:00r&   r9   r:   r   r;   r=   r@   rA   rB   rC   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   rD   rE   rF   r   rG   rH   rI   rJ   r$   rK   r
   rL   r*   repreval)r   rM   rN   rO   rP   rQ   rR   r   rT   r$   rU   rV   rW   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 inputr      zCannot convert Periodz
1000-01-01)pytestraises	TypeErrorr
   slice
ValueErrorr   r   r-   r   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\)r   z
2017-10-22r>   tzinfozat most one ofr%   )re   r$   Invalid frequency:The 'freq' argument
2012-01-01
US/Pacific)
r]   r^   r_   r
   ra   rG   rH   r   r   r    r   r-   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 implementedr   )r]   r^   NotImplementedErrorr
   strptime)r   fmtr   r-   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  r5      r%   )yearmonthdayr$   rr   rs   rt   re   rd   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
   rG   rH   r   allr   r!   r   rw   r   test_constructor_tz_or_tzinfo   s    z7TestTimestampConstructors.test_constructor_tz_or_tzinfoc                 C   s0   t dddtjd}t dtjd}||ks,td S )N        rd   
2020-12-31)r
   r   rH   r   )r   r   r   r   r   r   'test_constructor_positional_with_tzinfo  s    zATestTimestampConstructors.test_constructor_positional_with_tzinfozGH#45307)reasonkwd
nanosecondmicrosecondsecondminutec                 C   sT   |di}t d	dtji|}|d di}tf |}t dtjd| }||ksPtd S )
N   r{   r|   r}   re   sr~   r#   )r{   r|   r}   )r
   r   rH   r	   r   )r   r   kwargsr   Z	td_kwargstdr   r   r   r   5test_constructor_positional_keyword_mixed_with_tzinfo  s    
zOTestTimestampConstructors.test_constructor_positional_keyword_mixed_with_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 requiredr     r1   month must be in 1..12r      day is out of range for month         r|   20151112r\      ?B 2015-11-12 01:02:03.999999)r   r]   r^   r_   r
   ra   rY   r   rb   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'r   r   r1   rr   rs   r   r   )rr   rs   rt   r   r   r   r   r   r|   r   r\   r   r   )rr   rs   rt   r+   r   r   r   r   )r]   r^   r_   r
   ra   rY   r   rb   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 )Nr   r1   Dfreqr>   r#   z
2000-01-01i  r      r   z	2011-4-16)r   r
   fromordinal	toordinalr   r   Zto_pydatetime)r   baser   r,   ZstampZdt_tzr   r   r   test_constructor_fromordinal]  s    z6TestTimestampConstructors.test_constructor_fromordinalrT   r   r1   r\   r   r   r&      )r   )rr   rs   rt   r+   r   r   r   r   r%   )	rr   rs   rt   r+   r   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 )
Nr   r1   r\   r   r   r&   r   r#   )Znanoseconds)r
   r   r$   r	   r   r   rT   r   r   r   r   test_constructor_nanosecondv  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 Timestampr   z2014-11-02 01:00r]   r^   ra   r
   )r   r   r-   r   r   r   %test_constructor_invalid_Z0_isostring  s    z?TestTimestampConstructors.test_constructor_invalid_Z0_isostringargrr   rs   rt   r+   c              	   C   s4   |di}d}t jt|d td| W 5 Q R X d S )Nr1   z-Cannot pass a date attribute keyword argumentr   2010-10-10 12:59:59.999999999)r   r   )r   r   kwargr-   r   r   r   )test_invalid_date_kwarg_with_string_input  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 )Nr\   r   )strr
   maxrL   r]   r^   r   minrb   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 )Nr1   ztimedelta64[us]zM8[us]Out of boundsr   )
r'   Ztimedelta64Zastyper(   r
   r   r   r]   r^   ra   )r   Zone_usZ	min_ts_usZ	max_ts_usr-   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   r   z
1676-01-01z
2263-01-01r   rb   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:16r   z2262-04-11 23:47:16.854775808r]   r^   r   r
   rb   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mr   msusr   r   )z
1677-09-23z
2262-04-11)r'   r(   r]   r^   r   r
   )r   Zout_of_bounds_datesZ
time_unitsZdate_stringr   Zdt64r-   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   r   r   )r   r   r-   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   r!   r   r   r   test_min_valid  s    z(TestTimestampConstructors.test_min_validc                 C   s   t t j d S r   )r
   r   r!   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#   r1   seconds)r
   r   r   r	   absr   r)   r   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#   r1   r   )r
   r   r   r	   r   r   r)   r   r   r   r   
test_today  s"    
z$TestTimestampConstructors.test_todayr$   ri   c              	   C   s0   t d}d}tjt|d ||_W 5 Q R X d S )NZ2010zCannot directly set timezoner   )r
   r]   r^   AttributeErrorr$   )r   r$   r   r-   r   r   r   test_disallow_setting_tz1  s    z2TestTimestampConstructors.test_disallow_setting_tzrU   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
   r*   r   )r   rU   r   rT   r   r   r   !test_construct_timestamp_near_dst9  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  r1   i  r#   )r
   r   rG   rI   r   )r   r   rT   r   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 )Nrg   r   z
2010-08-08r   r   )r   r   r    r
   r   r   ZDayr   r   r   r   r   4test_construct_timestamp_preserve_original_frequencyI  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 )Nrf   rg   r   rh   r   )r]   r^   ra   r   r   r    r
   rj   r   r   r   "test_constructor_invalid_frequencyP  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  r1   ru   z#Cannot pass a datetime or Timestampr   ri   r#   rd   )rG   rH   r]   r^   ra   r
   r   )r   r   r   r-   r   r   r   *test_raise_tz_and_tzinfo_in_datetime_inputX  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   r1   r#   )r
   r   r   r)   r   r   r   r   r   *test_dont_convert_dateutil_utc_to_pytz_utcc  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   SubDatetimel  s   r   r   r1   )r   r
   r   )r   r   datarT   r   r   r   r   $test_constructor_subclassed_datetimeh  s
    z>TestTimestampConstructors.test_constructor_subclassed_datetimec                 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:00r   r1   )r
   r   fromisocalendarr   
isinstance)r   Zexpected_timestampZexpected_stdlibrT   r   r   r    test_constructor_fromisocalendart  s    z:TestTimestampConstructors.test_constructor_fromisocalendar)3r   r   r   r]   markparametrizeintfloatr   r"   r.   rX   r[   rc   rl   rp   rz   r   Zxfailr   r   r   filterwarningsr   r
   r   rG   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   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
   rL   r   )r   r   rT   r   r   r   test_constructor_ambigous_dst~  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   rL   r   )r   r   rT   r   r   r   r   "test_constructor_before_dst_switch  s
    
r   c                  C   s    t d} t | }|| kstd S )Nz2017-01-01T12r   )r   rT   r   r   r   #test_timestamp_constructor_identity  s    r   r   rr   r{   r1   r   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|r   )joinr]   r^   r_   r
   )r   Zmsg1rk   r-   r   r   r    test_constructor_missing_keyword  s
    r   )rD   r   r   r   Zdateutil.tzrJ   r   Znumpyr'   r]   rG   Zpandas.compatr   Zpandas.errorsr   Zpandasr   r	   r
   Zpandas._testingZ_testingr   Zpandas.tseriesr   r   r   r   r   r   r   r   r   r   r   r   <module>   s,       g
