o
    ҷhc                     @   s   d Z ddlmZm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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 )z/
See also: test_reindex.py:TestReindexSetIndex
    datetime	timedeltaN)	Categorical	DataFrameDatetimeIndexIndex
MultiIndexSeries
date_rangeperiod_rangeto_datetimec                   @   sh  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ejdddddgdgejdddgejdddgdd Zejdddddgdgejdddgdd Zejdddddgdgejdddgd d! Zd"d# Zejd$eeejed%d& d'd& gejd(g d)ejdddgd*d+ Zejd$eeejed,d& gejd(g d-ejdddgd.d/ Zejd0eeejeed1d& d2d& gejd3eeejeed4d& d5d& gejd(g d6ejdddgd7d8 Zejd9ddgejdddgd:d; Zd<d= Zd>d? Zd@dA ZdBdC Z dDS )ETestSetIndexc                 C   sF   g dg dd}t |}g d}||d< t|d }|| d S )N)   g      @   )         )t1t2))r      )r   r   )r   r   tuples)r   r	   from_tuples	set_index)selfddfr   index r   \/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/frame/methods/test_set_index.pytest_set_index_multiindex   s   z&TestSetIndex.test_set_index_multiindexc                 C   s~   t ddddddddd	d
dddd
gg dd}|ddg}|ddg }tj|d |d gddgd|_t|| d S )Nr   r   )apr   
   )r!   mr         )r!   r$   r"   r         )r!   r$   r"   xcolumnsr!   r)   r$   r"   names)r   r   r	   from_arraysr   tmassert_frame_equal)r   r   resultexpectedr   r   r   test_set_index_empty_column'   s   


 z(TestSetIndex.test_set_index_empty_columnc                 C   sT   t tddtddg d}|ddg}|j j}|ddg j}t|| d S )Nzdatetime64[ns])dtypeint64r!   bcr!   r7   )r   r
   r   r   to_framedtypesr/   assert_series_equal)r   df1df2r1   r2   r   r   r   test_set_index_empty_dataframe9   s   z+TestSetIndex.test_set_index_empty_dataframec                 C   s   t g d}ttjdd|d}||jd }|j	d d dd f }|j	d d df j
|_|jd g|j_t|| d S )N))foor   )r?   r   )barr   r   )r   r   r*   r   r   )r	   r   r   nprandomdefault_rngstandard_normalr   r+   ilocvaluesr   r-   r/   r0   )r   r+   r   r1   r2   r   r   r    test_set_index_multiindexcolumnsD   s   z-TestSetIndex.test_set_index_multiindexcolumnsc                 C   sv   t dgddd}td|i}||jd jdksJ t t|jd jdks+J ||jjd jdks9J d S )Nz2014-01-01 10:10:10UTCtzzEurope/RomeAr   r%   )r   
tz_convertr   r   r   hourr
   rK   )r   idxr   r   r   r   test_set_index_timezoneQ   s
    z$TestSetIndex.test_set_index_timezonec                 C   sH   t dd tdD tjddd}|d}t|jt	s"J d S )Nc                 S   s    g | ]}t d ddt| qS )i  r   r   ).0ir   r   r   
<listcomp>]   s     zBTestSetIndex.test_set_index_cast_datetimeindex.<locals>.<listcomp>i  r   rK   BrK   )
r   rangerA   rB   rC   rD   r   
isinstancer   r   )r   r   idfr   r   r   !test_set_index_cast_datetimeindexZ   s   
z.TestSetIndex.test_set_index_cast_datetimeindexc                 C   s   t ddddd}tg dg dd|d	 }|d
}tg dg ddt|d
dd	}|jd |_t|| |d
dg}t	j
|g dgd
dgd}tdg di|d}t|| d S )Nz2006-10-29 00:00:00r   H
US/Pacific)periodsfreqrJ   r   r   r   )r   r   r   )r!   r7   )datar   r   namer!   r,   r7   r   )r   r   reset_indexr   r   r   
_with_freqr/   r0   r	   r.   )r   dir   resexp	exp_indexr   r   r   test_set_index_dste   s   

