o
    7?e^                     @   sz   d Z ddlZddlZddlZddlmZmZmZm	Z	m
Z
mZmZ ddlmZ G dd dZG dd dZG dd	 d	ZdS )
zx
test setting *parts* of objects both positionally and label based

TODO: these should be split among the indexer tests
    N)	DataFrameIndexPeriodSeries	Timestamp
date_rangeperiod_rangec                   @   s|   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
dd Zdd Zdd Zdd Zdd Zdd ZdS )TestEmptyFrameSetitemExpansionc                 C   sd   t i tjdddd}tdtjdddd}||d< t ddgd itjdddd}t|| d S )	Nr   Zdf_indexnameindexGz?   series_indexseries)r   pd
RangeIndexr   tmassert_frame_equalselfdfr   expected r   c/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/pandas/tests/indexing/test_partial.py,test_empty_frame_setitem_index_name_retained   s   zKTestEmptyFrameSetitemExpansion.test_empty_frame_setitem_index_name_retainedc                 C   sT   t  }tdtjdddd}||d< t ddgd itjdddd}t|| d S )Nr   r   r   r
   r   r   )r   r   r   r   r   r   r   r   r   r   -test_empty_frame_setitem_index_name_inherited$   s   zLTestEmptyFrameSetitemExpansion.test_empty_frame_setitem_index_name_inheritedc                 C   s   t ddgd}tdtdd|jd< t ddgdgtjd}t|| t ddgd}tddgd|jd< t tjdggddgdgd	d}t|| d S )
NABcolumns   r   r   r   )r!   r   dtypefloat64)	r   r   rangelocnpr$   r   r   nan)r   r   r   expr   r   r   -test_loc_setitem_zerolen_series_columns_align.   s   zLTestEmptyFrameSetitemExpansion.test_loc_setitem_zerolen_series_columns_alignc                 C   s   t ddgd}d}tjt|d g d|jd< W d    n1 s"w   Y  t ddgd}dd	g|jd
< t dd	ggd
gddgtjd}t|| d S )Nr   r   r    z(cannot set a row with mismatched columnsmatchr"         r         r/   )r   r!   r#   )	r   pytestraises
ValueErrorr&   r'   int64r   r   )r   r   msgr)   r   r   r   7test_loc_setitem_zerolen_list_length_must_match_columns<   s   zVTestEmptyFrameSetitemExpansion.test_loc_setitem_zerolen_list_length_must_match_columnsc                 C   s   t  }d}tjt|d d|jd< W d    n1 sw   Y  tjt|d tdgdgd|jd< W d    n1 s>w   Y  d}tjt|d d|jd d df< W d    d S 1 saw   Y  d S )Nz*cannot set a frame with no defined columnsr+   r"   foor   z5cannot set a frame with no defined index and a scalar)r   r2   r3   r4   r&   r   )r   r   r6   r   r   r   test_partial_set_empty_frameJ   s   "z;TestEmptyFrameSetitemExpansion.test_partial_set_empty_framec                 C   s   t dgtg ddd}t tg ddd}tg dd|d< t|| t tg d}t|j|d< t|| t tg d}|j|d< t|| d S )Nr8   objectr#   r!   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   test_partial_set_empty_frame2[   s   
z<TestEmptyFrameSetitemExpansion.test_partial_set_empty_frame2c                 C   s   t dgtg ddd}|d d|d< t tg ddd}g |d< t|| t tg ddd}ttt|dd|d< t|| d S )Nr8   r5   r;   r<   r$   r   )	r   r   astyper   r   r   r'   arangelenr=   r   r   r   test_partial_set_empty_frame3p   s   z<TestEmptyFrameSetitemExpansion.test_partial_set_empty_frame3c                 C   sZ   t tg ddd}tt||d< t dgtg ddd}|d d|d< t|| d S )Nr5   r;   r   r8   r<   )r   r   r%   rA   r?   r   r   r   r   r   r   r   r   test_partial_set_empty_frame4~   s
   z<TestEmptyFrameSetitemExpansion.test_partial_set_empty_frame4c                 C   s   t  }t|jtd t  }tdgdgd|d< tdgdgd|jd d df< t|t dggdgdgd t|| d S )Nr   r"   r8   r   r   r!   )	r   r   assert_index_equalr!   r   r   r   r&   r   )r   r   Zdf2r   r   r   test_partial_set_empty_frame5   s   z<TestEmptyFrameSetitemExpansion.test_partial_set_empty_frame5c                 C   s   t dtdtddig dd}t ddgd}tdtdd|d< |j t| t|| t ddgd}tdtdd|jd d df< |j t| t|| d S )	Nr   r"   r   r   )r   r   r   r    r   r   )r   r   r%   Zdtypesstrr   r   r&   r=   r   r   r   %test_partial_set_empty_frame_no_index   s    zDTestEmptyFrameSetitemExpansion.test_partial_set_empty_frame_no_indexc                 C   sj  t g dtg ddd}|d d|d< |d d|d< |d d|d< t g d	g d
