U
    MZfrC                     @   sr   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mZmZmZ d dlmZ G dd dZdS )    )OrderedDictdefaultdict)datetimeN)NA	DataFrameIndex
MultiIndex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$ejd!dJe%dKdLge%dKdLgdMdNe&'dOdPge&j'dOdPgdQdRgdSgejdTdUdVge%dUdVge%dUdVgdWdNe&'dXdYge&j'dXdYgdZd[gdSgd\d] Z(ejdddddd!d^gejd_ed(d(ed-ge d)d,e d.ge d)d+ed-ge d)d`ed-ge d)d`ged-ge d)daed-gdbeeegeeegeeegeeegeeegee)egdbfd(d+d-gdDdcddgdOeeegeeegdOfd(ded-gdDdfddgdOeeegeeegdOffdgdh Z*ejddddidd!gdjdk Z+ejdlddmdngd(d-gd+doggdpfd^dmdngd(d-gd+doggdJgdqfgdrds Z,ejdldd`d(dJdtifdd`d(dJgifdd0d(gd`gd(gdJggdufd^d0d(gd`gd(gdJggdJgdJgdvfdd`d(id`dJigfd!d`d(id`dJidtfgdwdx Z-dydz Z.dJS ){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-nbcvw55c/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_orientD   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,   r0   r#   r#   r$   test_to_dict_invalid_orientL   s    z/TestDataFrameToDict.test_to_dict_invalid_orientr   dlrspsic              	   C   s:   t dddgi}tjtdd |j|d W 5 Q R X d S )Nr   r   r   znot understoodr*   r   r,   )r   r   r1   r#   r#   r$    test_to_dict_short_orient_raisesR   s    z4TestDataFrameToDict.test_to_dict_short_orient_raises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   
isinstancerB   lenzipr   copy)r   r<   r    Zrecons_datakvZk2Zv2r"   r!   leftrightr1   Z	comp_datar#   r#   r$   test_to_dictY   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 )NrE   |z unsupported type: <class 'list'>z1to_dict\(\) only accepts initialized defaultdictsr*   r@   )	r   rH   randomZrandnjoinr-   r.   	TypeErrorr   )r   r<   r1   r2   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   rE   r'   r(   r   )r   r   Zassert_produces_warningUserWarningr   )r   r1   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#   r5   colidxr#   r#   r$   <lambda>       zTestDataFrameToDict.<lambda>r   c                 C   s   | | | S r\   r#   r]   r#   r#   r$   r`      ra   rB   c                 C   s   | | | S r\   r#   r]   r#   r#   r$   r`      ra   r   c                 C   s   | d | | d  | S )Nr   r   r)   r]   r#   r#   r$   r`      ra   r   c                 C   s   | | | S r\   r#   r]   r#   r#   r$   r`      ra   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   rJ   rG   r   float)r   r   Zitem_getterr1   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  r5   rX   r   r   z2017-11-18 21:53:00.219225+0000)tzz2017-11-18 22:06:30.061810+0000r   )	r   pytzutcr   rB   r   r
   r   r   )r   r   r1   rc   expectedr#   r#   r$   test_to_dict_tz   s    z#TestDataFrameToDict.test_to_dict_tzzinto, expectedr   rC   int_col	float_colr   rD   rE         @)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   rE   rC   rD   ru   rr   r   )r   rA   rs   rt   r   )r   r   	from_dictr   assert_frame_equal)r   rA   rp   r1   rc   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.0r:   r#   r#   r$   
<dictcomp>   s      zBTestDataFrameToDict.test_to_dict_numeric_names.<locals>.<dictcomp>   r   r   )r   rangesetr   keysr   r   r   r1   rc   rp   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_r5   r#   r{   r#   r#   r$   r}     s     
 z9TestDataFrameToDict.test_to_dict_wide.<locals>.<dictcomp>   r   r   c                 S   s   i | ]}d |d|qS r   r#   r{   r#   r#   r$   r}   	  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)r|   recorddtyper#   r$   	<genexpr>"  s     z@TestDataFrameToDict.test_to_dict_orient_dtype.<locals>.<genexpr>)r   r   allr   )r   r   r   r1   r5   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_dtyper1   r5   rc   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 )NrC   g      "@r&   r   r   )r   r'   r(   )r   Zreset_indexr   r   r   r#   r#   r$    test_to_dict_mixed_numeric_frame6  s    z4TestDataFrameToDict.test_to_dict_mixed_numeric_frameNZaaZbbccr   r&   )r'   cZn1Zn2)namesr   xyz)r   r   )r   r   Zz1Zz2c                 C   sB   t jddgddgg||d}t j|jdddd}t|| d S )Nr   rE   r      )r   r   tightr   )r   Zfrom_recordsrv   r   r   rw   )r   r   r   r1   Z	roundtripr#   r#   r$   test_to_dict_orient_tight=  s    z-TestDataFrameToDict.test_to_dict_orient_tightr   zdata,expected_typesr'   )r'   )r'   r(   r   r5   efg@gffffff
@ZhelloZworldc           	         s   t |}|| |dkr.dd   D }nr|dkrJdd   D }nV|dkrj fdd d D }n6|d	krd
d t D }n|dkrdd   D }|D ]6\}}}||| | kstt||| | kstqd S )Nr[   c                 s   s.   | ]&\}}|  D ]\}}|||fV  qqd S r\   rF   )r|   keyZindex_value_mapr:   valuer#   r#   r$   r     s   
 zHTestDataFrameToDict.test_to_dict_returns_native_types.<locals>.<genexpr>rB   c                 s   s.   | ]&\}}t |D ]\}}|||fV  qqd S r\   	enumerate)r|   r   valuesr:   r   r#   r#   r$   r     s   
 >   r   r   c                 3   s:   | ]2}t  d  D ] \}}|| d | | fV  qqdS )r   r   Nr   )r|   r:   jr   rc   r#   r$   r     s    r   r   c                 s   s.   | ]&\}}|  D ]\}}|||fV  qqd S r\   r   r|   r:   r   r   r   r#   r#   r$   r     s   
 c                 s   s.   | ]&\}}|  D ]\}}|||fV  qqd S r\   r   r   r#   r#   r$   r     s   
 )r   r   rF   r   r   r   )	r   r   r   Zexpected_typesr1   Zassertion_iteratorr:   r   r   r#   r   r$   !test_to_dict_returns_native_types[  s2    2

