a
    GGboB                     @   s  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	 d dl
mZ ejdddgejdg ddd	 Zejd
eeje	egdd Zejjejd
eeje	eg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dd Zdd  Zd!d" Zd#d$ Zejjd%e d&d'd( Z!d)d* Z"ejdg dd+d, Z#d-d. Z$d/d0 Z%ejjddd1d2 Z&ejd3e'de'd gd4d5 Z(ejjddd6d7 Z)d8d9 Z*ejjddd:d; Z+d<d= Z,d>d? Z-d@dA Z.ejdg dBdCdD Z/ejdEdFgdFgfdFdFgdFgfdGdFgdGdFgfgdHdI Z0ejdJdKdddgfdKdLgddgfdKdMgddgfdNdOdPgddgfdNdMdLgddgfdQg dRddgfdNdLdOgdLdgfdNdOdMgddMgfdNdLdMgdLdMgfdNddMgddMgfg
dSdT Z1dUdV Z2dWdX Z3dYdZ Z4d[d\ Z5d]d^ Z6dS )_    N)CategoricalIndexIndexIntervalIndex
MultiIndexSeriescaseg      ?Zxxxmethod)intersectionunion
differencesymmetric_differencec                 C   sH   d}t jt|d" t| |||d W d    n1 s:0    Y  d S )Nz!Input must be Index or array-likematchsort)pytestraises	TypeErrorgetattr)idxr   r   r   msg r   Elib/python3.9/site-packages/pandas/tests/indexes/multi/test_setops.pytest_set_ops_error_cases   s    r   klassc                 C   s   | dd d }| d d }|t ur,||j}|j||d}|d u rL| }n|}t|| d}tjt|d" |jg d|d W d    n1 s0    Y  d S )N      r   .other must be a MultiIndex or a list of tuplesr      r      )	r   valuesr	   sort_valuestmassert_index_equalr   r   r   )r   r   r   firstsecondZ	intersectexpectedr   r   r   r   test_intersection_base   s    

r)   c                 C   s   | d d d }| d d }|t ur,||j}|j||d}|d u rL| }n|}t|| d}tjt|d" |jg d|d W d    n1 s0    Y  d S )Nr   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   test_union_base.   s    