d}||jdk }tj|d< t|| t g dd}|d d|d< t g dd}d|d< t|| t|d t	dt
d t g d	g d
d}||jdk }|jg dd}t g dd}|d d|d< |d d|d< |d d|d< t|| d S )N)r   r   Newr5   r;   r<   r   r   r$   rJ   r-   )g333333?g@g@r   r      )abc cdr    rP   )rM   rN   rO   r/   rO   r   r#   )r   r   CrR   )r   r   r?   r   r'   r(   r   r   assert_series_equalr   r:   reindex)r   r   r   yresultr   r   r    test_partial_set_empty_frame_row   s,   
z?TestEmptyFrameSetitemExpansion.test_partial_set_empty_frame_rowc                 C   s^   t ttd}t dttdi}t|| t tdtd}t dttdi}t|| d S )Nr;   r   r8   rQ   )r   r   r:   r   r   rC   r   r   r   'test_partial_set_empty_frame_set_series   s   zFTestEmptyFrameSetitemExpansion.test_partial_set_empty_frame_set_seriesc                 C   s>   t dgd}| }d|d< t ddgdgd}t|| d S )Nr   r   rM   rE   )r   copyr   r   rC   r   r   r   2test_partial_set_empty_frame_empty_copy_assignment   s
   zQTestEmptyFrameSetitemExpansion.test_partial_set_empty_frame_empty_copy_assignmentc                 C   s   t ddgd}ddg|d< t ddgtjtjgd}tj||dd t ddgd}d	d
g|d< t d	d
gtjtjgdtd}t|| t ddgd}d|jd< t dgtjgd}tj||dd d S )NxrU   r    r"   r.   )r[   rU   F)Zcheck_dtype12r;   )r   r[   )r   r'   r(   r   r   r:   r&   rC   r   r   r   0test_partial_set_empty_frame_empty_consistencies   s   
zOTestEmptyFrameSetitemExpansion.test_partial_set_empty_frame_empty_consistenciesN)__name__
__module____qualname__r   r   r*   r7   r9   r>   rB   rD   rG   rI   rW   rX   rZ   r^   r   r   r   r   r	      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ddgdd Zdd Ze	j
deddddg dedddedddedddgfeddddg dedededgfejdddg d ed!ed"ed#gfgd$d% Ze	j
d&edddddd'gfedddddd'gfejdddd(d)gfgd*d+ Ze	j
d,eddddd-d.gd/feddddd-d.gd/fejdddddgd0fgd1d2 Zd3S )4TestPartialSettingc                 C   sV  t g d}| }d|d< t g dg dd}t|| | }d|jd< t g dg dd}t|| | }d|d< t g dg dd}t|| | }d|jd< t g dg dd}t|| | }d}tjt|d	 d|jd
< W d    n1 sw   Y  d}tjt|d	 d|j	d
< W d    d S 1 sw   Y  d S )Nr-   rL   )r"   r.   r/   rL   )r   r"   r.   rL   r         @)r"   r.   r/   rc   %iloc cannot enlarge its target objectr+   r/   z/index 3 is out of bounds for axis 0 with size 3)
r   rY   r   rS   r&   r2   r3   
IndexErrorilociat)r   Zs_origsr   r6   r   r   r   test_partial_setting   s4   

"z'TestPartialSetting.test_partial_settingc                 C   s  t tdddddgdd}| }d}tjt|d	 d
|jd< W d    n1 s-w   Y  d}|r8d}tjt|d	 d
|j	d< W d    n1 sOw   Y  t g dg dd}| }|jd |jd< t
|| t g dg dd}| }|jd |jd< t
|| t g dg dd}| }|jd |jd< t
|| t g dg dd}| }|jd d df |jd d df< t
|| t g dtg dd}| }|d tj|d< |jd d df |jd d df< t
|| | }|d |d< | }|jd d df |jd d df< t
|| | }|d |d< | }|jd d df |jd d df< t
|| d S )Nr0   r/   r.   r   r   r5   )r!   r#   rd   r+   rc   )r   r.   z/index 2 is out of bounds for axis 0 with size 2zlist index out of range)r   r   r   )r"   rL   rL   rK   r"   )r   r.   r   r   )r"   r/   rL   rL   )r   r.   r   )g        g       @g      @rR   )r   r'   r@   ZreshaperY   r2   r3   re   rf   rg   r   r   r&   r   r?   r$   )r   Zusing_array_managerdf_origr   r6   r   r   r   r   test_partial_setting_frame  sZ       z-TestPartialSetting.test_partial_setting_framec                 C   s<  t ddd}ttjdd|g dd}tj|tdd	i|d
d  |j dgdd}|	 }d	|j
|d
 |j df< t|| |	 }d	|j|d
 |j df< t|| tdd	i|d
