o
    ŀg                  
   @   s*  d dl Zd dlZd dlZd dlmZ ej	dZ
edZd dlmZ dd ejD Zedd ejD 7 Zeejg dd	d
g7 Zejedd eD ddd Zdd Zdd Zdd Zdd Zdd Zdd Zejdd Zdd Zejded eed!ed"ggd#d$ Z d%d& Z!dS )'    Nz=ignore:Passing a BlockManager to DataFrame:DeprecationWarningpyarrow)pyarrow_array_to_numpy_and_maskc                 C      g | ]}t jg d |dqS ))         Ndtypepdarray.0r	    r   `/var/www/html/myenv/lib/python3.10/site-packages/pandas/tests/arrays/masked/test_arrow_compat.py
<listcomp>       r   c                 C   r   ))g?g?g333333?Nr   r
   r   r   r   r   r      r   )TFTNbooleanr   c                 C   s   g | ]}|j jqS r   )r	   name)r   ar   r   r   r      s    )paramsidsc                 C   s   | j S )zi
    Fixture returning parametrized array from given dtype, including integer,
    float and boolean
    )param)requestr   r   r   data   s   r   c                 C   s>   t | }t j| jtd dt | jjd}||sJ d S )N)na_valuetype)par   to_numpyobjectfrom_numpy_dtyper	   numpy_dtypeequals)r   arrexpectedr   r   r   test_arrow_array   s   
r&   c                 C   s`   t d| i}t|}|djt| jjksJ |	 }|d j| jks(J t
|| d S )Nr   )r   	DataFramer   tablefieldr   strr	   r"   	to_pandastmassert_frame_equalr   dfr(   resultr   r   r   test_arrow_roundtrip&   s   
r1   c            
      C   s   dd } t jg dt  d}t jg dt  d}t jg dt  d}t j|||gg d}|j| d}tj	g dd	d
}tj	g ddd
}tj	g ddd
}t
|||d}	t||	 d S )Nc                 S   s,   t j| r
t S t j| rt S d S )N)r   types
is_booleanr   BooleanDtype
is_integer
Int64Dtype)
arrow_typer   r   r   types_mapper1   s
   z<test_dataframe_from_arrow_types_mapper.<locals>.types_mapper)TNFr   )r   Nr   )r      )boolsints
small_ints)r8   r   r   Int64)r   r   bool_int64int8RecordBatchfrom_arraysr+   r   Seriesr'   r,   r-   )
r8   bools_array
ints_arraysmall_ints_arrayrecord_batchr0   r;   r<   r=   r%   r   r   r   &test_dataframe_from_arrow_types_mapper0   s   rI   c                 C   s   t d| dd i}t|}|djt| jjksJ tjtj	g |djdg|j
d}| }|d j| jks>J t|| d S )Nr   r   r   )schema)r   r'   r   r(   r)   r   r*   r	   r"   chunked_arrayrJ   r+   r,   r-   r.   r   r   r    test_arrow_load_from_zero_chunksE   s   
rL   c                  C   sB   t  } | tjg ddd}t jg ddd}t|| d S )N)r   r   r      Nr@   r   UInt32r   )r   UInt32Dtype__from_arrow__r   r   r,   assert_extension_array_equal)r	   r0   r%   r   r   r   test_arrow_from_arrow_uintS   s   rR   c                 C   s   t d| i}t|}|dd  }|jdd  jdd}t	|| |
| d }t|}|dd  }|jdd  jdd}t	|| d S )Nr   r   T)dropr   )r   r'   r   r(   slicer+   ilocreset_indexr,   r-   fillna)r   r/   r(   r0   r%   df2r   r   r   test_arrow_sliced^   s   

rY   c                 C   sR   t | }t|}tjg d|d}t jg d|d}t g d}||||fS )zt
    Fixture returning actual and expected dtype, pandas and numpy arrays and
    mask from a given numpy dtype
    )r   r   r   Nr   )r   r   r   r   )TTTF)npr	   r   r!   r   )any_real_numpy_dtypenp_dtypepa_typepa_arraynp_expectedmask_expectedr   r   r   np_dtype_to_arrayso   s   

ra   c                 C   s  | \}}}}t ||\}}t|dd | t|| | d }| d }| d  }	t|	d }
tjj|j	t
|||
g|jd}|  t ||\}}t|dd | t|| d|j	jd  }t||	 }td}tjj|j	t
|||g|jd d}|  t ||\}}t|dd | t|| tjg |d	}tjg tjd	}tjj|j	d||g|jd}|  t ||\}}t|dd | t|| dS )
a&  
    Test conversion from pyarrow array to numpy array.

    Modifies the pyarrow buffer to contain padding and offset, which are
    considered valid buffers by pyarrow.

    Also tests empty pyarrow arrays with non empty buffers.
    See https://github.com/pandas-dev/pandas/issues/40896
    Nr   r   r       )r   lengthbuffersoffset      r   )r   r,   assert_numpy_array_equalrd   
to_pybytesr   	py_bufferArrayfrom_buffersr   lenre   validate	bit_widthrZ   r   r?   )ra   r\   r^   r_   r`   r   maskmask_bufferdata_bufferdata_buffer_bytesdata_buffer_trailpa_array_trailre   data_buffer_offsetmask_buffer_offsetpa_array_offsetnp_expected_emptymask_expected_emptyr   r   r   $test_pyarrow_array_to_numpy_and_mask   sV   

r{   r$   
   rM      c                 C   s0   | j |}|  sJ t|dksJ d S )Nr|   )r	   rP   isnaallrm   )r   r$   resr   r   r   test_from_arrow_null   s   r   c                 C   sP   t | d}tjtd d | j| W d    d S 1 s!w   Y  d S )Nstring)match)r   r   castpytestraises	TypeErrorr	   rP   )r   r$   r   r   r   test_from_arrow_type_error   s   "r   )"numpyrZ   r   pandasr   pandas._testing_testingr,   markfilterwarnings
pytestmarkimportorskipr   %pandas.core.arrays.arrow._arrow_utilsr   ALL_INT_EA_DTYPESarraysFLOAT_EA_DTYPESr   fixturer   r&   r1   rI   rL   rR   rY   ra   r{   parametrizenullsrK   r   r   r   r   r   r   <module>   s8    

	

?$