r*   c                    s   | dd   | d d }| j  |d}|d u r6| }||sDJ t||  fddtjttfD }|D ]}| j ||d}t|| qnd}t	j
t|d" | j g d|d W d    n1 s0    Y  d S )N   r   c                    s   g | ]}| j qS r   r"   .0r   r'   r   r   
<listcomp>O       z(test_difference_base.<locals>.<listcomp>r   r   r   )r   r#   equalsr$   r%   nparrayr   listr   r   r   )r   r   answerresultcasesr   r   r   r/   r   test_difference_baseC   s    r9   c                    s   | dd  }| d d  | ddg }|j  |d}|d u rB| }t||  fddtjttfD }|D ]}|j ||d}t|| qld}tj	t
|d" |j g d	|d W d    n1 s0    Y  d S )
Nr    r   r   r   c                    s   g | ]}| j qS r   r,   r-   r/   r   r   r0   e   r1   z-test_symmetric_difference.<locals>.<listcomp>r   r   r   )r   r#   r$   r%   r3   r4   r   r5   r   r   r   )r   r   r&   r6   r7   r8   r   r   r   r/   r   test_symmetric_differenceY   s    r:   c                  C   s   t jddgddggddgd} tt | | A }W d    n1 sF0    Y  |j| jks`J |  ddg}tt | |A }W d    n1 s0    Y  |jd d gksJ d S )NabABnames)r   from_productr$   assert_produces_warningFutureWarningr@   copyrename)r   r7   idx2r   r   r   $test_multiindex_symmetric_differenceo   s    &&rG   c                 C   s    | j r
J | d d j sJ d S )Nr   )emptyr   r   r   r   
test_empty|   s    
rJ   c                 C   sv  | }|j | dd  |d}| d d j}|d u r8t|}tj|d| jd}t|tsXJ ||sfJ |j| jksvJ t	|| | j | |d}| d d }||sJ |j| jksJ | dd  j | |d}| d d }||sJ |j| jksJ | d d j | |d}| d d }||s,J |j| jks>J | dd  }ddg|_|j ||d}|jdksrJ | j | 
dd |d}t|dksJ |j |j|d}||d d sJ |j g |d}||sJ |j|jksJ |j d	g|d}tg d
}|j|_|j|jks,J d}tjt|d" |j g d|d W d    n1 sh0    Y  d S )Nr   r   )Z	sortorderr@   foobaz)NNr    rL   one))ZbarrO   )rM   two)rL   rP   )quxrO   )rQ   rP   r   r   )r    r   r!   r+   r   )r   r"   sortedr   from_tuplesr@   
isinstancer2   r$   r%   Z	sortlevellenr   r   r   )r   r   r&   r7   Zvalsr(   Zchunkletr   r   r   r   test_difference   sT    
rV   c                  C   s0   t ddgddgg} | g }t||  d S )Nr    r   r;   r<   r   rA   r   r$   r%   r   r7   r   r   r   test_difference_sort_special   s    
rY   zNot implemented.)reasonc                  C   sJ   t ddgddgg} | jg dd}t ddgddgg}t|| d S Nr    r   r;   r<   Tr   rW   )r   r7   r(   r   r   r   !test_difference_sort_special_true   s    r\   c                  C   s   t dtddgddgg} t dtddgdd	gg}d
}tjt|d | |}W d    n1 sn0    Y  t||  | j|dd}t||  d S )Nr    2000r   r;   r<   r!   r+   cdz0sort order is undefined for incomparable objectsr   Fr   )	r   rA   pd	Timestampr$   rB   RuntimeWarningr   r%   )r   otherr   r7   r   r   r   !test_difference_sort_incomparable   s    (rd   c                  C   s   t dtddgddgg} t dtddgdd	gg}d
}tjt|d | j|dd W d    n1 sr0    Y  d S )Nr    r]   r   r;   r<   r!   r+   r^   r_   zKThe 'sort' keyword only takes the values of None or False; True was passed.r   Tr   )r   rA   r`   ra   r   r   
ValueErrorr   )r   rc   r   r   r   r   &test_difference_sort_incomparable_true   s
    rf   c                 C   s   | d d d d d }| dd  }|j ||d}|d u rHt||   t|| sXJ | j | |d}t||  | j | d d |d}t||  | j}| d d j |dd  |d}|d u rt||  n|| sJ d S )Nr   r   r!   r   r   r+   )r
   r$   r%   r#   equalContentsr"   r2   )r   r   piece1piece2Z	the_uniontuplesr7   r   r   r   
test_union   s    rk   z9Length of names must match number of levels in MultiIndex)rZ   r   c                 C   s|   t g d}|| }d|v s"J d|v s.J d}tjt|d | |}W d    n1 s`0    Y  ||sxJ d S )N)r=   r>   CrN   r>   z'The values in the array are unorderabler   )r   r
   r$   rB   rb   r2   )r   rc   r7   r   Zresult2r   r   r   test_union_with_regular_index  s    
(rm   c           	      C   s   | d d d d d }| dd  }|j ||d}|d u rLt|| dd  t|| dd sdJ | j | |d}t||  | d d j | dd  |d}| d d }||sJ | j}|  |}|| sJ d S )Nr   r   r!   r   r   r   )r	   r$   r%   rg   r2   r"   )	r   r   rh   ri   Zthe_intrH   r(   rj   r7   r   r   r   test_intersection  s    
rn   c                 C   s   |   d}d g| j }t| |||d}t| || |d|}t|| t| ||d d |d}t| || d d |d|}t|| d S )Ncategoryr   r   )Zto_flat_indexZastypenlevelsr   rE   r$   r%   )r   r   r   rc   Z	res_namesr7   r(   r   r   r   test_setop_with_categorical3  s     rq   c                 C   s   t tddd}| j||d}t| jg g| j d d}tj||dd | jt	|d d |d}t| jg g| j | j
d}tj||dd d	}tjt|d
$ | jt	||d W d    n1 s0    Y  d S )Nr!   rL   )namer   )levelsZcodesr@   T)exactr   r   r   )r   ranger	   r   rs   rp   r$   r%   r3   Zasarrayr@   r   r   r   )r   r   rc   r7   r(   r   r   r   r   test_intersection_non_objectC  s    rv   c                  C   sF   t ddgddgg} t| j| dd|  t| j| d d|  d S Nr    r   r;   r<   Fr   r   rA   r$   r%   r	   rI   r   r   r   test_intersect_equal_sortU  s    ry   c                  C   sF   t ddgddgg} t ddgddgg}t| j| dd| d S r[   rx   )r   Zsorted_r   r   r   test_intersect_equal_sort_true\  s    rz   slice_c                 C   s\   t ddgddgg}||  }t||| t||| t|j|dd| d S rw   )r   rA   r$   r%   r
   )r{   r   rc   r   r   r   test_union_sort_other_emptyd  s
    r|   c                 C   sV   t ddgddgg}|d d }|j|dd}t ddgddgg}t|| d S r[   )r   rA   r
   r$   r%   )r{   r   rc   r7   r(   r   r   r    test_union_sort_other_empty_sortr  s
    r}   c                  C   s   t dtdgddgg} tt" | | d d }W d    n1 sN0    Y  t||  | j| d d dd}t||  d S )Nr    r]   r;   r<   Fr   )	r   rA   r`   ra   r$   rB   rb   r
   r%   rX   r   r   r   "test_union_sort_other_incomparable}  s    0r~   c                  C   sd   t dtdgddgg} tjtdd& | j| d d dd W d    n1 sV0    Y  d S )	Nr    r]   r;   r<   zCannot comparer   Tr   )r   rA   r`   ra   r   r   r   r
   rI   r   r   r   'test_union_sort_other_incomparable_sort  s    r   c                  C   s`   t ddgddgg} | jd }d}tjt|d | | W d    n1 sR0    Y  d S )Nr;   r<   r    r   z<Can only union MultiIndex with MultiIndex or Index of tuplesr   )r   rA   rs   r   r   NotImplementedErrorr
   )mir   r   r   r   r   "test_union_non_object_dtype_raises  s
    