d  |j d}tj||gdd}|	 }d	|j
|d
 |j df< t|| |	 }d	|j|d
 |j df< t|| d S )Nz1/1/2000   )periodsr.   )rl   r   )r   r   rR   DrE   r   r1   r   T)sortr   r"   Zaxis)r   r   r'   randomZdefault_rngZstandard_normalr   concatfreqrY   r&   r   r   at)r   datesrj   r   r   Z	exp_otherr   r   r   test_partial_setting2K  s.   "z(TestPartialSetting.test_partial_setting2c                 C   sh   t ddgddggddgd}|jd  }d|_t|t |j g}|jd |jd< t	|| d S )NTr"   Fr.   ZfemaleZfitnessr    )
r   r&   rY   r   r   rs   Tinfer_objectsr   r   )r   r   rh   r   r   r   r    test_partial_setting_mixed_dtypeh  s   z3TestPartialSetting.test_partial_setting_mixed_dtypec                 C   sL  t ddgddgd}t tjdtjgg dd}tjtdd |jg d  W d    n1 s0w   Y  |g d}tj	||d	d
 t tjdtjtjgg dd}tjtdd |jg d  W d    n1 slw   Y  |g d}tj	||d	d
 t g dg dd}|jg d }tj	||d	d
 t ddtjdgg dd}tjtdd |jg d  W d    n1 sw   Y  |g d}tj	||d	d
 dt
t d}tjt|d |jg d  W d    n1 sw   Y  t ddtjgg dd}tjtdd |jg d  W d    n	1 s w   Y  |g d}tj	||d	d
 t g dg dd}t dtjtjgg dd}tjtdd |jg d  W d    n	1 sew   Y  |g d}tj	||d	d
 t g dg dd}t tjddgg dd}tjtdd |jg d  W d    n	1 sw   Y  |g d}tj	||d	d
 t g dg dd}t tjddgg dd}tjtdd |jg d  W d    n	1 sw   Y  |g d}tj	||d	d
 t g dg dd}t dtjtjgg dd}tjtdd |jg d  W d    n	1 s2w   Y  |g d}tj	||d	d
 t g dg dd}t dtjtjgg dd}tjtdd |jg d  W d    n	1 sww   Y  |g d}tj	||d	d
 t g dg d d}|jg d! }tj	||d	d
 d S )"N皙?皙?r"   r.   r   r/   r.   r/   not in indexr+   TZcheck_index_typer/   r.   r/   r[   r|   r|   r{   r.   r.   r"   r.   r.   r[   r"   '\"None of \[Index\(\[3, 3, 3\], dtype='z'\)\] are in the \[index\]\"r/   r/   r/   r.   r.   r/   r{   r|   333333?r-   r   r/   r   r   r{   r|   r   皙?r"   r.   r/   r   rL   r/   r/   r   rL   r   r   r   rL   r0   r1   r1   r.   r.   r   rL   rL   r|   r|   r{   r{   r.   r.   r"   r"   r"   r"   r   r   )r   r'   r(   r2   r3   KeyErrorr&   rT   r   rS   r#   intrf   )r   serr   rV   r6   rh   r   r   r   test_series_partial_sett  s   z*TestPartialSetting.test_series_partial_setc                 C   s  t ddgddd}tddg|dd	}tjtd
d |jg d  W d    n1 s+w   Y  tjtdd |jg d  W d    n1 sIw   Y  t g dddd}tg d|dd	}|jg d }tj||dd tjtdd |jg d  W d    n1 sw   Y  dt	t
 d}tjt|d |jg d  W d    n1 sw   Y  tjtdd |jg d  W d    n1 sw   Y  t g dddd}tjtdd tg d|dd	jg d  W d    n1 sw   Y  t g dddd}tjtdd tg d|dd	jg d  W d    n	1 s*w   Y  t g dddd}tjtdd tg d|dd	jg d  W d    n	1 sYw   Y  t g d ddd}tjtdd tg d|dd	jg d!  W d    n	1 sw   Y  t g dddd}tjtdd tg d|dd	jg d"  W d    n	1 sw   Y  t g d#ddd}tg d$|dd	}|jg d% }tj||dd d S )&Nr"   r.   r5   idxr#   r   r{   r|   rh   )r   r   z\[3\] not in indexr+   r}   r~   r   r   r   Tr   z\['x'\] not in indexr   r   z(', name='idx'\)\] are in the \[index\]\"r   r   r-   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r2   r3   r   r&   r   rS   r'   r#   r   rf   )r   r   r   Zexp_idxr   rV   r6   r   r   r   !test_series_partial_set_with_name  s^   z4TestPartialSetting.test_series_partial_set_with_namekeyd   g      Y@c                 C   s   t  }| }|jd |j|d d f< tt|j|g t|jj	d}t
