
    `Vg(5                       d dl mZ d dlmZ d dlZd dlmZ d dlm	Z	 d dl
mZ d dlZd dlmZmZ d dlmZ d d	lmZmZmZmZ d d
lmZmZmZmZ ej:                  ej<                  ej>                  ej@                  ejB                  ejD                  ejD                  dZ#ej>                  ejH                  dfejD                  ejJ                  e	fej:                  ejL                  dfej<                  ejL                  dfej@                  ejL                  dfejN                  ejJ                  dfejB                  ejP                  d fiZ)ejL                  dejH                  dejJ                  diZ* G d de      Z+y)    )annotations)AnyN)infer_dtype)iNaT)cache_readonly)is_categorical_dtypeis_string_dtype)PandasBuffer)ColumnColumnBuffersColumnNullType	DtypeKind)ArrowCTypes
EndiannessNoBufferPresentdtype_to_arrow_c_fmt)iufbUMmzThis column is non-nullablezThis column uses NaN as nullz!This column uses a sentinel valuec                      e Zd ZdZdddZddZedd       Zedd       Z	ddZ
ed        Zed        Zedd	       Zedd
       ZddZdddZddZ	 	 ddZddZddZy)PandasColumna  
    A column object, with only the methods and properties required by the
    interchange protocol defined.
    A column can contain one or more chunks. Each chunk can contain up to three
    buffers - a data buffer, a mask buffer (depending on null representation),
    and an offsets buffer (if variable-size binary; e.g., variable-length
    strings).
    Note: this Column object can only be produced by ``__dataframe__``, so
          doesn't need its own version or ``__column__`` protocol.
    c                    t        |t        j                        st        dt	        |       d      || _        || _        y)zu
        Note: doesn't deal with extension arrays yet, just assume a regular
        Series/ndarray for now.
        zColumns of type  not handled yetN)
isinstancepdSeriesNotImplementedErrortype_col_allow_copy)selfcolumn
allow_copys      _/var/www/html/hubwallet-dev/venv/lib/python3.12/site-packages/pandas/core/interchange/column.py__init__zPandasColumn.__init__H   s=    
 &")),%(8fFV&WXX 	%    c                .    | j                   j                  S )z2
        Size of the column, in elements.
        )r$   sizer&   s    r)   r-   zPandasColumn.sizeT   s     yy~~r+   c                     y)z7
        Offset of first element. Always zero.
        r    r.   s    r)   offsetzPandasColumn.offsetZ   s     r+   c                   | j                   j                  }t        |      rb| j                   j                  j                  }| j                  |j                        \  }}}}t        j                  ||t        j                  fS t        |      rNt        | j                         dk(  r+t        j                  dt        |      t        j                  fS t        d      | j                  |      S )Nstring   z.Non-string object dtypes are not supported yet)r$   dtyper   valuescodes_dtype_from_pandasdtyper   CATEGORICALr   NATIVEr	   r   STRINGr   r"   )r&   r5   r7   _bitwidthc_arrow_dtype_f_strs         r)   r5   zPandasColumn.dtypeb   s    		&II$$**E ,,U[[9# %%#!!	  U#499%1$$(/%%	  &&VWW//66r+   c                    t         j                  |j                  d      }|t        d| d      ||j                  dz  t        |      |j                  fS )z/
        See `self.dtype` for details.
        N
Data type z& not supported by interchange protocolr4   )	_NP_KINDSgetkind
ValueErroritemsizer   	byteorder)r&   r5   rC   s      r)   r8   z$PandasColumn._dtype_from_pandasdtype   sT     }}UZZ.<z%0VWXXU^^a')=e)DeooUUr+   c                   | j                   d   t        j                  k(  st        d      | j                  j
                  j                  dt        t        j                  | j                  j
                  j                              dS )a:  
        If the dtype is categorical, there are two options:
        - There are only values in the data buffer.
        - There is a separate non-categorical Column encoding for categorical values.

        Raises TypeError if the dtype is not categorical

        Content of returned dict:
            - "is_ordered" : bool, whether the ordering of dictionary indices is
                             semantically meaningful.
            - "is_dictionary" : bool, whether a dictionary-style mapping of
                                categorical values to other objects exists
            - "categories" : Column representing the (implicit) mapping of indices to
                             category values (e.g. an array of cat1, cat2, ...).
                             None if not a dictionary-style categorical.
        r   zCdescribe_categorical only works on a column with categorical dtype!T)
is_orderedis_dictionary
categories)r5   r   r9   	TypeErrorr$   catorderedr   r    r!   rJ   r.   s    r)   describe_categoricalz!PandasColumn.describe_categorical   si    $ zz!}	 5 55U 
 ))--//!&ryy1I1I'JK
 	
r+   c                z    | j                   d   }	 t        |   \  }}||fS # t        $ r t        d| d      w xY w)Nr   r@   z not yet supported)r5   _NULL_DESCRIPTIONKeyErrorr"   )r&   rC   nullvalues       r)   describe_nullzPandasColumn.describe_null   sW    zz!}	M+D1KD% U{  	M%
4&8J&KLL	Ms   ! :c                n    | j                   j                         j                         j                         S )zB
        Number of null elements. Should always be known.
        )r$   isnasumitemr.   s    r)   
null_countzPandasColumn.null_count   s'    
 yy~~##%**,,r+   c                2    d| j                   j                  iS )z8
        Store specific metadata of the column.
        zpandas.index)r$   indexr.   s    r)   metadatazPandasColumn.metadata   s    
 		00r+   c                     y)zE
        Return the number of chunks the column consists of.
           r0   r.   s    r)   
num_chunkszPandasColumn.num_chunks   s     r+   Nc              #     K   |rt|dkD  rot        | j                        }||z  }||z  dk7  r|dz  }t        d||z  |      D ]4  }t        | j                  j                  |||z    | j
                         6 y|  yw)zy
        Return an iterator yielding the chunks.
        See `DataFrame.get_chunks` for details on ``n_chunks``.
        r^   r   N)lenr$   ranger   ilocr%   )r&   n_chunksr-   stepstarts        r)   
get_chunkszPandasColumn.get_chunks   s     
 1tyy>D8#Dh!#	q$/48 "IINN554<8$:J:J 
 Js   A<A>c                    | j                         ddd}	 | j                         |d<   	 | j                         |d<   |S # t        $ r Y !w xY w# t        $ r Y |S w xY w)a`  
        Return a dictionary containing the underlying buffers.
        The returned dictionary has the following contents:
            - "data": a two-element tuple whose first element is a buffer
                      containing the data and whose second element is the data
                      buffer's associated dtype.
            - "validity": a two-element tuple whose first element is a buffer
                          containing mask values indicating missing data and
                          whose second element is the mask value buffer's
                          associated dtype. None if the null representation is
                          not a bit or byte mask.
            - "offsets": a two-element tuple whose first element is a buffer
                         containing the offset values for variable-size binary
                         data (e.g., variable-length strings) and whose second
                         element is the offsets buffer's associated dtype. None
                         if the data buffer does not have an associated offsets
                         buffer.
        N)datavalidityoffsetsrj   rk   )_get_data_buffer_get_validity_bufferr   _get_offsets_buffer)r&   bufferss     r)   get_bufferszPandasColumn.get_buffers   s    ( ))+"
	"&";";"=GJ	!%!9!9!;GI   		
  		s    ? A 	A
