U
    *ife                     @   sh   d dl Zd dlZd dlmZ d dlZd dlmZmZm	Z	m
Z
 d dlmZ G dd dZG dd dZdS )    N)np_percentile_argname)	DataFrameIndexSeries	Timestampc                
   @   s  e Zd Zejdeeej	
ddgeej	
ddgdeddgd	d
geeddddgddedgd	d
g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d%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 ZdS )7TestDataFrameQuantilezdf,expected            )r   r         ?      @      ?nameg        N      ?       @zSparse[float]dtypec                 C   s   |  }t|| d S N)quantiletmassert_series_equal)selfdfexpectedresult r   [/home/mars/bis/venv/lib/python3.8/site-packages/pandas/tests/frame/methods/test_quantile.pytest_quantile_sparse   s    z*TestDataFrameQuantile.test_quantile_sparsec           	   	   C   s   ddl m} |}|jddd}|d ||d dks8tt|j|j |jddd}|d	 ||jd	 d
ksrtt|j|j t	g g djddd}t
|d rt
|d stt	ddddgddddgd}|d}tjtdd | d}W 5 Q R X t|| t	dddgdddgddddgd}|jddd}tdddgdddgdd}t|| |jddgdd}t	ddgddgddgd ddgd}tj||d!d" t	dddgd#d$dgg}|jddd}td%d&gddgdd}t|| d S )'Nr   )
percentile皙?axisA
   ?r   z
2000-01-17Z   xyr)   r*   Br	   r
   r   )Zcol1Zcol2r   zSelect only validmatchr$   r+   indexr         @r   r0   r         ?g      ?g      @g      @r   r	   r
   T)Zcheck_index_typeab      @      @)numpyr    r   AssertionErrorr   Zassert_index_equalr0   columnslocr   npisnanZassert_produces_warningFutureWarningZmedianrenamer   r   assert_frame_equal)	r   datetime_framer    r   qrsZxpr   r   r   r   r   test_quantile*   s:     
$ z#TestDataFrameQuantile.test_quantilec                 C   sL   t jdddd}t|}t|}|jdd}tdgdd	d
}t|| d S )N
2016-01-01r
   
US/PacificperiodstzFnumeric_onlyz2016-01-02 00:00:00r   zdatetime64[ns, US/Pacific])r   r   )pd
date_ranger   r   r   r   r   )r   Zdtiserr   r   r   r   r   r   test_quantile_date_rangeU   s      z.TestDataFrameQuantile.test_quantile_date_rangec              	   C   s   t dddgdddgtjdddd	d
dgd}|jddd}tdddgdd}t|| d}tjt	|d |jdddd W 5 Q R X d S )Nr   r	   r
   r   r7   r8   Z20130101rI   ZfoobarZbaz)r$   r+   CDr   r"   r   r1   r   r   z>'<' not supported between instances of 'Timestamp' and 'float'r,   Fr#   rL   )
r   rM   rN   r   r   r   r   pytestraises	TypeErrorr   r   r   r   msgr   r   r   test_quantile_axis_mixedc   s    z.TestDataFrameQuantile.test_quantile_axis_mixedc              	   C   s  t dddgdddgddddgd}|jddd	}td
dgddgdd}t|| |jddd	}t|| |jddd	}tdddgdddgdd}t|| |jddd	}t|| d}tjt|d |jddd	 W 5 Q R X d}tjt|d |jddd	 W 5 Q R X d S )Nr   r	   r
   r   r.   r/   r   r   r"   r   r7   r$   r+   r2   r0   r   r1   r   r;   z*No axis named -1 for object type DataFramer,   r!   z.No axis named column for object type DataFramecolumn)r   r   r   r   r   rV   rW   