zTestSetIndex.test_set_index_dstc                 C   s   |}t tt|d d d }||}t|j| tj	t
dd ||d d d  W d    d S 1 s9w   Y  d S )NzLength mismatchmatchr   )r   rA   arangelenr   r/   assert_index_equalr   pytestraises
ValueError)r   float_string_framer   rN   r   r   r   test_set_indexx   s   
"zTestSetIndex.test_set_indexc                 C   s  t  }d|j_||jjjdgksJ tj|ddg jj	ddgd}tj|g d jj	g dd}|ddg}||jjjddgksHJ t
||jjtsTJ t ||jj| |jddg}t
||j|gjtsuJ t ||j|gj| d S )	Nr`   rK   rT   r,   )rK   rT   rK   rT   )rK   rT   CDrt   ru   )r/   makeDataFramer   r`   r   r-   r	   r.   TrF   rV   rn   rename)r   r   mimi2idx2r   r   r   test_set_index_names   s   z!TestSetIndex.test_set_index_nameskeysrK   rt   rT   )tupleaslabelinplaceTFdropc           	         s   | t |trtj fdd|D |d}nt | |d}|r' j|ddn }||_|rA  }|j||dd}|d u s@J n j||d	}t	
|| d S )
Nc                       g | ]} | qS r   r   rP   r)   r   r   r   rR          z<TestSetIndex.test_set_index_drop_inplace.<locals>.<listcomp>r,   r_   r   axisT)r   r   )r   )rV   listr	   r.   r   r   r   copyr   r/   r0   )	r   frame_of_index_colsr   r   r}   rN   r2   r1   return_valuer   r   r   test_set_index_drop_inplace   s   
z(TestSetIndex.test_set_index_drop_inplacec                    s   | t |tr	|n|g}tj jg fdd|D  d g| d}|r* j|ddn  }||_ j||dd}t	|| d S )Nc                    r   r   r   r   r   r   r   rR      r   z6TestSetIndex.test_set_index_append.<locals>.<listcomp>r,   r   r   Tr   append)
rV   r   r	   r.   r   r   r   r   r/   r0   )r   r   r   r}   rN   r2   r1   r   r   r   test_set_index_append   s    z"TestSetIndex.test_set_index_appendc                 C   s\   |j dg|dd}t|tr|n|g}|j dg| |dd}|j ||dd}t|| d S )Nru   Tr   )r   rV   r   r/   r0   )r   r   r   r}   r   r2   r1   r   r   r   #test_set_index_append_to_multiindex   s
   z0TestSetIndex.test_set_index_append_to_multiindexc                 C   sd   t g dg dd}t dddgitddgd	d
}|j|jdd  }|d	}t|| d S )Nr]   r6   )valkeyr   r   r   r7   r8   r   r_   c                 S   s   | dkS )Nr   r   )indxr   r   r   <lambda>   s    z<TestSetIndex.test_set_index_after_mutation.<locals>.<lambda>)r   r   locr   mapr   r/   r0   )r   r   r2   r=   r1   r   r   r   test_set_index_after_mutation   s
   
z*TestSetIndex.test_set_index_after_mutationboxc                 C   s
   t | gS N)r   r)   r   r   r   r      s   
 zTestSetIndex.<lambda>c                 C      t | gS r   r	   r.   r   r   r   r   r          zappend, index_name)TNTrT   TtestFNc                 C   s   |}||j _||d }|tkr4d}tjt|d |j|||d W d    d S 1 s-w   Y  d S t|dd }	|	d u rEt|dd gn|	}
|j|||d}|jdgd|d}|r_|g|
 n|