z5TestDataFrameToDict.test_to_dict_returns_native_typesr   c              	   C   sN   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   rE   r   col1col2row1row2r)   z?'index=False' is only valid when 'orient' is 'split' or 'tight'r*   Fr   r   r,   )r   r   r1   r2   r#   r#   r$   test_to_dict_index_false_error  s    z2TestDataFrameToDict.test_to_dict_index_false_errorzorient, expectedr   r   r   )r   r   )r   r   column_namesc                 C   s<   t ddgddgdddgd}|j|d	d
}t|| d S )Nr   r   rE   r   r   r   r   r)   Fr   )r   r   r   r   r   r   rp   r1   rc   r#   r#   r$   test_to_dict_index_false  s    z,TestDataFrameToDict.test_to_dict_index_false)r   r   )r   r   r   )r   r   r   Zindex_namesr   c                 C   s0   t ddtgidd}|j|d}||ks,td S )Nr'   r   Int64r   r   )r   r   r   r   r   r#   r#   r$   test_to_dict_na_to_none  s    z+TestDataFrameToDict.test_to_dict_na_to_nonec                 C   s   t tddgdddd}|jdd}t|d d	 tks>tt tdtgdddd}|jdd}t|d d	 tks|td S )
Nr   r   r   r   )r'   r   r   r   r   r'   )r   r	   r   r   rG   r   r   )r   r1   rc   r#   r#   r$   !test_to_dict_masked_native_python  s    z5TestDataFrameToDict.test_to_dict_masked_native_python)/__name__
__module____qualname__r%   r3   r4   r-   markZparametrizer;   r[   r   rB   r   rR   rW   rZ   rd   rq   ry   r   r   boolr   r
   rb   rG   rz   r   rH   Zuint64Zint64Zfloat64Zbool_Z
datetime64r   r   r   r   from_tuplesr   tupler   r   r   r   r   r#   r#   r#   r$   r      sN  -

;








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






	+(




r   )collectionsr   r   r   ZnumpyrH   r-   rn   Zpandasr   r   r   r   r	   r
   Zpandas._testingZ_testingr   r   r#   r#   r#   r$   <module>   s    