U
    /e,                     @   s  d dl Z d dlZd dlZd dlZd dlZd dlmZ	 d dl
mZ d dlmZmZ d dlmZ d dlmZmZ ejdejddddddd	d
gddeejddddddd	d
gd
d	ddgdeddd	d
d
d	ddgeeddgdd Zdd Zdd Zdd Zdd Ze j dd Z!edd Z"edd Z#d d! Z$d"d# Z%ejd$d%d%gd%d&ggejd'd(d)d*d+d,gd-d. Z&ejd$d%d&gd%d&ggejd'd+d,gd/d0 Z'd1d2 Z(d3d4 Z)d5d6 Z*dS )7    N)parse)PANDAS_VERSIONtm)_get_dummies_dtype_default)	assert_eq	make_metadata            categorydtype
categoriesabcdabcdabc                 C   s>   t | }t| d}t|}t|| t|j|j d S )Nr
   )pdget_dummiesddfrom_pandasr   r   assert_index_equalcolumns)r   expZddatares r   E/tmp/pip-unpacked-wheel-dbjnr7gq/dask/dataframe/tests/test_reshape.pytest_get_dummies   s
    


r    c                  C   sj   t dddddgdddddgd} tj| dd}|jddgd}t | d	}t|}t|| d S )
Ng        g      ?r   r	   Znpartitionsr   r   r   r   )r   	DataFramer   r   Z
categorizer   astyper   )dfddfZres_pZres_dr   r   r   !test_get_dummies_categories_order"   s    $
r'   c                  C   s   t t ddddddddgtdt tdd} t| d}t j| ddgd	}tj|ddgd	}t|| t	|j
|j
 tt t| W 5 Q R X tt t|j W 5 Q R X tt tj|d
gd	 W 5 Q R X d S )Nr	   r
   r   r   r   )r   r   cr   r(   r"   r   )r   r#   Categoricallistr   r   r   r   r   r   r   pytestraisesNotImplementedErrorr   r%   r&   r   r   r   r   r   test_get_dummies_object+   s"    
r/   c               	   C   s   t jddddddddgdd} t j| ddd	}t| d}tj|ddd	}t|| t j| d
d}tj|d
d}t|| t jddddtjdtjdgdd} t | }t| d}t|}t|| t j| d
d}tj|d
d}t|| d S )Nr	   r
   r   r   r   r   X-)prefixZ
prefix_sepT)Z
drop_first   )Zdummy_na)r   Seriesr   r   r   r   npnan)sr   dsr   r   r   r   test_get_dummies_kwargsE   s     

"


r9   c                    s    fdd}|S )Nc               	      sN   t jdd}    W 5 Q R X ttdkrBtdd | D sJtn| rJtd S )NTrecordz1.4.0c                 s   s   | ]}d t |jkV  qdS )z*In a future version, passing a SparseArrayN)strmessage).0rr   r   r   	<genexpr>h   s   zFcheck_pandas_issue_45618_warning.<locals>.decorator.<locals>.<genexpr>)warningscatch_warningsr   parse_versionallAssertionErrorr:   	test_funcr   r   	decoratord   s    z3check_pandas_issue_45618_warning.<locals>.decoratorr   )rG   rH   r   rF   r    check_pandas_issue_45618_warning_   s    rI   c                	   c   s.   t   t jdtdd d V  W 5 Q R X d S )Nignorez/`np.bool8` is a deprecated alias for `np.bool_`)r   r=   )rA   rB   filterwarningsDeprecationWarningr   r   r   r   ignore_numpy_bool8_deprecationr   s    
rM   c               	   C   s  t t jdddgdddgd} t| d}t j| dd}tj|dd}t  t|| W 5 Q R X | j	j
}|jtdkst|jtkstt jj|j	 stt j| jdd	dd}tj|jdd	dd}t  t|| W 5 Q R X t jj|j s
td S )
Nr   r   r(   r   r
   Tsparser   name)r   r4   r)   r   r   r   rM   r   computer   r   
fill_valuer   rE   subtypeapitypes	is_sparseZto_frameZa_a)r7   r8   r   r   r   r   r   r   test_get_dummies_sparse   s     rX   c               	   C   s   t t jdddgdddgddddgd} t| d}t j| d	d
}tj|d	d
}t  t|| W 5 Q R X | j	j
}|jtdkst|jtkstt jj|j	 std S )Nr   r   r(   r   r   r	   ABr
   TrN   )r   r#   r)   r   r   r   rM   r   rR   A_ar   rS   r   rE   rT   rU   rV   rW   )r%   r&   r   r   r   r   r   r   test_get_dummies_sparse_mix   s    r]   c                  C   s   t t jdddgdddgddddgd} t| d}t j| d	d
}tj|d	d
}t|| | jj	d	ksrt
ttj| d	d
| | jj	d	kst
d S )Nr   r   r(   r   r   r	   rY   r
   float64r   )r   r#   r)   r   r   r   r   rR   r\   r   rE   r.   r   r   r   test_get_dummies_dtype   s    