|j _t	
|| d S )NrT   z%['one', 'two', 'three', 'one', 'two']rj   r   r-   r`   F)r   r`   r   ro   rp   KeyErrorr   getattrr-   r/   r0   )r   r   r   r   
index_namer   r   r   msgname_mir`   r1   r2   r   r   r    test_set_index_pass_single_array   s   "z-TestSetIndex.test_set_index_pass_single_arrayc                 C   r   r   r   r   r   r   r   r     r   )r   TrK   r   r   r   c                 C   s   |}||j _d||d g}d|tjtttfv rd ndg}|j|||d}	|jddgd|d}
|r8|
jdddn|
}
|rA|g| n||
j _	t
|	|
 d S )NrK   rT   r   Fr   r   )r   r`   rA   arrayr   r~   iterr   r   r-   r/   r0   )r   r   r   r   r   r   r   r}   r-   r1   r2   r   r   r   test_set_index_pass_arrays  s   z'TestSetIndex.test_set_index_pass_arraysbox2c                 C   r   r   r   r   r   r   r   r   3  r   c                 C      | j S r   r_   r   r   r   r   r   4      box1c                 C   r   r   r   r   r   r   r   r   ?  r   c                 C   r   r   r_   r   r   r   r   r   @  r   )r   r   r   r   c                 C   s   |}||j _||d ||d g}|j|||d}	||d ||d g}t|d trB|d dkrBt|d trB|d dkrBdn|}
|j|d g|
|d}|j|d g|dd}t|	| d S )NrK   r   r   r   FT)r   r`   r   rV   strr/   r0   )r   r   r   r   r   r   r   r   r}   r1   
first_dropr2   r   r   r   $test_set_index_pass_arrays_duplicate+  s"   z1TestSetIndex.test_set_index_pass_arrays_duplicater   c                 C   sV   |}t j|d |d gddgd}|j|||d}|jddgd|d}t|| d S )NrK   rT   r,   r   F)r	   r.   r   r/   r0   )r   r   r   r   r   r}   r1   r2   r   r   r   test_set_index_pass_multiindexg  s
   z+TestSetIndex.test_set_index_pass_multiindexc                 C   s   t d}d|_ttjdd|jd}|	d}t 
|j| ttjdd|d}|	d}t 
|j| | 	d}t 
|j| d S )Nr#   rT   r   rS   )r/   makeCategoricalIndexr`   r   rA   rB   rC   rD   rF   r   rn   r   rb   )r   cir   rW   r   r   r   (test_construction_with_categorical_indext  s   


z5TestSetIndex.test_construction_with_categorical_indexc              	   C   s   t g dg dttdtdddttdtdddd}d	d
dd	gdd
gd	d
gfD ]}|| }|j|jd}t|| q,d S )N)r   r   r   r   r   )r#            "   abaabbacF)
categoriesorderedT)rK   rT   C1C2r   r   rK   r*   )	r   r   r   r   rb   reindexr+   r/   r0   )r   r   colsr1   r   r   r   )test_set_index_preserve_categorical_dtype  s   z6TestSetIndex.test_set_index_preserve_categorical_dtypec                 C   s  t g dg dtdd}t|ddd|_|jd|_tg d	dd
}|dd}|jddd}t	
|jjd | t	
|jjd tddgdd
 |jjddgksZJ |dd}t	
|jjd tddgdd
 t	
