U
    *if.                     @   sn   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 d dlmZ G dd dZdS )    )OrderedDictdefaultdict)datetimeN)	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(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"   Z/home/mars/bis/venv/lib/python3.8/site-packages/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+   r/   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}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   r0   r1   r"   r"   r#   test_to_dict_short_orient_warnsR   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   
isinstancerC   lenzipr   copy)r   r=   r   Zrecons_datakvZk2Zv2r!   r    leftrightr0   Z	comp_datar"   r"   r#   test_to_dictZ   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 )NrF   |z unsupported type: <class 'list'>z1to_dict\(\) only accepts initialized defaultdictsr)   rA   )	r   rI   randomZrandnjoinr,   r-   	TypeErrorr   )r   r=   r0   r1   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   rF   r&   r'   r   )r   r   r:   UserWarningr   )r   r0   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"   r4   colidxr"   r"   r#   <lambda>       zTestDataFrameToDict.<lambda>r   c                 C   s   | | | S r]   r"   r^   r"   r"   r#   ra      rb   rC   c                 C   s   | | | S r]   r"   r^   r"   r"   r#   ra      rb   r   c                 C   s   | d | | d  | S )Nr   r   r(   r^   r"   r"   r#   ra      rb   r   c                 C   s   | | | S r]   r"   r^   r"   r"   r#   ra      rb   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   rK   rH   r   float)r   r   Zitem_getterr0   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  r4   rY   r   r   z2017-11-18 21:53:00.219225+0000)tzz2017-11-18 22:06:30.061810+0000r   )	r   pytzutcr   rC   r   r	   r   r   )r   r   r0   rd   expectedr"   r"   r#   test_to_dict_tz   s    z#TestDataFrameToDict.test_to_dict_tzzinto, expectedr   rD   int_col	float_colr   rE   rF         @)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   rF   rD   rE   rv   rs   r   )r   rB   rt   ru   r   )r   r   	from_dictr   assert_frame_equal)r   rB   rq   r0   rd   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.0r9   r"   r"   r#   
<dictcomp>  s      zBTestDataFrameToDict.test_to_dict_numeric_names.<locals>.<dictcomp>   r   r   )r   rangesetr   keysr   r   r   r0   rd   rq   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_r4   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   r0   r4   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_dtyper0   r4   rd   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 )NrD   g      "@r%   r   r   )r   r&   r'   )r   Zreset_indexr   r   r   r"   r"   r#    test_to_dict_mixed_numeric_frame7  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   rF   r      )r   r   Ztightr   )r   Zfrom_recordsrw   r   r   rx   )r   r   r   r0   Z	roundtripr"   r"   r#   test_to_dict_orient_tight>  s    z-TestDataFrameToDict.test_to_dict_orient_tight))__name__
__module____qualname__r$   r2   r3   r,   markZparametrizer<   r\   r   rC   r   rS   rX   r[   re   rr   rz   r   r   boolr   r	   rc   rH   r{   r   rI   Zuint64Zint64Zfloat64Zbool_Z
datetime64r   r   r   r   from_tuplesr   r"   r"   r"   r#   r
      s   .

;








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





r
   )collectionsr   r   r   numpyrI   r,   ro   Zpandasr   r   r   r   r	   Zpandas._testingZ_testingr   r
   r"   r"   r"   r#   <module>   s   