a
    Gb.                     @   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g d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d+d#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d,d- Zd.d/ Zd0d1 Zejd2g d3efed4d#d#ed5d&d&ed6d(d(gegg d7egg d8egg d9egfd:d; Zejd<ed&eged=ege d>ege!d?ege"d@egfdAdB Z#dCdD Z$ejddEe%dFdGge%dFdGgdHdIe&'dJdKge&j'dJdKgdLdMgdNgejdOdPdQge%dPdQge%dPdQgdRdIe&'dSdTge&j'dSdTgdUdVgdNgdWdX Z(dES )Y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spJ |jdd|ksJ 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_dictr   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!   Flib/python3.9/site-packages/pandas/tests/frame/methods/test_to_dict.pytest_to_dict_timestamp   s:    z*TestDataFrameToDict.test_to_dict_timestampc                 C   s`   t ddgddgdddgd}d}tjt|d	 |jd
d W d    n1 sR0    Y  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   sR   t dddgi}d}tjt|d |jdd W d    n1 sD0    Y  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rZspsic                 C   sR   t dddgi}d}tjt|d |j|d W d    n1 sD0    Y  d S )Nr   r   r   z+Using short name for 'orient' is deprecatedr(   r   )r   r   assert_produces_warningFutureWarningr   )r   r   r/   r0   r!   r!   r"   test_to_dict_short_orient_warnsR   s    z3TestDataFrameToDict.test_to_dict_short_orient_warnsmappingc                 C   sz  dddddddd}t |j|d	}| D ].\}}| D ]\}}||| | ks@J q@q0t |d
|}| D ]6\}}| D ]$\}}||| t|d  ksJ qqxt |d|}| D ].\}}| D ]\}}||| | ksJ qqt |d|}ddgg d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	szJ t
|dksJ t||	D ]\}
}t|
| qt |d}| D ]4\}}| D ] \}}||| | ksJ qԐqt |}||jd  |d< |d}| }||jd  |d< | D ]4\}}| D ] \}}||| | ksPJ qPq@d 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   itemsintnpnanr   r   
isinstancerA   lenzipr   copy)r   r;   r   Zrecons_datakvZk2Zv2r    r   leftrightr/   Z	comp_datar!   r!   r"   test_to_dictZ   sP     

z TestDataFrameToDict.test_to_dictc                 C   s^   t tjdd}dddg}tjt|d |j|d W d    n1 sP0    Y  d S )NrD   |z unsupported type: <class 'list'>z1to_dict\(\) only accepts initialized defaultdictsr(   r?   )	r   rG   ZrandomZrandnjoinr+   r,   	TypeErrorr   )r   r;   r/   r0   r!   r!   r"   test_to_dict_errors   s    z'TestDataFrameToDict.test_to_dict_errorsc                 C   sL   t g dgg dd}tt |  W d    n1 s>0    Y  d S )Nr   r   rD   )r%   r%   r&   r   )r   r   r8   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!   r3   colidxr!   r!   r"   <lambda>       zTestDataFrameToDict.<lambda>r   c                 C   s   | | | S r[   r!   r\   r!   r!   r"   r_      r`   rA   c                 C   s   | | | S r[   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 r[   r!   r\   r!   r!   r"   r_      r`   c                 C   sR   t ddgddgd}|j|d}t||ddts8J t||d	dtsNJ d S )
Nr   r   g?g?r$   r   r%   r   r&   )r   r   rI   rF   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 )Ztzinfo         ir  r3   rW   r   r   z2017-11-18 21:53:00.219225+0000)Ztzz2017-11-18 22:06:30.061810+0000r   )	r   pytzZutcr   rA   r   r	   r   r   )r   r   r/   rb   expectedr!   r!   r"   test_to_dict_tz   s    z#TestDataFrameToDict.test_to_dict_tzzinto, expectedr   rB   int_col	float_colr   rC   rD         @)r   r   r   r   c                 C   s`   t g dg dd}|jd|d}ddg}t j|dd| }t j|dd| }t|| d S )	NrV   rB   rC   rq   rn   r   )r   r@   ro   rp   r   )r   r   	from_dictr   assert_frame_equal)r   r@   rl   r/   rb   Z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sBJ d S )Nc                 S   s   i | ]}t ||gqS r!   )str.0r7   r!   r!   r"   
<dictcomp>  r`   zBTestDataFrameToDict.test_to_dict_numeric_names.<locals>.<dictcomp>   r   r   )r   rangesetr   keysr   r   r/   rb   rl   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sBJ d S )Nc                 S   s   i | ]}d |d|gqS ZA_r3   r!   rw   r!   r!   r"   ry     r`   z9TestDataFrameToDict.test_to_dict_wide.<locals>.<dictcomp>   r   r   c                 S   s   i | ]}d |d|qS r   r!   rw   r!   r!   r"   ry   
  r`   )r   r{   r   r~   r!   r!   r"   test_to_dict_wide  s    z%TestDataFrameToDict.test_to_dict_widez
data,dtype)TTFi  i  i  rr   rV   )XYZc                    s6   t d|i}|jdd}t fdd|D s2J d S )Nr%   r   r   c                 3   s   | ]}t |d   u V  qdS )r%   N)type)rx   recorddtyper!   r"   	<genexpr>#  r`   z@TestDataFrameToDict.test_to_dict_orient_dtype.<locals>.<genexpr>)r   r   all)r   r   r   r/   r3   r!   r   r"   test_to_dict_orient_dtype  s    z-TestDataFrameToDict.test_to_dict_orient_dtypezdata,expected_dtypeig?Tz
2005-02-25c                 C   s>   t d|idgd}|jdd}t|d d }||u s:J d S )Nr%   r   r'   r   r   )r   r   r   )r   r   Zexpected_dtyper/   r3   rb   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:J d S )NrB   g      "@r$   r   r   )r   r%   r&   )r   Zreset_indexr   r~   r!   r!   r"    test_to_dict_mixed_numeric_frame7  s    z4TestDataFrameToDict.test_to_dict_mixed_numeric_frameNZaaZbbZ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   rD   r      )r   r   Ztightr   )r   Zfrom_recordsrs   r   r   rt   )r   r   r   r/   Z	roundtripr!   r!   r"   test_to_dict_orient_tight>  s    z-TestDataFrameToDict.test_to_dict_orient_tight))__name__
__module____qualname__r#   r1   r2   r+   ZmarkZparametrizer:   rZ   r   rA   r   rQ   rU   rY   rc   rm   ru   r   r   boolr   r	   ra   rF   rv   r   rG   Zuint64Zint64Zfloat64Zbool_Z
datetime64r   r   r   r   Zfrom_tuplesr   r!   r!   r!   r"   r
      s   .

;








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





r
   )collectionsr   r   r   ZnumpyrG   r+   rk   Zpandasr   r   r   r   r	   Zpandas._testingZ_testingr   r
   r!   r!   r!   r"   <module>   s   