U
    f/e*                     @   sf   d dl mZmZ d dlmZ d dlZd dlZd dlZd dlm	Z	m
Z
mZ d dlmZ G dd dZdS )    )OrderedDictdefaultdict)datetimeN)	DataFrameSeries	Timestampc                   @   s<  e Zd Zdd Zdd Zdd Zejddd	d
dddgdd Z	ejde
eeegdd Zejdeeg gdd Zdd Zejdddd fddd fddd fdd d fd!d"d fgd#d$ Zd%d& Zejd'e
d(d)d*d+d,d*d-d.d*d/feed0d(d)d*fd(d+d,d*fd+d-d.d*fgfee
ee
d(d)d*d+d,d*d-d.d*d/fgd1d2 Zd3d4 Zd5d6 Zejd7d8d8d9gefed:d(d(ed;d+d+ed<d-d-gegd)d,d.gegd(d+d-gegd=d>d?gegfd@dA ZejdBed+egedCege dDege!d8ege"dEegfdFdG Z#dHdI Z$dJS )KTestDataFrameToDictc           
      C   s\  t d}t||g||gd}t||gddgd}||d||dg}|dd|ddg}|jdd|kspt|jdd|kstt||gddt||gd	dd}t||gddtddgd	dd}t|jd
d| t|jd
d| ddg||g||ggdd	gd}ddg|dg|dggdd	gd}	t|jdd| t|jdd|	 d S )NZ20130101AB      recordsorientr
   )namer   seriesr   )indexdatacolumnssplit)r   r   to_dictAssertionErrorr   tmassert_dict_equal)
selfZtsmp	test_dataZtest_data_mixedexpected_recordsZexpected_records_mixedZexpected_seriesZexpected_series_mixedexpected_splitZexpected_split_mixed r   K/tmp/pip-unpacked-wheel-tiezk1ph/pandas/tests/frame/methods/test_to_dict.pytest_to_dict_timestamp   s>    
 
 z*TestDataFrameToDict.test_to_dict_timestampc              	   C   sL   t ddgddgdddgd}d}tjt|d	 |jd
d W 5 Q R X d S )Nr   r   g      ?g      ?abr
   r   z1DataFrame index must be unique for orient='index'matchr   r   r   pytestraises
ValueErrorr   r   dfmsgr   r   r    /test_to_dict_index_not_unique_with_index_orientB   s    zCTestDataFrameToDict.test_to_dict_index_not_unique_with_index_orientc              	   C   s>   t dddgi}d}tjt|d |jdd W 5 Q R X d S )Nr
   r   r   z orient 'xinvalid' not understoodr&   Zxinvalidr   r(   r,   r   r   r    test_to_dict_invalid_orientJ   s    z/TestDataFrameToDict.test_to_dict_invalid_orientr   dlrspsic              	   C   s>   t dddgi}d}tjt|d |j|d W 5 Q R X d S )Nr
   r   r   z+Using short name for 'orient' is deprecatedr&   r   )r   r   assert_produces_warningFutureWarningr   )r   r   r-   r.   r   r   r    test_to_dict_short_orient_warnsP   s    z3TestDataFrameToDict.test_to_dict_short_orient_warnsmappingc                 C   s|  dddddddd}t |j|d	}| D ].\}}| D ]\}}||| | ks@tq@q0t |d
|}| D ]6\}}| D ]$\}}||| t|d  kstqqxt |d|}| D ].\}}| D ]\}}||| | kstqqt |d|}ddgdddgddgddgtjdggd}t|| t |d|}ddddddtjddg}	t	|t
s|tt|dkstt||	D ]\}
}t|
| qt |d}| D ]4\}}| D ] \}}||| | kstq֐qt |}||jd  |d< |d}| }||jd  |d< | D ]4\}}| D ] \}}||| | ksRtqRqBd S )Nr   r   )12r;   r<   3)r;   r<   r=   r	   intolistr   r   r
   r         ?       @)r   r   r   r      r   r   Zduped)r   r   itemsr   intnpnanr   r   
isinstancer@   lenzipr   copy)r   r:   r   Zrecons_datakvZk2Zv2r   r   leftrightr-   Z	comp_datar   r   r    test_to_dictX   sP     

