o
    �¨Êh�   ã                   @  sä   d Z ddlmZ ddlmZmZ ddlZddlZddlZddl	Z	ddl
mZmZ ddlZddlmZ ddlmZmZmZ ddlZddlmZmZ dd	lmZ er\dd
lmZ ddlmZ G dd„ deƒZ G dd„ deƒZ!dd„ Z"dS )a¦  
Test extension array for storing nested data in a pandas container.

The JSONArray stores lists of dictionaries. The storage mechanism is a list,
not an ndarray.

Note
----
We currently store lists of UserDicts. Pandas has a few places
internally that specifically check for dicts, and does non-scalar things
in that case. We *want* the dictionaries to be treated as scalars, so we
hack around pandas by using UserDicts.
é    )Úannotations)ÚUserDictÚabcN)ÚTYPE_CHECKINGÚAny©Ú'construct_1d_object_array_from_listlike)Úis_bool_dtypeÚis_list_likeÚpandas_dtype)ÚExtensionArrayÚExtensionDtype)Úunpack_tuple_and_ellipses)ÚMapping)Útype_tc                   @  s4   e Zd ZU ejZdZeƒ Zde	d< e
d	dd„ƒZdS )
Ú	JSONDtypeÚjsonzMapping[str, Any]Úna_valueÚreturnútype_t[JSONArray]c                 C  ó   t S )zq
        Return the array type associated with this dtype.

        Returns
        -------
        type
        )Ú	JSONArray)Úcls© r   úS/var/www/html/env/lib/python3.10/site-packages/pandas/tests/extension/json/array.pyÚconstruct_array_type8   s   	zJSONDtype.construct_array_typeN)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   ÚtypeÚnamer   r   Ú__annotations__Úclassmethodr   r   r   r   r   r   3   s   
 r   c                      sæ   e Zd Zeƒ ZdZd0d1dd„Zedddœd	d
„ƒZedd„ ƒZ	dd„ Z
d1dd„Zd2dd„Zdd„ Zdd„ Zd3dd„Zed2dd„ƒZdd„ Zd4dd„Zd d!„ Zd5d#d$„Zd%d&„ Zed'd(„ ƒZd)d*„ Zd+d,„ Zdd"d-œ‡ fd.d/„
Z‡  ZS )6r   iè  NFr   ÚNonec                 C  sD   |D ]}t || jjƒstdt| jjƒ ƒ‚q|| _| j | _| _d S )NzAll values must be of type )Ú
isinstanceÚdtyper   Ú	TypeErrorÚstrÚdataÚ_itemsÚ_data)ÚselfÚvaluesr%   ÚcopyÚvalr   r   r   Ú__init__H   s   ÿzJSONArray.__init__©r%   r-   c                C  s   | |ƒS ©Nr   )r   Úscalarsr%   r-   r   r   r   Ú_from_sequenceU   s   zJSONArray._from_sequencec                 C  s   | dd„ |D ƒƒS )Nc                 S  s   g | ]
}|d krt |ƒ‘qS )r   )r   ©Ú.0Úxr   r   r   Ú
<listcomp>[   s    z.JSONArray._from_factorized.<locals>.<listcomp>r   )r   r,   Úoriginalr   r   r   Ú_from_factorizedY   ó   zJSONArray._from_factorizedc                   sÐ   t |tƒr	t|ƒ}t |tjƒrˆ j| S t |tƒr&|td ƒkr&tˆ ƒˆ jƒS t |tƒr4tˆ ƒˆ j| ƒS t|ƒs<t	dƒ‚t
jj ˆ |¡}t|jƒr[tˆ ƒjdd„ tˆ |ƒD ƒˆ jd�S tˆ ƒ‡ fdd„|D ƒƒS )Nzuonly integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indicesc                 S  s   g | ]\}}|r|‘qS r   r   )r5   r6   Úmr   r   r   r7   t   ó    z)JSONArray.__getitem__.<locals>.<listcomp>©r%   c                   ó   g | ]}ˆ j | ‘qS r   ©r(   )r5   Úi©r+   r   r   r7   w   ó    )r$   Útupler   ÚnumbersÚIntegralr(   Úslicer   r
   Ú
IndexErrorÚpdÚapiÚindexersÚcheck_array_indexerr	   r%   r3   Úzip)r+   Úitemr   rA   r   Ú__getitem__]   s$   


