
    q-Ph                        d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZmZmZmZmZ d dlmZ erd dlmZ d dlmZ d d	lmZ d d
lmZmZmZ  G d de          ZdS )    )annotations)TYPE_CHECKING)BooleanCategoricalEnumString)PolarsBuffer)ColumnColumnNullTypeCopyNotAllowedError	DtypeKind
Endianness)polars_dtype_to_dtype)Iterator)Any)Series)CategoricalDescriptionColumnBuffersDtypec                      e Zd ZdZddd&d
Zd'dZed'd            Zed(d            Zed)d            Z	ed*d            Z
ed'd            Zed+d            Zd'dZd,d-dZd.dZd/d!Zd0d$Zd0d%ZdS )1PolarsColumna6  
    A column object backed by a Polars Series.

    Parameters
    ----------
    column
        The Polars Series backing the column object.
    allow_copy
        Allow data to be copied during operations on this column. If set to `False`,
        a RuntimeError will be raised if data would be copied.
    T
allow_copycolumnr   r   boolreturnNonec               "    || _         || _        d S N)_col_allow_copy)selfr   r   s      Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/polars/interchange/column.py__init__zPolarsColumn.__init__%   s    	%    intc                4    | j                                         S )zSize of the column in elements.)r    lenr"   s    r#   sizezPolarsColumn.size)   s    y}}r%   c                n    | j         j        t          k    r| j                                         d         S dS )zOOffset of the first element with respect to the start of the underlying buffer.   r   )r    dtyper   _get_buffer_infor)   s    r#   offsetzPolarsColumn.offset-   s1     9?g%%9--//221r%   r   c                8    | j         j        }t          |          S )zData type of the column.)r    r-   r   )r"   pl_dtypes     r#   r-   zPolarsColumn.dtype5   s     9?$X...r%   r   c                   | j         j        }|t          k    r*| j         j                                        }|j        dk    }n&|t          k    r
|j        }d}nd}t          |          |dt          || j
                  dS )z
        Description of the categorical data type of the column.

        Raises
        ------
        TypeError
            If the data type of the column is not categorical.
        physicalTz8`describe_categorical` only works on categorical columnsr   )
is_orderedis_dictionary
categories)r    r-   r   catget_categoriesorderingr   r6   	TypeErrorr   r!   )r"   r-   r6   r4   msgs        r#   describe_categoricalz!PolarsColumn.describe_categorical;   s     	K5577J:5JJd]])JJJLCC..  %!&zd>NOOO
 
 	
r%   !tuple[ColumnNullType, int | None]c                P    | j         dk    rt          j        dfS t          j        dfS )z7Description of the null representation the column uses.r   N)
null_countr   NON_NULLABLEUSE_BITMASKr)   s    r#   describe_nullzPolarsColumn.describe_nullV   s-     ?a!.44!-q00r%   c                4    | j                                         S )zThe number of null elements.)r    r?   r)   s    r#   r?   zPolarsColumn.null_count^   s     y##%%%r%   dict[str, Any]c                    i S )zThe metadata for the column. r)   s    r#   metadatazPolarsColumn.metadatac   s	     	r%   c                4    | j                                         S )z3Return the number of chunks the column consists of.)r    n_chunksr)   s    r#   
num_chunkszPolarsColumn.num_chunksh   s    y!!###r%   NrI   
int | NoneIterator[PolarsColumn]c              #    K   |                                  }| j                                        }|||k    r|D ]}t          || j                  V  dS |dk    s	||z  dk    rd| d}t          |          ||z  }|D ]^}t          |          }||z  }||z  dk    r|dz  }t          d||z  |          D ]%}	t          ||	|	|z            | j                  V  &_dS )a  
        Return an iterator yielding the column chunks.

        Parameters
        ----------
        n_chunks
            The number of chunks to return. Must be a multiple of the number of chunks
            in the column.

        Notes
        -----
        When `n_chunks` is higher than the number of chunks in the column, a slice
        must be performed that is not on the chunk boundary. This will trigger some
        compute if the column contains null values or if the column is of data type
        boolean.
        Nr   r   zF`n_chunks` must be a multiple of the number of chunks of this column ()r,   )rJ   r    
get_chunksr   r!   
ValueErrorr(   range)
r"   rI   total_n_chunkschunkschunkr;   subchunks_per_chunkr*   stepstarts
             r#   rO   zPolarsColumn.get_chunksl   sr     " **%%''(n"<"< G G"5T5EFFFFFFFG G !mmN!:a!?!?'#' ' '  S//! #+n"<  5zz22--22AID"1d-@&@$GG  E&eedl23@P       r%   r   c                   | j         j        }|t          k    r| j        sd}t	          |          |t
          k    rg| j         j                                        sI| j        sd| j         j        d}t	          |          | j         j        	                                | _         | j         
                                }|                     |d                   |                     |d                   |                     |d                   dS )z6Return a dictionary containing the underlying buffers.z string buffers must be convertedzcolumn z) must be converted to a local categoricalvaluesvalidityoffsets)datarZ   r[   )r    r-   r   r!   r   r   r7   is_localnameto_local_get_buffers_wrap_data_buffer_wrap_validity_buffer_wrap_offsets_buffer)r"   r-   r;   bufferss       r#   get_bufferszPolarsColumn.get_buffers   s    	F??4#3?4C%c***k!!$)-*@*@*B*B!# /[	[[[)#...	..00DI)((** **78+<==227:3FGG001CDD
 
 	
r%   buffertuple[PolarsBuffer, Dtype]c                ^    t          || j                  }t          |j                  }||fS )Nr   )r	   r!   r   r-   r"   rf   interchange_bufferr-   s       r#   ra   zPolarsColumn._wrap_data_buffer   s2    )&T=MNNN%fl33!5((r%   Series | None!tuple[PolarsBuffer, Dtype] | Nonec                r    |d S t          || j                  }t          j        ddt          j        f}||fS )Nr   r,   b)r	   r!   r   BOOLr   NATIVEri   s       r#   rb   z"PolarsColumn._wrap_validity_buffer   sB     >4)&T=MNNNC):;!5((r%   c                r    |d S t          || j                  }t          j        ddt          j        f}||fS )Nr   @   l)r	   r!   r   INTr   rp   ri   s       r#   rc   z!PolarsColumn._wrap_offsets_buffer   sB     >4)&T=MNNNC):;!5((r%   )r   r   r   r   r   r   )r   r&   )r   r   )r   r   )r   r=   )r   rD   r   )rI   rK   r   rL   )r   r   )rf   r   r   rg   )rf   rk   r   rl   )__name__
__module____qualname____doc__r$   r*   propertyr/   r-   r<   rB   r?   rG   rJ   rO   re   ra   rb   rc   rF   r%   r#   r   r      s       
 
 >B & & & & & &       X / / / X/
 
 
 
 X
4 1 1 1 X1 & & & X&    X$ $ $ $) ) ) ) )V
 
 
 
*) ) ) )
) ) ) )) ) ) ) ) )r%   r   N)
__future__r   typingr   polars.datatypesr   r   r   r   polars.interchange.bufferr	   polars.interchange.protocolr
   r   r   r   r   polars.interchange.utilsr   collections.abcr   r   polarsr   r   r   r   r   rF   r%   r#   <module>r      sT   " " " " " "             ? ? ? ? ? ? ? ? ? ? ? ? 2 2 2 2 2 2              ; : : : : : Y((((((XXXXXXXXXXk) k) k) k) k)6 k) k) k) k) k)r%   