ValueErrorrY   r   r   r   test_quantile_axis_parameterw   s"    $z2TestDataFrameQuantile.test_quantile_axis_parameterc                 C   sV  t dddgdddgddddgd}|jdddd	}tdddgdddgdd
}t|| tjtdddgdddggdfdditdi}t|dddgddd}t|| t dddgdddgddddgd}|jdddd	}tdddgdddgdd
}t|| tjtdddgdddggdfdditdi}t|dddgddd}t|| |jddgddd	}t ddgddgddgdddgd}t	|| t g g d}|jdddd	}t
|d rt
|d stt dddgdddgdddggdddgd}|jd dgd!d"}t d#d#d#gdddggd dgdddgd$}t	|| d S )%Nr   r	   r
   r   r.   r/   r   Znearestr#   interpolationr2   r#   r   int64r0   r   r   r   r   r7   r8   float64r3   lowerr4   r(   r!   Zhigherr)   r*   r5   r6   cr;         ?Zmidpoint)ra   r   r0   r;   )r   r   r   r   r   r=   r    arrayr   rA   r>   r:   )r   r   r   r   exprC   r   r   r   test_quantile_interpolation   sZ    $$ $*z1TestDataFrameQuantile.test_quantile_interpolationc                 C   s4   |}|j dddd}|d t|d dks0td S )Nr!   r   linearr`   r$   r%   )r   r=   r    r:   )r   rB   r   rC   r   r   r   $test_quantile_interpolation_datetime   s    z:TestDataFrameQuantile.test_quantile_interpolation_datetimec                 C   sf   |}| d}|d t|d dks*t|j dddd}|d t|d dksVtt|| d S )Nr!   r$   r%   r   rm   r`   )r   r=   r    r:   r   r   )r   Z	int_framer   rC   q1r   r   r   test_quantile_interpolation_int   s    
z5TestDataFrameQuantile.test_quantile_interpolation_intc                 C   s   t dddgdddgdddggdddgd}|dd	g}t d
d
d
gdddggdd	gdddgd}t|| |jdd	gdd}t d
d
d
gdddggdd	gdddgd}t g g djddgdd}t tjtjgtjtjgdddgd}t|| d S )Nr   r	   r
   r5   r6   rf   rg   rh   r   r   r   ri   r"   r   r(   r!   r&   r/   )r   r   r   rA   r=   nanr   r   r   r   r   r   r   test_quantile_multi   s(    *   z)TestDataFrameQuantile.test_quantile_multic                 C   s  t tddgddgd}|d}tdgdgd	}|jdd
d}ttddgddgdd}t|| |jdgd
d}t tddggdgddgd}t|| tddg|d< |ddg jddd
d}ttdtdgddgdd}t|| |ddg jdgdd
d}t tdtdggdgddgd}t|| |ddg d}tg g t	j
dd}t|| |ddg dg}t dgd	}t|| d S )N20102011r      r5   r6   r   r1   r6   r/   FrK   z2010-07-02 12:00:00r5   r2   ri   2012rf   r   rU   z2011-07-02 12:00:00r0   r   r   )r   rM   to_datetimer   r   r   r   r   rA   r=   rd   rr   r   r   r   test_quantile_datetime   sL    

    z,TestDataFrameQuantile.test_quantile_datetimec              
   C   sF   d}ddddgddgfD ](}t jt|d || W 5 Q R X qd S )Nz2percentiles should all be in the interval \[0, 1\]r\   r	   r   r,   )rV   rW   r^   r   )r   rB   rZ   invalidr   r   r   test_quantile_invalid(  s    z+TestDataFrameQuantile.test_quantile_invalidc                 C   sr  t tdtdtdgtdddtdddtdddgtdtdtdgd	}|jd
dd}ttdtdddtdgd
dddgd}t|| |jd
gdd}t tdtdddtdggd
gdddgd}t|| t tdtj	tdtdgtdtdtj	tdgtdddtj	tdddtdddgtdddtdddtj	tdddgtdtdtdtj	gtj	tdtdtdgdt
dd}|jd
dd}ttdtdtdddtdddtdtdgd
t
dd}t|| |jd
gdd}t tdtdtdddtdddtdtdggd
gt
dd}t|| d S )Nz
2011-01-01z
2011-01-02z
2011-01-03z
US/Eastern)rJ   z1 daysz2 daysz3 days)r$   r+   rS   r   FrK   r$   r+   rS   )r   r0   ri   )r$   r5   r+   r6   rS   rf   ZAaBbCcrg   )r   r   rM   Z	Timedeltar   r   r   r   rA   NaTlistr   r   resrk   r   r   r   test_quantile_box.  s    



	






&*




z'TestDataFrameQuantile.test_quantile_boxc                 C   sp  t tddtddd}tj|jd< |d}tddgdd	gdd
}t|| |ddg}t ddgddgdddgd}t	|| |jddd}ttdddd}t|| |jddgdd}t tddgd ddgd}t	|| tj|d	< |d}tdtjgdd	gdd
}t|| |ddg}t ddgtjtjgdddgd}t	|| d S )Nr   g      @rw   )r\   r   r   r7   r1   r5   r6   r2   r3   r8   g      
@r/   r"   r   r   r	   )
r   r=   arangerq   ilocr   r   r   r   rA   r   r   r   r   test_quantile_nan  s*    


