o
    Û­jƒ#  ã                   @  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Zddlm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rfd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)Úfind_stack_level©Ú'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   ú^/var/www/html/CropPilot/venv/lib/python3.10/site-packages/pandas/tests/extension/json/array.pyÚconstruct_array_type;   s   	zJSONDtype.construct_array_typeN)r   r   )Ú__name__Ú
__module__Ú__qualname__r   r   ÚtypeÚnamer   r   Ú__annotations__Úclassmethodr   r   r   r   r   r   6   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__K   s   ÿzJSONArray.__init__©r&   r.   c                C  s   | |ƒS ©Nr   )r   Úscalarsr&   r.   r   r   r   Ú_from_sequenceX   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_factorized\   ó   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   )r6   r7   Úmr   r   r   r8   w   ó    z)JSONArray.__getitem__.<locals>.<listcomp>©r&   c                   ó   g | ]}ˆ j | ‘qS r   ©r)   )r6   Úi©r,   r   r   r8   z   ó    )r%   Útupler   ÚnumbersÚIntegralr)   Úslicer    r   Ú
IndexErrorÚpdÚapiÚindexersÚcheck_array_indexerr
   r&   r4   Úzip)r,   Úitemr   rB   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%   rE   rF   r)   r    r   ÚSequenceÚ	itertoolsÚcycleÚnpÚndarrayr&   Ú	enumeraterM   )r,   ÚkeyÚvaluerA   ÚkÚvr   r   r   Ú__setitem__|   s   
€ýþzJSONArray.__setitem__Úintc                 C  s
   t | jƒS r2   )Úlenr)   rB   r   r   r   Ú__len__�   s   
zJSONArray.__len__c                 C  r   r2   ©ÚNotImplemented©r,   Úotherr   r   r   Ú__eq__’   ó   zJSONArray.__eq__c                 C  r   r2   r_   ra   r   r   r   Ú__ne__•   rd   zJSONArray.__ne__c                 C  sd   |du rt jdttƒ d� |d u rt}|tkrtt| ƒƒS |d u r)tj| j	|d�S tj| j	||d�S )NFaS  Starting with NumPy 2.0, the behavior of the 'copy' keyword has changed and passing 'copy=False' raises an error when returning a zero-copy NumPy array is not possible. pandas will follow this behavior starting with pandas 3.0.
This conversion to NumPy requires a copy, but 'copy=False' was passed. Consider using 'np.asarray(..)' instead.)Ú
stacklevelr>   r1   )
ÚwarningsÚwarnÚFutureWarningr   Úobjectr	   ÚlistrT   Úasarrayr)   )r,   r&   r.   r   r   r   Ú	__array__˜   s   øzJSONArray.__array__c                 C  s   t  | j¡S r2   )ÚsysÚ	getsizeofr)   rB   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   r5   rB   r   r   r8   ´   r=   z"JSONArray.isna.<locals>.<listcomp>r>   )rT   Úarrayr)   rP   rB   r   rB   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 )rs   r@   ©r6   Úloc©Ú
fill_valuer,   r   r   r8   Ç   s    ÿz"JSONArray.take.<locals>.<listcomp>c                   r?   r   r@   rt   rB   r   r   r8   Î   rC   r>   )	rT   rl   r&   r   ÚanyÚ
ValueErrorrH   r    r4   )r,   ÚindexerÚ
allow_fillrw   ÚmsgÚoutputÚerrr   rv   r   Útake¶   s.   
ÿ
ÿ
€ÿ
€ÿzJSONArray.takec                 C  s   t | ƒ| jd d … ƒS r2   ©r    r)   rB   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||ƒr0| ¡ }|j| |dd�S |s>t	j
dd„ | D ƒ|d�S t	jdd„ | D ƒ||d�S )	Nr   )ÚStringDtypeFr1   c                 S  ó   g | ]}t |ƒ‘qS r   ©Údictr5   r   r   r   r8   ç   ó    z$JSONArray.astype.<locals>.<listcomp>r>   c                 S  r‚   r   rƒ   r5   r   r   r   r8   é   r…   )Úpandas.core.arrays.string_r�   r   r%   r    r&   r.   r   r4   rT   rl   rq   )r,   r&   r.   r�   Úarr_clsr   r   r   Úastype×   s   
zJSONArray.astypec                 C  s"   t | ƒdd„ dd„ | jD ƒD ƒƒS )Nc                 S  r‚   r   rƒ   r5   r   r   r   r8   î   r…   z$JSONArray.unique.<locals>.<listcomp>c                 S  s   h | ]}t | ¡ ƒ’qS r   ©rD   Úitems)r6   Údr   r   r   Ú	<setcomp>î   r=   z#JSONArray.unique.<locals>.<setcomp>r€   rB   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 r2   r@   r5   r   r   r   Ú	<genexpr>ò   s   € z.JSONArray._concat_same_type.<locals>.<genexpr>)rk   rR   Ú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‰   r5   r   r   r   r8   þ   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__r0   r#   r4   r:   rO   r[   r^   rc   re   rm   Úpropertyrp   rr   r   r.   rˆ   r�   r’   r—   r“   r›   Ú__classcell__r   r   rœ   r   r   G   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   )Úchoicerk   ÚstringÚascii_lettersÚintegers©r6   Ú_©Úrngr   r   r8     s    ÿÿz(make_data.<locals>.<listcomp>.<listcomp>r   é
   )r   Úranger¦   r§   r©   r   r   r8   	  s    ú
þÿÿzmake_data.<locals>.<listcomp>r¢   )rT   ÚrandomÚdefault_rngr¬   r   r   r©   r   Ú	make_data  s   
ùr¯   )&Ú__doc__Ú
__future__r   Úcollectionsr   r   rR   rE   r¤   rn   Útypingr   r   rg   ÚnumpyrT   Úpandas.util._exceptionsr   Úpandas.core.dtypes.castr	   Úpandas.core.dtypes.commonr
   r   r   ÚpandasrI   Úpandas.api.extensionsr   r   Úpandas.core.indexersr   Úcollections.abcr   Úpandas._typingr   r   r   r¯   r   r   r   r   Ú<module>   s.     @