o
    Eh#                     @  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 )
	JSONDtypeZ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   Alib/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   Z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,   Z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   )r4   r5   mr   r   r   r6   w       z)JSONArray.__getitem__.<locals>.<listcomp>r%   c                      g | ]} j | qS r   r(   )r4   ir+   r   r   r6   z       )r$   tupler   numbersIntegralr(   slicer   r   
IndexErrorpdZapiZindexersZcheck_array_indexerr
   r%   r2   zip)r+   itemr   r?   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$   rB   rC   r(   r   r   Sequence	itertoolscyclenpZndarrayr%   	enumeraterG   )r+   keyvaluer>   kvr   r   r   __setitem__|   s   
zJSONArray.__setitem__intc                 C  s
   t | jS r1   )lenr(   r?   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   rX   rZ   r   r   r   __ne__   r]   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;   r0   )
warningswarnFutureWarningr   objectr	   listrN   asarrayr(   )r+   r%   r-   r   r   r   	__array__   s   zJSONArray.__array__c                 C  s   t | jS r1   )sys	getsizeofr(   r?   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   r3   r?   r   r   r6      r:   z"JSONArray.isna.<locals>.<listcomp>r;   )rN   arrayr(   rJ   r?   r   r?   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 )rl   r=   r4   loc
fill_valuer+   r   r   r6      s    z"JSONArray.take.<locals>.<listcomp>c                   r<   r   r=   rm   r?   r   r   r6      r@   r;   )	rN   re   r%   r   any
ValueErrorrE   r   r2   )r+   ZindexerZ
allow_fillrp   msgoutputerrr   ro   r   take   s.   



zJSONArray.takec                 C  s   t | | jd d  S r1   r   r(   r?   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   )StringDtypeFr0   c                 S     g | ]}t |qS r   dictr3   r   r   r   r6          z$JSONArray.astype.<locals>.<listcomp>r;   c                 S  ry   r   rz   r3   r   r   r   r6      r|   )Zpandas.core.arrays.string_rx   r   r$   r   r%   r-   r   r2   rN   re   rj   )r+   r%   r-   rx   Zarr_clsr   r   r   astype   s   
zJSONArray.astypec                 C  s"   t | dd dd | jD D S )Nc                 S  ry   r   rz   r3   r   r   r   r6      r|   z$JSONArray.unique.<locals>.<listcomp>c                 S  s   h | ]}t | qS r   rA   items)r4   dr   r   r   	<setcomp>   r:   z#JSONArray.unique.<locals>.<setcomp>rw   r?   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=   r3   r   r   r   	<genexpr>   s    z.JSONArray._concat_same_type.<locals>.<genexpr>)rd   rL   chainfrom_iterable)r   Z	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_argsortrV   Z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~   r3   r   r   r   r6      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     r8   zJSONArray._pad_or_backfill)NF)r   r#   )r   rU   )NN)FN)T)r   r   r   r   r%   Z__array_priority__r/   r"   r2   r7   rI   rT   rW   r\   r^   rf   propertyri   rk   rv   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   )choicerd   stringZascii_lettersintegersr4   _rngr   r   r6     s    z(make_data.<locals>.<listcomp>.<listcomp>r   
   )r   ranger   r   r   r   r   r6   	  s    
zmake_data.<locals>.<listcomp>r   )rN   ZrandomZdefault_rngr   r   r   r   r   	make_data  s   
r   )&__doc__Z
__future__r   collectionsr   r   rL   rB   r   rg   typingr   r   r`   ZnumpyrN   Zpandas.util._exceptionsr   Zpandas.core.dtypes.castr	   Zpandas.core.dtypes.commonr
   r   r   ZpandasrF   Zpandas.api.extensionsr   r   Zpandas.core.indexersr   Zcollections.abcr   Zpandas._typingr   r   r   r   r   r   r   r   <module>   s.     @