A	AAc                   | j                   d   t        j                  t        j                  t        j                  t        j
                  t        j                  fv r?t        | j                  j                         | j                        }| j                   }||fS | j                   d   t        j                  k(  rV| j                  j                  j                  }t        || j                        }| j                  |j                         }||fS | j                   d   t        j                  k(  r| j                  j                         }t!               }|D ]4  }t#        |t$              s|j'                  |j)                  d             6 t        t+        j,                  |d            }t        j                  dt.        j                  t0        j2                  f}||fS t5        d| j                  j                    d	      )
zZ
        Return the buffer containing the data and the buffer's associated dtype.
        r   )r(   utf-8encodinguint8)r5   r4   r@   r   )r5   r   INTUINTFLOATBOOLDATETIMEr
   r$   to_numpyr%   r9   r6   _codesr8   r;   	bytearrayr   strextendencodenp
frombufferr   r   r:   r"   )r&   bufferr5   r7   bufr   objs          r)   rl   zPandasColumn._get_data_buffer   s    ::a=MMNNOONN
 
 "$))"4"4"64CSCSTFJJE: u}9 ZZ]i333II$$++E!%D4D4DEF00=E2 u}1 ZZ]i...))$$&CA  ;c3'HHSZZZ9:; ""--"ABF   ""!!	E u} &