z TestDataFrameToDict.test_to_dictc              	   C   sJ   t tjdd}dddg}tjt|d |j|d W 5 Q R X d S )NrC   |z unsupported type: <class 'list'>z1to_dict\(\) only accepts initialized defaultdictsr&   r>   )	r   rF   randomZrandnjoinr)   r*   	TypeErrorr   )r   r:   r-   r.   r   r   r    test_to_dict_errors   s    z'TestDataFrameToDict.test_to_dict_errorsc              	   C   s<   t dddggdddgd}tt |  W 5 Q R X d S )Nr   r   rC   r#   r$   r   )r   r   r7   UserWarningr   )r   r-   r   r   r    test_to_dict_not_unique_warning   s    z3TestDataFrameToDict.test_to_dict_not_unique_warningzorient,item_getterdictc                 C   s   | | | S Nr   r1   colidxr   r   r    <lambda>       zTestDataFrameToDict.<lambda>r   c                 C   s   | | | S rZ   r   r[   r   r   r    r^      r_   r@   c                 C   s   | | | S rZ   r   r[   r   r   r    r^      r_   r   c                 C   s   | d | | d  | S )Nr   r   r%   r[   r   r   r    r^      r_   r   c                 C   s   | | | S rZ   r   r[   r   r   r    r^      r_   c                 C   sR   t ddgddgd}|j|d}t||ddts8tt||d	dtsNtd S )
Nr   r   g?g?r"   r   r#   r   r$   )r   r   rH   rE   r   float)r   r   Zitem_getterr-   resultr   r   r    test_to_dict_box_scalars   s    z,TestDataFrameToDict.test_to_dict_box_scalarsc                 C   s   t dddddddtjdft dddd	d
ddtjdfg}tt|dgd}|jdd}dtdtjdidtdtjdig}t|d |d  t|d |d  d S )Ni           5   r   iYX )tzinfo         ir  r1   rV   r   r   z2017-11-18 21:53:00.219225+0000)tzz2017-11-18 22:06:30.061810+0000r   )	r   pytzutcr   r@   r   r   r   r   )r   r   r-   ra   expectedr   r   r    test_to_dict_tz   s    z#TestDataFrameToDict.test_to_dict_tzzinto, expectedr   rA   int_col	float_colr   rB   rC         @)r   r   r   r   c                 C   sd   t dddgdddgd}|jd|d	}d
dg}t j|dd| }t j|dd| }t|| d S )Nr   r   rC   rA   rB   rs   rp   r   )r   r?   rq   rr   r   )r   r   	from_dictr   Zassert_frame_equal)r   r?   rn   r-   ra   colsr   r   r    test_to_dict_index_dtypes   s    'z-TestDataFrameToDict.test_to_dict_index_dtypesc                 C   sF   t dd tdD }t|dd  }t|j}||ksBtd S )Nc                 S   s   i | ]}t ||gqS r   )str.0r6   r   r   r    
<dictcomp>   s      zBTestDataFrameToDict.test_to_dict_numeric_names.<locals>.<dictcomp>   r   r   )r   rangesetr   keysr   r   r   r-   ra   rn   r   r   r    test_to_dict_numeric_names   s    
z.TestDataFrameToDict.test_to_dict_numeric_namesc                 C   sF   t dd tdD }|dd }dd tdD }||ksBtd S )Nc                 S   s   i | ]}d |d|gqS ZA_r1   r   rx   r   r   r    rz     s     
 z9TestDataFrameToDict.test_to_dict_wide.<locals>.<dictcomp>   r   r   c                 S   s   i | ]}d |d|qS r   r   rx   r   r   r    rz     s     
 )r   r|   r   r   r   r   r   r    test_to_dict_wide  s    z%TestDataFrameToDict.test_to_dict_widez
data,dtypeTFi  i  i  XYZc                    s6   t d|i}|jdd}t fdd|D s2td S )Nr#   r   r   c                 3   s   | ]}t |d   kV  qdS )r#   N)type)ry   recorddtyper   r    	<genexpr>!  s     z@TestDataFrameToDict.test_to_dict_orient_dtype.<locals>.<genexpr>)r   r   allr   )r   r   r   r-   r1   r   r   r    test_to_dict_orient_dtype  s    z-TestDataFrameToDict.test_to_dict_orient_dtypezdata,expected_dtypeig?z
2005-02-25c                 C   s>   t d|idgd}|jdd}t|d d }||ks:td S )Nr#   r   r%   r   r   )r   r   r   r   )r   r   Zexpected_dtyper-   r1   ra   r   r   r    ,test_to_dict_scalar_constructor_orient_dtype#  s    z@TestDataFrameToDict.test_to_dict_scalar_constructor_orient_dtypec                 C   s>   t dgdgd}| d}ddddg}||ks:td S )NrA   g      "@r"   r   r   )r   r#   r$   )r   Zreset_indexr   r   r   r   r   r     test_to_dict_mixed_numeric_frame5  s    z4TestDataFrameToDict.test_to_dict_mixed_numeric_frameN)%__name__
__module____qualname__r!   r/   r0   r)   markZparametrizer9   rY   r   r@   r   rP   rU   rX   rb   ro   rv   r   r   boolr   r   r`   rE   rw   r   rF   Zuint64Zint64Zfloat64Zbool_Z
datetime64r   r   r   r   r   r    r      s   .

;








	"






r   )collectionsr   r   r   ZnumpyrF   r)   rl   Zpandasr   r   r   Zpandas._testingZ_testingr   r   r   r   r   r    <module>   s   