|jjd | |jjddgksJ t tjdd}tg ddd}tg ddd}tdddd}|d }||}|j|dd}|j|dd}tg d	dd}tddgdd}t	
|jjd | t	
|jjd | t	
|jjd | t	
|jd| t	
|jd| t	
|jd| d S )N)r!   r!   r!   r7   r7   r7   )2011-07-19 07:00:002011-07-19 08:00:002011-07-19 09:00:00r   r   r   r   )r   r   valuer   T)utcrZ   )r   r   r   r_   rH   r   r   r   r   r!   r7   r   z
US/EasternrI   )2012-04-01 09:00r   r   2012-04-02 09:00r   r   z2011-01-01 09:00z
Asia/Tokyo)r[   rJ   r   r   )r   rU   r   popr   rL   r   tz_localizer   r/   rn   levelsr   r-   	swaplevelrA   rB   rC   r   rc   get_level_values)r   r   r2   idx1r{   idx3	expected1	expected2r   r   r   test_set_index_datetime  s`     

z$TestSetIndex.test_set_index_datetimec                 C   s  t tjdd}tdddd}||}tdddd}|||}td	dd
d}||}|j|dd}|j|dd}tdddd}tdddd}t|j	j
d | t|j	j
d | t|j	j
d | t|j	d| t|j	d| t|j	d| d S )Nr   r   z
2011-01-01r   Mr[   r\   z2013-01-01 09:00rY   2005rK   Tr   r   r   )r   rA   rB   rC   r   r   r   r/   rn   r   r   r   )r   r   r   r{   r   r   r   r   r   r   test_set_index_period  s"   

z"TestSetIndex.test_set_index_periodN)!__name__
__module____qualname__r    r3   r>   rG   rO   rX   rh   rs   r|   ro   markparametrizer   r   r   r   r
   r   rA   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    		 Rr   c                   @   s   e Zd Zdd Zejdddgejdddgdd Zejdddgejdddgejjd	egd
gddd Z	ejjd	e
eejedd gg ddejjdddgddgdejdddgejdddgdd ZdS )TestSetIndexInvalidc                 C   s   |}t jtdd |jddd W d    n1 sw   Y  t jtdd |j|d |d gdd W d    d S 1 s@w   Y  d S )NzIndex has duplicate keysrj   rK   T)verify_integrity)ro   rp   rq   r   )r   r   r   r   r   r   test_set_index_verify_integrity  s   "z3TestSetIndexInvalid.test_set_index_verify_integrityr   TFr   c                 C   s2  |}t jtdd |jg d||d W d    n1 sw   Y  t jtdd |j|d |d dg||d W d    n1 sDw   Y  d}t jt|d |jt|d ||d W d    n1 siw   Y  t jt|d |jd|d t|d g||d W d    d S 1 sw   Y  d S )	Nz['foo', 'bar', 'baz']rj   )r?   r@   bazr   XrK   rT   z%[('foo', 'foo', 'foo', 'bar', 'bar')])ro   rp   r   r   r~   )r   r   r   r   r   r   r   r   r   test_set_index_raise_keys  s    $"z-TestSetIndexInvalid.test_set_index_raise_keysr   set)idsc                 C   s   |}d}t jt|d |j||d ||d W d    n1 s"w   Y  t jt|d |jd|d ||d g||d W d    d S 1 sKw   Y  d S )N,The parameter "keys" may be a column key, .*rj   rK   r   )ro   rp   	TypeErrorr   )r   r   r   r   r   r   r   r   r   r   test_set_index_raise_on_type"  s   $"z0TestSetIndexInvalid.test_set_index_raise_on_typec                 C   r   r   r   r   r   r   r   r   4  r   zTestSetIndexInvalid.<lambda>)r
   r   znp.arrayr   r	   lengthr   r   	too_shorttoo_longc           	      C   s   |}t jddd|f}d}tjt|d |j||||d W d    n1 s,w   Y  tjt|d |jd|j||g||d W d    d S 1 sRw   Y  d S )Nr   r   r#   z<Length mismatch: Expected 5 rows, received array of length.*rj   r   rK   )	rA   rB   rC   integersro   rp   rq   r   rK   )	r   r   r   r   r   r   r   rF   r   r   r   r   test_set_index_raise_on_len2  s   "z/TestSetIndexInvalid.test_set_index_raise_on_lenN)r   r   r   r   ro   r   r   r   r   r   r
   r   rA   r   r   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	S )
TestSetIndexCustomLabelTypec           	      C   s  G dd d}|dd}|dd}t |ddg|d	d
gi}t |ddgitd	d
g|dd}||}t|| ||g}t|| |dd}d}tjt|d || W d    n1 sbw   Y  tjt|d ||g W d    d S 1 sw   Y  d S )Nc                   @   s(   e Zd ZdddZdefddZeZdS )zKTestSetIndexCustomLabelType.test_set_index_custom_label_type.<locals>.ThingreturnNc                 S      || _ || _d S r   r`   colorr   r`   r   r   r   r   __init__R     
zTTestSetIndexCustomLabelType.test_set_index_custom_label_type.<locals>.Thing.__init__c                 S      dt | j dS Nz<Thing >reprr`   r   r   r   r   __str__V     zSTestSetIndexCustomLabelType.test_set_index_custom_label_type.<locals>.Thing.__str__r   N)r   r   r   r   r   r   __repr__r   r   r   r   ThingQ  s    
r  OneredTwobluer   r   r   r   r_   ra   Threepinkz<Thing 'Three'>rj   )r   r   r   r/   r0   ro   rp   r   	r   r  thing1thing2r   r2   r1   thing3r   r   r   r    test_set_index_custom_label_typeN  s"   

 