ÿ
ÿzJSONArray.__getitem__c                 C  sÄ   t |tjƒr|| j|< d S t |t| ƒtjfƒst |g¡}t |t	j
ƒrH|jdkrHtt||ƒƒD ]\}\}}|rEt || jjƒs@J ‚|| j|< q/d S t||ƒD ]\}}t || jjƒsZJ ‚|| j|< qMd S )NÚbool)r$   rD   rE   r(   r   r   ÚSequenceÚ	itertoolsÚcycleÚnpÚndarrayr%   Ú	enumeraterL   )r+   ÚkeyÚvaluer@   ÚkÚvr   r   r   Ú__setitem__y   s   
€ýþzJSONArray.__setitem__Úintc                 C  s
   t | jƒS r1   )Úlenr(   rA   r   r   r   Ú__len__Œ   s   
zJSONArray.__len__c                 C  r   r1   ©ÚNotImplemented©r+   Úotherr   r   r   Ú__eq__�   ó   zJSONArray.__eq__c                 C  r   r1   r^   r`   r   r   r   Ú__ne__’   rc   zJSONArray.__ne__c                 C  s0   |d u rt }|t krtt| ƒƒS tj| j|d�S )Nr=   )Úobjectr   ÚlistrS   Úasarrayr(   )r+   r%   r-   r   r   r   Ú	__array__•   s
   zJSONArray.__array__c                 C  s   t  | j¡S r1   )ÚsysÚ	getsizeofr(   rA   r   r   r   Únbytes�   s   zJSONArray.nbytesc                   s   t j‡ fdd„ˆ jD ƒtd�S )Nc                   s   g | ]}|ˆ j jk‘qS r   )r%   r   r4   rA   r   r   r7   ¢   r<   z"JSONArray.isna.<locals>.<listcomp>r=   )rS   Úarrayr(   rO   rA   r   rA   r   Úisna¡   s   zJSONArray.isnac              
     s¼   t  |¡}d}|r7ˆ d u rˆjj‰ |dk  ¡ rt‚z‡ ‡fdd„|D ƒ}W n. ty6 } zt|ƒ|‚d }~ww z‡fdd„|D ƒ}W n tyS } zt|ƒ|‚d }~ww tˆƒj|ˆjd�S )NzIIndex is out of bounds or cannot do a non-empty take from an empty array.éÿÿÿÿc                   s"   g | ]}|d krˆj | nˆ ‘qS )rn   r?   ©r5   Úloc©Ú
fill_valuer+   r   r   r7   µ   s    ÿz"JSONArray.take.<locals>.<listcomp>c                   r>   r   r?   ro   rA   r   r   r7   ¼   rB   r=   )	rS   rg   r%   r   ÚanyÚ
ValueErrorrG   r   r3   )r+   ÚindexerÚ
allow_fillrr   ÚmsgÚoutputÚerrr   rq   r   Útake¤   s.   
ÿ
ÿ
€ÿ
€ÿzJSONArray.takec                 C  s   t | ƒ| jd d … ƒS r1   ©r   r(   rA   r   r   r   r-   Â   s   zJSONArray.copyTc                 C  s    ddl m} t|ƒ}t|t| jƒƒr|| jkr|r|  ¡ S | S t||ƒr5|  t¡}| 	¡ }|j
||dd�S |sCtjdd„ | D ƒ|d�S tjdd„ | D ƒ||d�S )	Nr   )ÚStringDtypeFr0   c                 S  ó   g | ]}t |ƒ‘qS r   ©Údictr4   r   r   r   r7   Ö   ó    z$JSONArray.astype.<locals>.<listcomp>r=   c                 S  r}   r   r~   r4   r   r   r   r7   Ø   r€   )Úpandas.core.arrays.string_r|   r   r$   r   r%   r-   Úastyper'   r   r3   rS   rg   rl   )r+   r%   r-   r|   rW   Úarr_clsr   r   r   r‚   Å   s   

zJSONArray.astypec                 C  s"   t | ƒdd„ dd„ | jD ƒD ƒƒS )Nc                 S  r}   r   r~   r4   r   r   r   r7   Ý   r€   z$JSONArray.unique.<locals>.<listcomp>c                 S  s   h | ]}t | ¡ ƒ’qS r   ©rC   Úitems)r5   Údr   r   r   Ú	<setcomp>Ý   r<   z#JSONArray.unique.<locals>.<setcomp>r{   rA   r   r   r   ÚuniqueÚ   s   "zJSONArray.uniquec                 C  s"   t tj dd„ |D ƒ¡ƒ}| |ƒS )Nc                 s  s   � | ]}|j V  qd S r1   r?   r4   r   r   r   Ú	<genexpr>á   s   € z.JSONArray._concat_same_type.<locals>.<genexpr>)rf   rQ   ÚchainÚfrom_iterable)r   Ú	to_concatr(   r   r   r   Ú_concat_same_typeß   s   zJSONArray._concat_same_typec                 C  s$   |   ¡ }t|ƒdkr| ¡ }|dfS )Nr   r   )Ú_values_for_argsortr\   Úravel©r+   Úfrozenr   r   r   Ú_values_for_factorizeä   s   zJSONArray._values_for_factorizec                 C  s   dd„ | D ƒ}t |ƒS )Nc                 S  s   g | ]}t | ¡ ƒ‘qS r   r„   r4   r   r   r   r7   í   r<   z1JSONArray._values_for_argsort.<locals>.<listcomp>r   r�   r   r   r   rŽ   ë   s   zJSONArray._values_for_argsort)Úlimitr-   c                  s   t ƒ j|||d�S )N)Úmethodr“   r-   )ÚsuperÚ_pad_or_backfill)r+   r”   r“   r-   ©Ú	__class__r   r   r–   ð   r:   zJSONArray._pad_or_backfill)NF)r   r#   )r   r[   )NN)FN)T)r   r   r   r   r%   Ú__array_priority__r/   r"   r3   r9   rN   rZ   r]   rb   rd   rh   Úpropertyrk   rm   rz   r-   r‚   rˆ   r�   r’   rŽ   r–   Ú__classcell__r   r   r—   r   r   D   s4    






r   c                     s"   t j d¡‰ ‡ fdd„tdƒD ƒS )Né   c              	     s.   g | ]}t ‡ fd d„tˆ  dd¡ƒD ƒƒ‘qS )c                   s(   g | ]}ˆ   ttjƒ¡ˆ  d d¡f‘qS )r   éd   )Úchoicerf   ÚstringÚascii_lettersÚintegers©r5   Ú_©Úrngr   r   r7   ú   s    ÿÿz(make_data.<locals>.<listcomp>.<listcomp>r   é
   )r   Úranger¡   r¢   r¤   r   r   r7   ø   s    ú
þÿÿzmake_data.<locals>.<listcomp>r�   )rS   ÚrandomÚdefault_rngr§   r   r   r¤   r   Ú	make_dataõ   s   
ùrª   )#Ú__doc__Ú
__future__r   Úcollectionsr   r   rQ   rD   rŸ   ri   Útypingr   r   ÚnumpyrS   Úpandas.core.dtypes.castr   Úpandas.core.dtypes.commonr	   r
   r   ÚpandasrH   Úpandas.api.extensionsr   r   Úpandas.core.indexersr   Úcollections.abcr   Úpandas._typingr   r   r   rª   r   r   r   r   Ú<module>   s*     2