"z'TestDataFrameQuantile.test_quantile_nanc                 C   s  t dtjtjtjgi}|jddd}ttjgdgdd}t|| |jdgdd}t dtjgidgd}t|| t tdtdtd	gtjtjtjgd
}|jddd}ttdtjgddgdd}t|| |jdgdd}t tdtjggdgddgd}t|| d S )Nr5   r   FrK   r2   r/   z
2012-01-01z
2012-01-02z
2012-01-03rw   r6   ri   )	r   rM   r~   r   r   r   r   rA   r   r   r   r   r   test_quantile_nat  s2      z'TestDataFrameQuantile.test_quantile_natc                 C   s   t ddgdd}|d}ttjtjgddgdd}t|| |dg}t tjtjggddgdgd}t|| |jddd	}tg g ddd
}t|| |jdgdd	}t g dgd}t|| d S )Nr5   r6   rd   r;   r   r   r2   )r;   r0   r   r"   ry   )r   r   r   r=   rq   r   r   rA   r   r   r   r   "test_quantile_empty_no_rows_floats  s    
z8TestDataFrameQuantile.test_quantile_empty_no_rows_floatsc                 C   sD   t ddgdd}|d}ttjtjgddgdd}t|| d S )Nr5   r6   rb   r   r   r2   )r   r   r   r=   rq   r   r   r   r   r   r    test_quantile_empty_no_rows_ints  s    
z6TestDataFrameQuantile.test_quantile_empty_no_rows_intsc                 C   s   t ddgdd}|jddd}ttjtjgddgddd}t|| |d jd	|d< |jddd}|	t
}t|| |d jd	|d< |jddd}|	|d j}t|| d S )
Nr5   r6   datetime64[ns]r   r   FrK   ry   z
US/Central)r   r   r   rM   r~   r   r   dtZtz_localizeastypeobjectr   r   r   r   r    test_quantile_empty_no_rows_dt64
  s"    
   
z6TestDataFrameQuantile.test_quantile_empty_no_rows_dt64c                 C   s~   t tjddd}d|j_|d}tg g dtjd}d|j	_t
|| |dg}t g dgg d}d|j_t
|| d S )Nz1/1/18rv   rQ   zcaptain tightpantsr   rc   ri   )r   rM   rN   r;   r   r   r   r=   rd   r0   r   r   rA   rr   r   r   r   test_quantile_empty_no_columns   s    
z4TestDataFrameQuantile.test_quantile_empty_no_columnsc                 C   s   t tjdddddgd}|d d |d< |d }|sLt|jjdksLt|jd	d
 d|j	d< |j
d |d d ks|td S )Nr   r
   r$   r+   rS   rg   r	   rT   FrK   c   r   )r   r   )r   r=   randomZrandnlenZ_mgrblocksr:   r   valuesr   )r   using_array_managerr   rO   r   r   r   test_quantile_item_cache.  s    
z.TestDataFrameQuantile.test_quantile_item_cache)__name__
__module____qualname__rV   markparametrizer   r   rM   ZarraysZSparseArrayr   rE   rP   r[   r_   rl   rn   rp   rs   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r      sB   

+90{!#r   c                   @   s  e Zd Zejejeje	dej
jdddejddddejddd	d
ejdddejedddejedddgdd ddd Zejdd Zdd Zdd Zdd Zej
ddd Zd d! Zej
d"d#g g d$gd%g g d$gd#ejejgd&d'gd(gd%ejejgd&d'gd(ggd)d* Zej
d+ejd,g g d$d,ej
jd-ddd,ejejgd&d'gd(d,ggd.d/ Zej
d0ejejge	d1d$gg g d(ggd2d3 Zd4S )5TestQuantileExtensionDtyper%   z#raises when trying to add Intervalsreason)ZmarksrF   	   rT   )rI   freqrG   rH   z1 DayrQ   ZInt64r   ZFloat64c                 C   s
   t | jS r   )strr   )r)   r   r   r   <lambda>L      z#TestQuantileExtensionDtype.<lambda>)paramsZidsc                 C   s   |j }d|_|S Nr$   )paramr   )r   requestidxr   r   r   r0   @  s    z TestQuantileExtensionDtype.indexc                 C   s(   ||  }|tkrd|_ndg|_|S r   )copyr   r   r;   )r   r0   frame_or_seriesobjr   r   r   r   T  s
    zTestQuantileExtensionDtype.objc                 C   s(   t |tr||}n|j|dd}|S )NFrK   )