r   c                  C   sZ   t g g} t jddgddggddgd}| |}t ddgddgg}t|| d S )Nr    r   r!   r+   r;   r<   r?   )r   from_arraysr
   r$   r%   )r   mi2r7   r(   r   r   r   %test_union_empty_self_different_names  s
    
r   c                  C   s`   t jddgddggddgd} td}| |}tj| |d	d
 || }tj| |d	d
 d S )Nr    r   r!   r+   r;   r<   r?   r   F)Zcheck_names)r   r   r`   Z
RangeIndexr
   r$   r%   )r   ZriZresult_leftZresult_rightr   r   r   &test_union_multiindex_empty_rangeindex  s    


r   )r
   r	   r   r   c                 C   sp   t ddgddgg}t ddgddgg}tjtdd" t|| |dd	 W d    n1 sb0    Y  d S )
Nr;   r<   r    r   r^   zThe 'sort' keyword only takesr   Tr   )r   rA   r   r   re   r   )r   Zidx1rF   r   r   r   test_setops_disallow_true  s    r   )rj   
exp_tuplesZval1Ztest1Zval2Ztest2c                 C   sT   t j| ddgd}t jg dddgd}||}t j|ddgd}t|| d S )Nr&   r'   r?   )r   r   r   )r   rS   r	   r$   r%   )rj   r   leftrightr7   r(   r   r   r   test_intersect_with_duplicates  s    
r   zdata, names, expected)r    r;   r<   )r    r   r^   r_   r   )r;   r<   r^   c                 C   s<   t jg ddgd}t j| g|d}||}||ks8J d S )Nr;   r<   r?   )r   rS   Z_maybe_match_names)datar@   r(   r   r   r7   r   r   r   test_maybe_match_names  s    
r   c                  C   st   t jddgddggddgd} t jddgddggddgd}| |}t jddgddggd dgd}t|| d S )	Nr    r   r!   r+   r^   r<   r?   r;   r   r   r	   r$   r%   )mi1r   r7   r(   r   r   r   'test_intersection_equal_different_names  s
    
r   c                  C   sF   t jdgdggddgd} t dgdgg}| |}t|| d S )Nr    r!   r^   r<   r?   r   )r   r   r7   r   r   r   !test_intersection_different_names  s    
r   c                 C   sb   t d| d| gg dg}t d| dgg dg}||}t d| gddgg}t|| d S )Nr!   r+   )r    r   r+   r   )r    r   r+         @r    r   r   )Znulls_fixturer   r   r7   r(   r   r   r   3test_intersection_with_missing_values_on_both_sides  s
    
r   c                  C   sL   t dtjgddgg} t dtjdgg dg}| |}t|| d S )Ng      ?r   r!   r   )r   r!   r+   )r   r   r3   nanr
   r$   r%   )r   r   r7   r   r   r   test_union_nan_got_duplicated  s    
r   c                 C   s   | j st| ttfrd S |  j }t|dgt	| g}t|d g| dgt	|d  g}|
|}t||  |
|}t||  d S )Nr    r   )rH   rT   r   r   uniquer"   tolistr   r   rU   r
   r$   r%   r#   )indexr"   r   r   r7   r   r   r   test_union_duplicates  s    &

r   )7Znumpyr3   r   Zpandasr`   r   r   r   r   r   Zpandas._testingZ_testingr$   ZmarkZparametrizer   r4   r5   r)   Zarm_slowr*   r9   r:   rG   rJ   rV   rY   Zxfailr\   rd   rf   rk   re   rm   rn   rq   rv   ry   rz   slicer|   r}   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   
B
	






	




	