"z<TestSetIndexCustomLabelType.test_set_index_custom_label_typec           	      C   s  G dd dt }|ddg}|ddg}t|ddg|d	d
gi}t|ddgitd	d
g|dd}||}t|| ||g}t|| |ddg}d}tjt|d || W d    n1 sfw   Y  tjt|d ||g W d    d S 1 sw   Y  d S )Nc                   @   s   e Zd ZdefddZdS )zXTestSetIndexCustomLabelType.test_set_index_custom_label_hashable_iterable.<locals>.Thingr   c                 S   s$   t | }dtt|}d| dS )Nz, zfrozenset({z}))sortedjoinr   r   )r   tmpjoined_reprsr   r   r   r  }  s   zaTestSetIndexCustomLabelType.test_set_index_custom_label_hashable_iterable.<locals>.Thing.__repr__N)r   r   r   r   r  r   r   r   r   r  {  s    r  r  r  r  r  r   r   r   r   r_   ra   r  r	  z frozenset\(\{'Three', 'pink'\}\)rj   )		frozensetr   r   r   r/   r0   ro   rp   r   r
  r   r   r   -test_set_index_custom_label_hashable_iterablet  s"    
"zITestSetIndexCustomLabelType.test_set_index_custom_label_hashable_iterablec                 C   s   G dd dt }|dd}|dd}tddgd	d
gg||gd}d}tjt|d || W d    n1 s9w   Y  tjt|d ||g W d    d S 1 sWw   Y  d S )Nc                   @   s$   e Zd ZdddZdefddZdS )zRTestSetIndexCustomLabelType.test_set_index_custom_label_type_raises.<locals>.Thingr   Nc                 S   r   r   r   r   r   r   r   r     r   z[TestSetIndexCustomLabelType.test_set_index_custom_label_type_raises.<locals>.Thing.__init__c                 S   r   r   r   r   r   r   r   r     r   zZTestSetIndexCustomLabelType.test_set_index_custom_label_type_raises.<locals>.Thing.__str__r  )r   r   r   r   r   r   r   r   r   r   r    s    
r  r  r  r  r  r   r   r   r   r*   r   rj   )r   r   ro   rp   r   r   )r   r  r  r  r   r   r   r   r   'test_set_index_custom_label_type_raises  s   

"zCTestSetIndexCustomLabelType.test_set_index_custom_label_type_raisesc                 C   sf   t tjdd}tdddd}tdddd}||}t|j| ||}t|j| d S )Nr   r   z
2011/01/01r   r   2013rK   )	r   rA   rB   rC   r   r   r/   rn   r   )r   r   r   r{   r   r   r   test_set_index_periodindex  s   

z6TestSetIndexCustomLabelType.test_set_index_periodindexN)r   r   r   r  r  r  r  r   r   r   r   r   M  s
    &'r   )__doc__r   r   numpyrA   ro   pandasr   r   r   r   r	   r
   r   r   r   pandas._testing_testingr/   r   r   r   r   r   r   r   <module>   s    ,   jK