o
    ҷhC                     @   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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(ejdg dYejdZe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$d\ed(gd]eeegeeegeeegeeegeeegee)egd]fg d8g d^dJeeegeeegdJfg d_g d`dJeeegeeegdJffdadb Z*ejdg dcddde Z+ejdfddgdhgd#d(gd&diggdjfdkdgdhgd#d(gd&diggdEgdlfgdmdn Z,ejdfdd[d#dEdoifdd[d#dEgifdd+d#gd[gd#gdEggdpfdkd+d#gd[gd#gdEggdEgdEgdqfdd[d#id[dEigfdd[d#id[dEidofgdrds Z-dtdu Z.dES )v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s8J |jdd|ksBJ 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 )N20130101AB      recordsorientr   namer   seriesr   )indexdatacolumnssplit)r
   r   to_dictr	   tmassert_dict_equal)
selftsmp	test_datatest_data_mixedexpected_recordsexpected_records_mixedexpected_seriesexpected_series_mixedexpected_splitexpected_split_mixed r)   Z/var/www/html/venv/lib/python3.10/site-packages/pandas/tests/frame/methods/test_to_dict.pytest_to_dict_timestamp   s:   z*TestDataFrameToDict.test_to_dict_timestampc                 C   sb   t ddgddgdddgd}d}tjt|d	 |jd
d W d    d S 1 s*w   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   sT   t dddgi}d}tjt|d |jdd W d    d S 1 s#w   Y  d S )Nr   r   r   z orient 'xinvalid' not understoodr0   xinvalidr   r2   r6   r)   r)   r*   test_to_dict_invalid_orientL   s
   "z/TestDataFrameToDict.test_to_dict_invalid_orientr   )dlrspsic                 C   sP   t dddgi}tjtdd |j|d W d    d S 1 s!w   Y  d S )Nr   r   r   znot understoodr0   r   r2   )r   r   r7   r)   r)   r*    test_to_dict_short_orient_raisesR   s   "z4TestDataFrameToDict.test_to_dict_short_orient_raisesmappingc                 C   sn  dddddddd}t |j|d	}| D ]\}}| D ]\}}||| | ks.J q qt |d
|}| D ]\}}| D ]\}}||| t|d  ksVJ qDq<t |d|}| D ]\}}| D ]\}}||| | kszJ qlqdt |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	sJ t
|dksJ t||	D ]
\}
}t|
| qt |d}| D ]\}}| D ]\}}||| | ksJ qqt |}||jd  |d< |d}| }||jd  |d< | D ]\}}| D ]\}}||| | ks1J q"qd S )Nr   r   )12rD   rE   3)rD   rE   rF   r   intolistr   r   r   r         ?       @)r   r   r   r      r   r   duped)r   r   itemsintnpnanr   r   
isinstancerI   lenzipr   copy)r   rC   r!   recons_datakvk2v2r'   r#   leftrightr7   	comp_datar)   r)   r*   test_to_dictY   s\   

z TestDataFrameToDict.test_to_dictc                 C   sd   t tjdd}dddg}tjt|d |j	|d W d    d S 1 s+w   Y  d S )Nr   )rL   rL   |z unsupported type: <class 'list'>z1to_dict\(\) only accepts initialized defaultdictsr0   rG   )
r   rP   randomdefault_rngstandard_normaljoinr3   r4   	TypeErrorr   )r   rC   r7   r8   r)   r)   r*   test_to_dict_errors   s   "z'TestDataFrameToDict.test_to_dict_errorsc                 C   sN   t g dgg dd}tt |  W d    d S 1 s w   Y  d S )Nr   r   rL   )r-   r-   r.   r   )r   r   assert_produces_warningUserWarningr   )r   r7   r)   r)   r*   test_to_dict_not_unique_warning   s   
"z3TestDataFrameToDict.test_to_dict_not_unique_warningzorient,item_getterdictc                 C      | | | S Nr)   r<   colidxr)   r)   r*   <lambda>       zTestDataFrameToDict.<lambda>r   c                 C      | | | S rm   r)   rn   r)   r)   r*   rq      rr   rI   c                 C   rl   rm   r)   rn   r)   r)   r*   rq      rr   r   c                 C   s   | d | | d  | S )Nr   r   r/   rn   r)   r)   r*   rq          r   c                 C   rs   rm   r)   rn   r)   r)   r*   rq      rr   c                 C   sR   t ddgddgd}|j|d}t||ddtsJ t||d	dts'J d S )
Nr   r   g?g?r,   r   r-   r   r.   )r   r   rR   rO   float)r   r   item_getterr7   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  r<   rg   r   r   z2017-11-18 21:53:00.219225+0000)tzz2017-11-18 22:06:30.061810+0000r   )	r   pytzutcr   rI   r   r
   r   r   )r   r   r7   rw   expectedr)   r)   r*   test_to_dict_tz   s   z#TestDataFrameToDict.test_to_dict_tzzinto, expectedr   rJ   int_col	float_colr   rK   rL         @)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 )	Nrf   rJ   rK   r   r   r   )r   rH   r   r   r   )r   r   	from_dictr   assert_frame_equal)r   rH   r   r7   rw   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s!J d S )Nc                 S   s   i | ]}t ||gqS r)   )str.0rA   r)   r)   r*   
<dictcomp>   s    zBTestDataFrameToDict.test_to_dict_numeric_names.<locals>.<dictcomp>   r   r   )r   rangesetr   keysr   r   r7   rw   r   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s!J d S )Nc                 S   s   i | ]
}d |d|gqS A_r<   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   	  rt   )r   r   r   r   r)   r)   r*   test_to_dict_wide  s   z%TestDataFrameToDict.test_to_dict_widez
data,dtype)TTFi  i  i  r   rf   )XYZc                    s6   t d|i}|jdd}t fdd|D sJ d S )Nr-   r   r   c                 3   s     | ]}t |d   u V  qdS )r-   N)type)r   recorddtyper)   r*   	<genexpr>"  s    z@TestDataFrameToDict.test_to_dict_orient_dtype.<locals>.<genexpr>)r   r   all)r   r   r   r7   r<   r)   r   r*   test_to_dict_orient_dtype  s   z-TestDataFrameToDict.test_to_dict_orient_dtypezdata,expected_dtypei皙?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   expected_dtyper7   r<   rw   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 )NrJ   g      "@r,   r   r   )r   r-   r.   )r   reset_indexr   r   r)   r)   r*    test_to_dict_mixed_numeric_frame6  s   z4TestDataFrameToDict.test_to_dict_mixed_numeric_frameNaabbccr   r,   )r-   cn1n2)namesr   xyz)r   r   )r   r   z1z2c                 C   sB   t jddgddgg||d}t j|jdddd}t|| d S )Nr   rL   r      )r   r   tightr   )r   from_recordsr   r   r   r   )r   r   r   r7   	roundtripr)   r)   r*   test_to_dict_orient_tight=  s   z-TestDataFrameToDict.test_to_dict_orient_tight)rk   rI   r   r   r   r   zdata,expected_typesr-   )r-   )r-   r.   r   r<   ef)r   g@ffffff
@)r   hellorL   )r   worldr   c           	         s   t |}|| |dkrdd   D }n9|dkr%dd   D }n+|dv r5 fdd d D }n|d	krCd
d t D }n|dkrPdd   D }|D ]\}}}||| | ksaJ t||| | u smJ qRd S )Nrk   c                 s   s0    | ]\}}|  D ]
\}}|||fV  q
qd S rm   rN   )r   keyindex_value_maprA   valuer)   r)   r*   r         zHTestDataFrameToDict.test_to_dict_returns_native_types.<locals>.<genexpr>rI   c                 s   s0    | ]\}}t |D ]
\}}|||fV  q
qd S rm   	enumerate)r   r   valuesrA   r   r)   r)   r*   r     r   >   r   r   c                 3   s<    | ]}t  d  D ]\}}|| d | | fV  q
qdS )r   r   Nr   )r   rA   jr   rw   r)   r*   r     s    
r   r   c                 s   0    | ]\}}|  D ]
\}}|||fV  q
qd S rm   r   r   rA   r   r   r   r)   r)   r*   r     r   c                 s   r   rm   r   r   r)   r)   r*   r     r   )r   r   rN   r   r   )	r   r   r   expected_typesr7   assertion_iteratorrA   r   r   r)   r   r*   !test_to_dict_returns_native_types[  s4   2

z5TestDataFrameToDict.test_to_dict_returns_native_types)rk   rI   r   r   r   c                 C   sd   t ddgddgdddgd}d	}tjt|d
 |j|dd W d    d S 1 s+w   Y  d S )Nr   r   rL   r   col1col2row1row2r/   z?'index=False' is only valid when 'orient' is 'split' or 'tight'r0   Fr   r   r2   )r   r   r7   r8   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   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   rL   r   r   r   r   r/   Fr   )r   r   r   r   r   r   r   r7   rw   r)   r)   r*   test_to_dict_index_false  s   z,TestDataFrameToDict.test_to_dict_index_false)r   r   )r   r   r   )r   r   r   index_namesr   c                 C   s0   t ddtgidd}|j|d}||ksJ d S )Nr-   r   Int64r   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u sJ t tdtgdddd}|jdd}t|d d	 tu s>J d S )
Nr   r   r   r   )r-   r   r   r   r   r-   )r   r	   r   r   rO   r   )r   r7   rw   r)   r)   r*   !test_to_dict_masked_native_python  s   z5TestDataFrameToDict.test_to_dict_masked_native_python)/__name__
__module____qualname__r+   r9   r;   r3   markparametrizerB   rk   r   rI   r   r^   re   rj   rx   r   r   r   r   boolr   r
   ru   rO   r   r   rP   uint64int64float64bool_
datetime64r   r   r   r   from_tuplesr   tupler   r   r   r   r   r)   r)   r)   r*   r      sP   -

;








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






	+(




r   )collectionsr   r   r   numpyrP   r3   r   pandasr   r   r   r   r	   r
   pandas._testing_testingr   r   r)   r)   r)   r*   <module>   s     