j|j|jdg jgdd}t|||jd}t || d S )Nr   r   rq   rE   )r   makeTimeDataFramerY   rf   r&   r   listr   r:   r   r'   Zconcatenatevaluesr   r!   r   )r   r   origr   Zex_indexZex_datar   r   r   r   6test_setitem_with_expansion_numeric_into_datetimeindex	  s   zITestPartialSetting.test_setitem_with_expansion_numeric_into_datetimeindexc                 C   s   t  }| }|jd |jdd d f< t|jd dd}t|t|j	
 g}t || t |jt|j dg  |jjdksFJ d S )Nr   rM   r
   r:   )r   r   rY   rf   r&   r   r   rs   r   rx   ry   r   rF   r   r   tolistr#   )r   r   r   r   r)   r   r   r   test_partial_set_invalid  s   z+TestPartialSetting.test_partial_set_invalidzidx,labels,expected_idx2000   rn   )startrm   rt   )
2000-01-04
2000-01-08
2000-01-12r   )rt   r   r   z1 day)r   rm   )4D8DZ12Dz4 dayz8 dayz12 dayc                 C   sT   |t d|d}g d}|||}t||j|  |tu r(t|||  d S d S )Nr   r   )r/   r1      )r%   r   Zassert_equalr&   r   rS   )r   r   labelsZexpected_idxZframe_or_seriesobjZexpected_valuer   r   r   r   4test_loc_with_list_of_strings_representing_datetimes%  s    
zGTestPartialSetting.test_loc_with_list_of_strings_representing_datetimesz
idx,labelsz
2000-01-30z3 dayz30 dayc                 C   s   t td|d}ttd|d}d}tjt|d |j|  W d    n1 s)w   Y  tjt|d ||  W d    n1 sDw   Y  tjt|d |j|  W d    d S 1 saw   Y  d S )Nr   r   r~   r+   r   r%   r   r2   r3   r   r&   )r   r   r   r   r   r6   r   r   r   Btest_loc_with_list_of_strings_representing_datetimes_missing_valueN  s   
"zUTestPartialSetting.test_loc_with_list_of_strings_representing_datetimes_missing_valuezidx,labels,msgr   r   zHNone of \[Index\(\['4D', '8D'\], dtype='object'\)\] are in the \[index\]zXNone of \[Index\(\['2000-01-04', '2000-01-08'\], dtype='object'\)\] are in the \[index\]c                 C   s   t td|d}ttd|d}tjt|d |j|  W d    n1 s'w   Y  tjt|d ||  W d    n1 sBw   Y  tjt|d |j|  W d    d S 1 s_w   Y  d S )Nr   r   r+   r   )r   r   r   r6   r   r   r   r   r   Etest_loc_with_list_of_strings_representing_datetimes_not_matched_typek  s   !
"zXTestPartialSetting.test_loc_with_list_of_strings_representing_datetimes_not_matched_typeN)r_   r`   ra   ri   rk   rw   rz   r   r   r2   markZparametrizer   r   r   r   r   r   r   Ztimedelta_rangeZ	Timedeltar   r   r   r   r   r   r   rb      s|    %@[:







		rb   c                   @   s   e Zd Zdd ZdS )TestStringSlicingc                 C   s   t g d}ttt||d}ttt|d d |d d d}tjtdd |dd  W d    n1 s;w   Y  |dd }t	|| d S )	N)
2012-01-01z
2012-01-02z
2012-01-03Nr   r/   z non-existing keys is not allowedr+   r   z
2012-01-04z2012-01-03 00:00:00.000000000)
r   to_datetimer   r%   rA   r2   r3   r   r   r   )r   r   r   r   rV   r   r   r   ,test_slice_irregular_datetime_index_with_nan  s   $	z>TestStringSlicing.test_slice_irregular_datetime_index_with_nanN)r_   r`   ra   r   r   r   r   r   r     s    r   )__doc__numpyr'   r2   Zpandasr   r   r   r   r   r   r   r   Zpandas._testingZ_testingr   r	   rb   r   r   r   r   r   <module>   s    $	 O   5