r_   c                  C   s   t t6 tddddddddg} t| d}t| W 5 Q R X tt	dt	dd}tj|dd}t
d	d	dt d
|_t t t| W 5 Q R X t t tj|ddgd W 5 Q R X t t t|j W 5 Q R X d S )Nr	   r
   r   r   ZabcbcZbcbcb)xyr!   r   Zparent_metar`   ra   r"   )r+   r,   r-   r   r4   r   r   r   r#   r*   r   _metar`   )r7   r8   r%   r&   r   r   r   test_get_dummies_errors   s      rd   valuesr[   DaggfuncZmeansumcountfirstlastc              
   C   s   t tjjtdddtjdt tjjtdddtjdd}t	|d
d}tj|dd	| |d
}t j|dd	| |d
}|dkr|tj}t|| |jdd	| |d
}|jdd	| |d
}|dkr|tj}t|| d S )NXYZd   sizeabcrZ   r[   Crf   r3   r      (   <   P   b   c   rZ   rr   indexr   re   rg   ri   )r   r#   r5   randomchoicer*   randnr)   r   r   repartitionpivot_tabler$   r^   r   re   rg   r%   r&   r   r   r   r   r   test_pivot_table   s$    


r   c              
   C   s   t tjjtdddtjdt tjjtdddtjjtdddd}t	|d
d}tj|dd	| |d
}t j|dd	| |d
}t|| |jdd	| |d
}|jdd	| |d
}t|| d S )Nrl   rm   rn   rp   rq   r3   rs   rZ   rr   rz   )r   r#   r5   r|   r}   r*   r~   r)   r   r   r   r   r   r   r   r   r   test_pivot_table_firstlast   s    

r   c                  C   s   t tdt tdddddgd} t| d}tj|dd	d
dd}t jdd	gd	d}t jt	j
gd |d}t|j| t j| dd	d
ddt	j
}t|| d S )NZAABBZABABr	   r
   r   r   rZ   r[   rr   rZ   r[   rr   ri   rz   rP   )r{   )r   r#   r*   r)   r   r   r   ZCategoricalIndexr4   r5   r^   r   Zassert_series_equalZdtypesr$   r   )r%   r&   r   Z	exp_indexr   r   r   r   test_pivot_table_dtype  s$         r   c                  C   sh   t t jddddt tddddgd} t| d}tj|d	d
ddd}|jj	t
	dksdtd S )Nz
2019-08-01r   Z1D)startZperiodsfreqrp   r	   r
   r   rZ   r[   rr   ri   rz   zdatetime64[ns])r   r#   Z
date_ranger)   r*   r   r   r   r{   r   r5   rE   )r%   r&   r   r   r   r   test_pivot_table_index_dtype   s    r   c               
   C   sx  t tjjtdddtjdt tjjtdddd} t	| d}d}t
t}tj|dgdd	d
 W 5 Q R X |t|jkstd}t
t}tj|ddgd	d
 W 5 Q R X |t|jkstd}t
t}tj|ddd	ggd
 W 5 Q R X |t|jkstd}t
t}tj|ddd	dgd W 5 Q R X |t|jksTtt
t}tj|ddd	dd W 5 Q R X |t|jksttttddt  d|_d}t
t}tj|ddd	gd
 W 5 Q R X |t|jkstt tjjtdddtjdtjjtdddd} t	| d}d}t
t}tj|ddd	d
 W 5 Q R X |t|jksttd S )Nrp   
   rn   r   r
   z.'index' must be the name of an existing columnrZ   rr   r[   )r{   r   re   z0'columns' must be the name of an existing columnz4'values' must refer to an existing column or columnsz>aggfunc must be either 'mean', 'sum', 'count', 'first', 'last'rh   rz   Zxxr   rb   z$'columns' must have known categoriesz 'columns' must be category dtype)r   r#   r5   r|   r}   r*   r~   r)   r   r   r+   r,   
ValueErrorr   r<   valuerE   r   objectfloatrc   )r%   r&   msgerrr   r   r   test_pivot_table_errors.  sZ    
  
 
r   )+
contextlibrA   Znumpyr5   Zpandasr   r+   Zpackaging.versionr   rC   Zdask.dataframeZ	dataframer   Zdask.dataframe._compatr   r   Zdask.dataframe.reshaper   Zdask.dataframe.utilsr   r   markZparametrizer4   r)   r#   r*   r    r'   r/   r9   rI   contextmanagerrM   rX   r]   r_   rd   r   r   r   r   r   r   r   r   r   <module>   sP   *"

		


