a
    PfH                  	   @   s   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mZmZmZ d dlmZ G dd dZejdedd	d	ed	ed
ed	dgdd ZdS )    )OrderedDictdefaultdict)datetimeN)	NA	DataFrameIndexInterval
MultiIndexPeriodSeries	Timedelta	Timestampc                   @   sT  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ejddddgddgdfddddddddfg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d0d/dd1d/d2feed3d-d.d/fd-dd0d/fddd1d/fgfee
ee
d-d.d/dd0d/dd1d/d2fgd4d5 Zd6d7 Zd8d9 Zejd:g d;efed<d-d-ed=dded>ddgegg d?egg d@egg dAegfdBdC ZejdDe dege!dEege"dFege#dGege$dHegfdIdJ Z%dKdL Z&ejd&dMe'dNdOge'dNdOgdPdQe()dRdSge(j)dRdSgdTdUgdVgejdWdXdYge'dXdYge'dXdYgdZdQe()d[d\ge(j)d[d\gd]d^gdVgd_d` Z*ejdg daejdbe!d-d-e!dge"d.d0e"d1ge"d.de!dge"d.dce!dge"d.dcge!dge"d.dde!dgdeeeegeeegeeegeeegeeegee+egdefg d@g dfdReeegeeegdRfg dgg dhdReeegeeegdRffdidj Z,ejdg dkdldm Z-ejdnd$dodpgd-dgddqggdrfdsdodpgd-dgddqggdMgdtfgdudv Z.ejdnddcd-dMdifddcd-dMgifd$d3d-gdcgd-gdMggdwfdsd3d-gdcgd-gdMggdMgdMgdxfd!dcd-idcdMigfd&dcd-idcdMidfgdydz Z/d{d| Z0d}d~ Z1dMS )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%   `/var/www/ai-form-bot/venv/lib/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_orientG   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.   r2   r%   r%   r&   test_to_dict_invalid_orientO   s    z/TestDataFrameToDict.test_to_dict_invalid_orientr   )dlrspsic                 C   sN   t dddgi}tjtdd |j|d W d    n1 s@0    Y  d S )Nr   r   r   znot understoodr,   r   r.   )r!   r   r3   r%   r%   r&    test_to_dict_short_orient_raisesU   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@J q@q0t |jd
|d	}| D ]6\}}| D ]$\}}||| t|d  ksJ qqzt |jd|d	}| D ].\}}| D ]\}}||| | ksJ qqt |jd|d	}ddgg dddgddgtjdggd}t|| t |jd|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 ]4\}}| D ] \}}||| | ksJ qܐqt |}||jd  |d< |d}| }||jd  |d< | D ]4\}}| D ] \}}||| | ksXJ qXqHd S )Nr   r   )12r?   r@   3)r?   r@   rA   r   intolistr   r   r   r         ?       @)r   r   r   r      r   r   Zduped)r   r   itemsintnpnanr   r    
isinstancerD   lenzipr   copy)r!   r>   r"   Zrecons_datakvZk2Zv2r$   r#   leftrightr3   Z	comp_datar%   r%   r&   test_to_dict\   sP     