499??2CCS&TUUr+   c                4   | j                   \  }}| j                  d   t        j                  k(  r| j                  j                         }|dk(  }| }t        j                  t        |      ft        j                        }t        |      D ]  \  }}t        |t              r|n|||<    t        |      }t        j                  dt        j                  t         j"                  f}	||	fS 	 t$        |   dz   }
t+        |
      # t&        $ r t)        d      w xY w)z
        Return the buffer containing the mask values indicating missing data and
        the buffer's associated dtype.
        Raises NoBufferPresent if null representation is not a bit or byte mask.
        r   shaper5   r4   z! so does not have a separate maskzSee self.describe_null)rT   r5   r   r;   r$   r{   r   zerosra   bool_	enumerater   r~   r
   ry   r   r   r:   _NO_VALIDITY_BUFFERrQ   r"   r   )r&   rR   invalidr   validmaskr   r   r   r5   msgs              r)   rm   z!PandasColumn._get_validity_buffer-  s    **g::a=I,,, ))$$&C qLEiG883s8+RXX>D#C. E3#-c3#7%WQE
 "$'F ^^Q(8(8*:K:KLE5= 	@%d+.QQC
 c""	  	@%&>??	@s   +D Dc                   | j                   d   t        j                  k(  r| j                  j	                         }d}t        j                  t        |      dz   ft
        j                        }t        |      D ]=  \  }}t        |t              r |j                  d      }|t        |      z  }|||dz   <   ? t        |      }t        j                  dt        j                   t"        j$                  f}||fS t'        d      )a  
        Return the buffer containing the offset values for variable-size binary
        data (e.g., variable-length strings) and the buffer's associated dtype.
        Raises NoBufferPresent if the data buffer does not have an associated
        offsets buffer.
        r   r^   r   rr   rs   @   zJThis column has a fixed-length dtype so it does not have an offsets buffer)r5   r   r;   r$   r{   r   r   ra   int64r   r   r~   r   r
   rv   r   INT64r   r:   r   )	r&   r6   ptrrk   r   vr   r   r5   s	            r)   rn   z PandasColumn._get_offsets_bufferS  s     ::a=I,,,YY'')FChhc&kAo%7rxxHG!&) %1 a%'2A3q6MC!$A% "'*F !!!!	E u} "5 r+   )T)r'   z	pd.Seriesr(   boolreturnNone)r   int)r   ztuple[DtypeKind, int, str, str])r   zdict[str, pd.Index])N)rd   z
int | None)r   r   )r   ztuple[PandasBuffer, Any])__name__
__module____qualname____doc__r*   r-   propertyr1   r   r5   r8   rN   rT   rY   r\   r_   rg   rp   rl   rm   rn   r0   r+   r)   r   r   <   s    	
&   7 7:V 
 
8   - - 1 1"#J+	!+Z$#L&r+   r   ),
__future__r   typingr   numpyr   pandas._libs.libr   pandas._libs.tslibsr   pandas.util._decoratorsr   pandasr    pandas.api.typesr   r	   pandas.core.interchange.bufferr
   *pandas.core.interchange.dataframe_protocolr   r   r   r   pandas.core.interchange.utilsr   r   r   r   rv   rw   rx   ry   r;   rz   rA   USE_NANUSE_SENTINELNON_NULLABLEr9   USE_BYTEMASKrP   r   r   r0   r+   r)   <module>r      sR   "   ( $ 2  8   
													 OOn,,d344d;MMN//6NN^00$7NN^00$7 N77<~22A6  !>:!D }6 }r+   