a
    j=ic\                     @   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   s/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/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 sD0    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 s80    Y  tjt|d& tdgdgd|jd< W d    n1 s|0    Y  d}tjt|d" d|jd d df< W d    n1 s0    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    (4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|j|d< t|| t  }|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_frame3q   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g 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   st  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tg d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   sX  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 s0    Y  d}tjt|d	 d|j	d
< W d    n1 sJ0    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   s0    

*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 sZ0    Y  d}|rpd}tjt|d	 d
|j	d< W d    n1 s0    Y  t t
g dg dd}| }|jd |jd< t|| t t
g dg dd}| }|jd |jd< t|| t t
g dg dd}| }|jd |jd< t|| t t
g dg dd}| }|jd d df |jd d df< t|| t t
g dtg dd}| }|d tj|d< d}tjt|d	0 |jd d df |jd d df< W d    n1 s40    Y  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   z.will attempt to set the values inplace insteadrR   )r   r'   r@   ZreshaperY   r2   r3   re   rf   rg   dictr   r   r&   r   r?   r$   Zassert_produces_warningFutureWarning)r   Zusing_array_managerdf_origr   r6   r   r   r   r   test_partial_setting_frame  sZ    (( @  z-TestPartialSetting.test_partial_setting_framec                 C   s8  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   )r   r   rR   DrE   r   r1   r   T)sortr   r"   Zaxis)r   r   r'   randomZrandnr   concatfreqrY   r&   r   r   at)r   datesrl   r   r   Z	exp_otherr   r   r   test_partial_setting2M  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   ru   Tinfer_objectsr   r   )r   r   rh   r   r   r   r    test_partial_setting_mixed_dtypeh  s    z3TestPartialSetting.test_partial_setting_mixed_dtypec                 C   sB  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 s`0    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 s0    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 s0    Y  |g d}tj	||d	d
 d}tjt|d |jg d  W d    n1 s0    Y  t ddtjgg dd}tjtdd |jg d  W d    n1 s60    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 s0    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 sH0    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 s0    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 sZ0    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 s0    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"   zM\"None of \[Int64Index\(\[3, 3, 3\], dtype='int64'\)\] 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   rf   )r   serr   rV   r6   rh   r   r   r   test_series_partial_setu  sz    ,,........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 sV0    Y  tjtdd |jg d  W d    n1 s0    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 s0    Y  d}tjt|d |jg d  W d    n1 sT0    Y  tjtdd |jg d  W d    n1 s0    Y  t g dddd}tjtdd, tg d|dd	jg d  W d    n1 s0    Y  t g dddd}tjtdd, tg d|dd	jg d  W d    n1 sN0    Y  t g dddd}tjtdd, tg d|dd	jg d  W d    n1 s0    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
0    Y  t g dddd}tjtdd, tg d|dd	jg d!  W d    n1 sh0    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   zY\"None of \[Int64Index\(\[3, 3, 3\], dtype='int64', 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   rf   )r   r   r   Zexp_idxr   rV   r6   r   r   r   !test_series_partial_set_with_name  sJ    ,,...<<<<<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   rs   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sJ d S )Nr   rM   r
   r:   )r   r   rY   rf   r&   r   r   ru   r   rz   r{   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   rp   )startro   rv   )
2000-01-04
2000-01-08
2000-01-12r   )rv   r   r   z1 day)r   ro   )4D8DZ12Dz4 dayz8 dayz12 dayc                 C   sP   |t d|d}g d}|||}t||j|  |tu rLt|||  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 sR0    Y  tjt|d ||  W d    n1 s0    Y  tjt|d |j|  W d    n1 s0    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_valueP  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 sN0    Y  tjt|d ||  W d    n1 s0    Y  tjt|d |j|  W d    n1 s0    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_typem  s    !(&zXTestPartialSetting.test_loc_with_list_of_strings_representing_datetimes_not_matched_typeN)r_   r`   ra   ri   rm   ry   r|   r   r   r2   markZparametrizer   r   r   r   r   r   r   Ztimedelta_rangeZ	Timedeltar   r   r   r   r   r   r   rb      sz   &@[:







		rb   c                   @   s   e Zd Zdd ZdS )TestStringSlicingc                 C   sb   t g d}ttt||d}ttt|d d |d d d}|dd }t|| d S )N)
2012-01-01z
2012-01-02z
2012-01-03Nr   r/   r   z
2012-01-04)r   to_datetimer   r%   rA   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   $	 P   6