z TestDataFrameToDict.test_to_dictc                 C   sb   t tjdd}dddg}tjt|d |j	|d W d    n1 sT0    Y  d S )Nr   )rG   rG   |z unsupported type: <class 'list'>z1to_dict\(\) only accepts initialized defaultdictsr,   rB   )
r   rJ   randomZdefault_rngZstandard_normaljoinr/   r0   	TypeErrorr   )r!   r>   r3   r4   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   rG   )r)   r)   r*   r   )r   r   assert_produces_warningUserWarningr   )r!   r3   r%   r%   r&   test_to_dict_not_unique_warning   s    z3TestDataFrameToDict.test_to_dict_not_unique_warningzignore::UserWarningzorient,expectedrD   r      rG      r   dict)r   r   c                 C   s6   t g dg dgg dd}||}||ks2J d S )NrZ   )   r_   r`   )r   r   r   r[   r   r   r!   r   expectedr3   resultr%   r%   r&   test_to_dict_not_unique   s    
z+TestDataFrameToDict.test_to_dict_not_uniquezorient,item_getterc                 C   s   | | | S Nr%   r7   colidxr%   r%   r&   <lambda>       zTestDataFrameToDict.<lambda>r   c                 C   s   | | | S rh   r%   ri   r%   r%   r&   rl      rm   c                 C   s   | | | S rh   r%   ri   r%   r%   r&   rl      rm   r   c                 C   s   | d | | d  | S )Nr   r   r+   ri   r%   r%   r&   rl      rm   r   c                 C   s   | | | S rh   r%   ri   r%   r%   r&   rl      rm   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   rL   rI   float)r!   r   Zitem_getterr3   rf   r%   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   r`      ir  r7   r[   r   r   z2017-11-18 21:53:00.219225+0000)tzz2017-11-18 22:06:30.061810+0000r   )	r   pytzutcr   rD   r   r   r   r    )r!   r   r3   rf   re   r%   r%   r&   test_to_dict_tz   s    z#TestDataFrameToDict.test_to_dict_tzzinto, expectedr   rE   int_col	float_colrF         @)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 )	NrZ   rE   rF   r~   r{   r   )r   rC   r|   r}   r   )r   r   	from_dictr   assert_frame_equal)r!   rC   re   r3   rf   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.0r<   r%   r%   r&   
<dictcomp>  rm   zBTestDataFrameToDict.test_to_dict_numeric_names.<locals>.<dictcomp>r_   r   r   )r   rangesetr   keysr   r!   r3   rf   re   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_r7   r%   r   r%   r%   r&   r     rm   z9TestDataFrameToDict.test_to_dict_wide.<locals>.<dictcomp>   r   r   c                 S   s   i | ]}d |d|qS r   r%   r   r%   r%   r&   r     rm   )r   r   r   r   r%   r%   r&   test_to_dict_wide  s    z%TestDataFrameToDict.test_to_dict_widez
data,dtype)TTFi  i    r   rZ   )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)r   recorddtyper%   r&   	<genexpr>4  rm   z@TestDataFrameToDict.test_to_dict_orient_dtype.<locals>.<genexpr>)r   r   all)r!   r   r   r3   r7   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   Zexpected_dtyper3   r7   rf   r%   r%   r&   ,test_to_dict_scalar_constructor_orient_dtype6  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 )NrE   g      "@r(   r   r   )r   r)   r*   )r   Zreset_indexr   r   r%   r%   r&    test_to_dict_mixed_numeric_frameH  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   rG   r   rb   )r   r   tightr   )r   Zfrom_recordsr   r   r   r   )r!   r   r   r3   Z	roundtripr%   r%   r&   test_to_dict_orient_tightO  s    z-TestDataFrameToDict.test_to_dict_orient_tight)ra   rD   r   r   r   r   zdata,expected_typesr)   )r)   )r)   r*   r   r7   ef)r   g@ffffff
@)r   ZhellorG   )r   Zworldr   c           	         s   t |}|| |dkr.dd   D }nr|dkrJdd   D }nV|dv rj fdd d D }n6|d	krd
d t D }n|dkrdd   D }|D ]6\}}}||| | ksJ t||| | u sJ qd S )Nra   c                 s   s.   | ]&\}}|  D ]\}}|||fV  qqd S rh   rH   )r   keyZindex_value_mapr<   valuer%   r%   r&   r     s   zHTestDataFrameToDict.test_to_dict_returns_native_types.<locals>.<genexpr>rD   c                 s   s.   | ]&\}}t |D ]\}}|||fV  qqd S rh   	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   rf   r%   r&   r     s   r   r   c                 s   s.   | ]&\}}|  D ]\}}|||fV  qqd S rh   r   r   r<   r   r   r   r%   r%   r&   r     s   c                 s   s.   | ]&\}}|  D ]\}}|||fV  qqd S rh   r   r   r%   r%   r&   r     s   )r   r   rH   r   r   )	r!   r   r   Zexpected_typesr3   Zassertion_iteratorr<   r   r   r%   r   r&   !test_to_dict_returns_native_typesm  s2    2

z5TestDataFrameToDict.test_to_dict_returns_native_types)ra   rD   r   r   r   c                 C   sb   t ddgddgdddgd}d	}tjt|d
 |j|dd W d    n1 sT0    Y  d S )Nr   r   rG   rb   col1col2row1row2r+   z?'index=False' is only valid when 'orient' is 'split' or 'tight'r,   Fr   r   r.   )r!   r   r3   r4   r%   r%   r&   test_to_dict_index_false_error  s    z2TestDataFrameToDict.test_to_dict_index_false_errorzorient, expectedr   r   rb   )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   rG   rb   r   r   r   r+   Fr   )r   r   r   r    rd   r%   r%   r&   test_to_dict_index_false  s    z,TestDataFrameToDict.test_to_dict_index_false)r   r   r   )r   r   r   Z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   rd   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s<J t tdtgdddd}|jdd}t|d d	 tsxJ d S )
Nr   r   r   r   )r)   r   r   r   r   r)   )r   r   r   rL   rI   r   )r!   r3   rf   r%   r%   r&   !test_to_dict_masked_native_python  s    z5TestDataFrameToDict.test_to_dict_masked_native_pythonc                 C   sR   t dg di}d}tjt|d |di  W d    n1 sD0    Y  d S )Nr)   rZ   zpStarting with pandas version 3.0 all arguments of to_dict except for the argument 'orient' will be keyword-only.r,   r   )r   r   r\   FutureWarningr   r2   r%   r%   r&   !test_to_dict_pos_args_deprecation  s
    z5TestDataFrameToDict.test_to_dict_pos_args_deprecation)2__name__
__module____qualname__r'   r5   r6   r/   markparametrizer=   ra   r   rD   r   rT   rY   r^   filterwarningsrg   ro   rz   r   r   r   boolr   r   rn   rI   r   r   rJ   Zuint64Zint64Zfloat64Zbool_Z
datetime64r   r   r   r	   from_tuplesr   tupler   r   r   r   r   r   r%   r%   r%   r&   r      s`  -

;









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






	+(





r   valr   r   Z2020r   c                 C   s4   t d| gi}|jdd}d| gi}||ks0J d S )Nr)   rD   r   rc   )r   r3   rf   re   r%   r%   r&   test_to_dict_list_pd_scalars  s    
r   )collectionsr   r   r   numpyrJ   r/   rx   Zpandasr   r   r   r   r	   r
   r   r   r   Zpandas._testingZ_testingr   r   r   r   r   r%   r%   r%   r&   <module>   s   ,   y"