isinstancer   r   )r   r   qsr   r   r   r   compute_quantile_  s    
z+TestQuantileExtensionDtype.compute_quantilec                 C   s   t jt|t jd}t j| |j| }dddg}| ||}t|d |d |d g|j	|dd}t
||}t|| d S )	Nr   r   r   r   r   r\   r$   r   r0   r   )r=   r   r   intpr   shuffler   r   r   r   typer   assert_equalr   r   r0   indexerr   r   r   r   r   r   test_quantile_eaf  s    

   z+TestQuantileExtensionDtype.test_quantile_eac                 C   s   |j |jd< |j |jd< tjt|tjd}tj| |j| }dddg}| ||}t	|d |d |d g|j
|dd	}t||}t|| d S )
Nr   r\   r   r   r   r   r$   r   )	_na_valuer   r=   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   test_quantile_ea_with_nax  s    

   z3TestQuantileExtensionDtype.test_quantile_ea_with_naz-ignore:Using .astype to convert:FutureWarningc                 C   s   |r0|t kr0|jdkr0tjjdd}|j| |j|jd d < |	|j|d d < t
|j|jksjtt
jt|t
jd}t
j| |j| }dddg}| ||}	|jdddgd	|jd
}
t|
|dd}
t||
}
t|	|
 d S )Nzm8[ns]z9obj.astype fails bc obj is incorrectly dt64 at this pointr   r   r   r   r   r\   T)Z
allow_fillZ
fill_valuer$   r2   )r   r   rV   r   xfailnodeZ
add_markerr   r   r   r=   allZdtypesr:   r   r   r   r   r   r   Ztaker   r   r   r   )r   r   r0   r   r   r   r   r   r   r   r   r   r   r   test_quantile_ea_all_na  s,    

z2TestQuantileExtensionDtype.test_quantile_ea_all_nac                 C   s   t jt|t jd}t j| |j| }d}| ||}td|d i|j	dd}t
|trr|d }||ks~tnt|| 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   test_quantile_ea_scalar  s    

z2TestQuantileExtensionDtype.test_quantile_ea_scalarz*dtype, expected_data, expected_index, axisrd   r   rb   r5   r6   r   c                 C   sB   t ddg|d}|jd|d}t|dt|dd}t|| d S )Nr5   r6   r   r   r"   rd   r   r0   r   r   r   r   r   r   r   )r   r   expected_dataexpected_indexr#   r   r   r   r   r   r   test_empty_numeric  s       z-TestQuantileExtensionDtype.test_empty_numericz:dtype, expected_data, expected_index, axis, expected_dtyper   z	#GH 41544c           	      C   sD   t ddg|d}|jd|dd}t|dt||d}t|| d S )Nr5   r6   r   r   FrU   r   r   )	r   r   r   r   r#   Zexpected_dtyper   r   r   r   r   r   test_empty_datelike  s       z.TestQuantileExtensionDtype.test_empty_datelikez#expected_data, expected_index, axisr	   c                 C   sd   t tddgddgtddgd}|ddg jd	|d
}t|d	t|tjd}t	|| d S )Nrt   ru   r   rv   rx   )r5   r6   rf   r5   rf   r   r"   r   )
r   rM   rz   r   r   r   r=   rd   r   r   )r   r   r   r#   r   r   r   r   r   r   test_datelike_numeric_only  s    	   z5TestQuantileExtensionDtype.test_datelike_numeric_onlyN) r   r   r   rV   Zfixturer   rM   ZIntervalIndexZfrom_breaksranger   r   Zperiod_rangerN   Ztimedelta_rangerj   r=   r   r0   r   r   r   r   filterwarningsr   r   r   rq   r   r~   r   r   r   r   r   r   r   <  sh   




 

	
	
r   )r9   r=   rV   Zpandas.compat.numpyr   ZpandasrM   r   r   r   r   Zpandas._testingZ_testingr   r   r   r   r   r   r   <module>   s       0