o
    itR                     @  s   d dl mZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 erBd dlmZmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ e
G dd dZdS )    )annotations)TYPE_CHECKING)	functions)wrap_s)expr_dispatch)CallableSequence)Series)PySeries)IntoExprIntoExprColumn)Exprc                   @  s  e Zd ZdZdZdrddZdsd	d
ZdsddZdsddZdsddZ	dtduddZ
dtduddZdsddZdddvddZdsd d!Zdsd"d#Zdsd$d%Zdsd&d'Z	(dwdd)dxd/d0Zdydd)dzd3d4Zdydd)dzd5d6Zdsd7d8Zddd9d:d{d>d?Zdsd@dAZdsdBdCZdsdDdEZddFd|dJdKZdsdLdMZdsdNdOZd9dPd}dTdUZd9d9dVd~dYdZZd9d[dd_d`ZddbdcZ 	(dwddfdgZ!dtddhdiZ"ddjddndoZ#ddpdqZ$d(S )ArrayNameSpacez$Namespace for array related methods.arrseriesr	   returnNonec                 C  s   |j | _ d S N)_s)selfr    r   J/home/app/Keep/.python/lib/python3.10/site-packages/polars/series/array.py__init__   s   zArrayNameSpace.__init__c                 C     dS )a"  
        Compute the min values of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.min()
        shape: (2,)
        Series: 'a' [i64]
        [
            1
            3
        ]
        Nr   r   r   r   r   min       zArrayNameSpace.minc                 C  r   )a"  
        Compute the max values of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.max()
        shape: (2,)
        Series: 'a' [i64]
        [
            2
            4
        ]
        Nr   r   r   r   r   max+   r   zArrayNameSpace.maxc                 C  r   )a  
        Compute the sum values of the sub-arrays.

        Notes
        -----
        If there are no non-null elements in a row, the output is `0`.

        Examples
        --------
        >>> s = pl.Series([[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.sum()
        shape: (2,)
        Series: '' [i64]
        [
            3
            7
        ]
        Nr   r   r   r   r   sum;   r   zArrayNameSpace.sumc                 C  r   )a/  
        Compute the mean of the values of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.mean()
        shape: (2,)
        Series: 'a' [f64]
        [
            1.5
            3.5
        ]
        Nr   r   r   r   r   meanO   r   zArrayNameSpace.mean   ddofintc                 C  r   )a7  
        Compute the std of the values of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.std()
        shape: (2,)
        Series: 'a' [f64]
        [
            0.707107
            0.707107
        ]
        Nr   r   r!   r   r   r   std_   r   zArrayNameSpace.stdc                 C  r   )a5  
        Compute the var of the values of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.var()
        shape: (2,)
        Series: 'a' [f64]
        [
                0.5
                0.5
        ]
        Nr   r#   r   r   r   varo   r   zArrayNameSpace.varc                 C  r   )a3  
        Compute the median of the values of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.median()
        shape: (2,)
        Series: 'a' [f64]
        [
            1.5
            3.5
        ]
        Nr   r   r   r   r   median   r   zArrayNameSpace.medianF)maintain_orderr'   boolc                C  r   )a  
        Get the unique/distinct values in the array.

        Parameters
        ----------
        maintain_order
            Maintain order of data. This requires more work.

        Returns
        -------
        Series
            Series of data type :class:`List`.

        Examples
        --------
        >>> s = pl.Series([[1, 1, 2], [3, 4, 5]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.unique()
        shape: (2,)
        Series: '' [list[i64]]
        [
            [1, 2]
            [3, 4, 5]
        ]
        Nr   )r   r'   r   r   r   unique   r   zArrayNameSpace.uniquec                 C  r   )a4  
        Count the number of unique values in every sub-arrays.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 4]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.n_unique()
        shape: (2,)
        Series: 'a' [u32]
        [
            2
            1
        ]
        Nr   r   r   r   r   n_unique   r   zArrayNameSpace.n_uniquec                 C  r   )a  
        Convert an Array column into a List column with the same inner data type.

        Returns
        -------
        Series
            Series of data type :class:`List`.

        Examples
        --------
        >>> s = pl.Series([[1, 2], [3, 4]], dtype=pl.Array(pl.Int8, 2))
        >>> s.arr.to_list()
        shape: (2,)
        Series: '' [list[i8]]
        [
                [1, 2]
                [3, 4]
        ]
        Nr   r   r   r   r   to_list   r   zArrayNameSpace.to_listc                 C  r   )a  
        Evaluate whether any boolean value is true for every subarray.

        Returns
        -------
        Series
            Series of data type :class:`Boolean`.

        Notes
        -----
        If there are no non-null elements in a row, the output is `False`.

        Examples
        --------
        >>> s = pl.Series(
        ...     [[True, True], [False, True], [False, False], [None, None], None],
        ...     dtype=pl.Array(pl.Boolean, 2),
        ... )
        >>> s.arr.any()
        shape: (5,)
        Series: '' [bool]
        [
            true
            true
            false
            false
            null
        ]
        Nr   r   r   r   r   any   r   zArrayNameSpace.anyc                 C  r   )a  
        Return the number of elements in each array.

        Returns
        -------
        Series
            Series of data type :class:`UInt32`.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2], [4, 3]], dtype=pl.Array(pl.Int64, 2))
        >>> s.arr.len()
        shape: (2,)
        Series: 'a' [u32]
        [
            2
            2
        ]
        Nr   r   r   r   r   len   r   zArrayNameSpace.lenN)as_arrayoffset
int | Exprlengthint | Expr | Noner.   c                C  r   )u  
        Slice the sub-arrays.

        Parameters
        ----------
        offset
            The starting index of the slice.
        length
            The length of the slice.
        as_array
            Return the result as a Series of data type :class:`.Array`.

        Returns
        -------
        Series
            Series of data type :class:`.List` or :class:`.Array` if `as_array=True`.

        Examples
        --------
        >>> s = pl.Series(
        ...     [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
        ...     dtype=pl.Array(pl.Int64, 6),
        ... )
        >>> s.arr.slice(1)
        shape: (2,)
        Series: '' [list[i64]]
        [
            [2, 3, … 6]
            [8, 9, … 12]
        ]
        >>> s.arr.slice(1, 3, as_array=True)
        shape: (2,)
        Series: '' [array[i64, 3]]
        [
            [2, 3, 4]
            [8, 9, 10]
        ]
        >>> s.arr.slice(-2)
        shape: (2,)
        Series: '' [list[i64]]
        [
            [5, 6]
            [11, 12]
        ]
        Nr   )r   r/   r1   r.   r   r   r   slice  r   zArrayNameSpace.slice   nc                C  r   )u#  
        Get the first `n` elements of the sub-arrays.

        Parameters
        ----------
        n
            Number of values to return for each sublist.
        as_array
            Return result as a fixed-length `Array`, otherwise as a `List`.
            If true `n` must be a constant value.

        Examples
        --------
        >>> s = pl.Series(
        ...     [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
        ...     dtype=pl.Array(pl.Int64, 6),
        ... )
        >>> s.arr.head()
        shape: (2,)
        Series: '' [list[i64]]
        [
            [1, 2, … 5]
            [7, 8, … 11]
        ]
        >>> s.arr.head(3, as_array=True)
        shape: (2,)
        Series: '' [array[i64, 3]]
        [
            [1, 2, 3]
            [7, 8, 9]
        ]
        Nr   r   r5   r.   r   r   r   head7  r   zArrayNameSpace.headc                C  r   )u$  
        Slice the last `n` values of every sublist.

        Parameters
        ----------
        n
            Number of values to return for each sublist.
        as_array
            Return result as a fixed-length `Array`, otherwise as a `List`.
            If true `n` must be a constant value.

        Examples
        --------
        >>> s = pl.Series(
        ...     [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]],
        ...     dtype=pl.Array(pl.Int64, 6),
        ... )
        >>> s.arr.tail()
        shape: (2,)
        Series: '' [list[i64]]
        [
            [2, 3, … 6]
            [8, 9, … 12]
        ]
        >>> s.arr.tail(3, as_array=True)
        shape: (2,)
        Series: '' [array[i64, 3]]
        [
            [4, 5, 6]
            [10, 11, 12]
        ]
        Nr   r6   r   r   r   tailY  r   zArrayNameSpace.tailc                 C  r   )a  
        Evaluate whether all boolean values are true for every subarray.

        Returns
        -------
        Series
            Series of data type :class:`Boolean`.

        Notes
        -----
        If there are no non-null elements in a row, the output is `True`.

        Examples
        --------
        >>> s = pl.Series(
        ...     [[True, True], [False, True], [False, False], [None, None], None],
        ...     dtype=pl.Array(pl.Boolean, 2),
        ... )
        >>> s.arr.all()
        shape: (5,)
        Series: '' [bool]
        [
            true
            false
            false
            true
            null
        ]
        Nr   r   r   r   r   all{  r   zArrayNameSpace.allT)
descending
nulls_lastmultithreadedr:   r;   r<   c                C  r   )a  
        Sort the arrays in this column.

        Parameters
        ----------
        descending
            Sort in descending order.
        nulls_last
            Place null values last.
        multithreaded
            Sort using multiple threads.

        Examples
        --------
        >>> s = pl.Series("a", [[3, 2, 1], [9, 1, 2]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.sort()
        shape: (2,)
        Series: 'a' [array[i64, 3]]
        [
            [1, 2, 3]
            [1, 2, 9]
        ]
        >>> s.arr.sort(descending=True)
        shape: (2,)
        Series: 'a' [array[i64, 3]]
        [
            [3, 2, 1]
            [9, 2, 1]
        ]

        Nr   )r   r:   r;   r<   r   r   r   sort  r   zArrayNameSpace.sortc                 C  r   )a@  
        Reverse the arrays in this column.

        Examples
        --------
        >>> s = pl.Series("a", [[3, 2, 1], [9, 1, 2]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.reverse()
        shape: (2,)
        Series: 'a' [array[i64, 3]]
        [
            [1, 2, 3]
            [2, 1, 9]
        ]

        Nr   r   r   r   r   reverse  r   zArrayNameSpace.reversec                 C  r   )a  
        Retrieve the index of the minimal value in every sub-array.

        Returns
        -------
        Series
            Series of data type :class:`UInt32` or :class:`UInt64`
            (depending on compilation).

        Examples
        --------
        >>> s = pl.Series("a", [[3, 2, 1], [9, 1, 2]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.arg_min()
        shape: (2,)
        Series: 'a' [u32]
        [
            2
            1
        ]

        Nr   r   r   r   r   arg_min  r   zArrayNameSpace.arg_minc                 C  r   )a  
        Retrieve the index of the maximum value in every sub-array.

        Returns
        -------
        Series
            Series of data type :class:`UInt32` or :class:`UInt64`
            (depending on compilation).

        Examples
        --------
        >>> s = pl.Series("a", [[0, 9, 3], [9, 1, 2]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.arg_max()
        shape: (2,)
        Series: 'a' [u32]
        [
            1
            0
        ]

        Nr   r   r   r   r   arg_max  r   zArrayNameSpace.arg_max)null_on_oobindexint | IntoExprColumnrA   c                C  r   )a  
        Get the value by index in the sub-arrays.

        So index `0` would return the first item of every sublist
        and index `-1` would return the last item of every sublist
        if an index is out of bounds, it will return a `None`.

        Parameters
        ----------
        index
            Index to return per sublist
        null_on_oob
            Behavior if an index is out of bounds:
            True -> set as null
            False -> raise an error

        Returns
        -------
        Series
            Series of innter data type.

        Examples
        --------
        >>> s = pl.Series(
        ...     "a", [[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=pl.Array(pl.Int32, 3)
        ... )
        >>> s.arr.get(pl.Series([1, -2, 0]), null_on_oob=True)
        shape: (3,)
        Series: 'a' [i32]
        [
            2
            5
            7
        ]

        Nr   )r   rB   rA   r   r   r   get   r   zArrayNameSpace.getc                 C  r   )a_  
        Get the first value of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series(
        ...     "a", [[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype=pl.Array(pl.Int32, 3)
        ... )
        >>> s.arr.first()
        shape: (3,)
        Series: 'a' [i32]
        [
            1
            4
            7
        ]

        Nr   r   r   r   r   first&  r   zArrayNameSpace.firstc                 C  r   )a]  
        Get the last value of the sub-arrays.

        Examples
        --------
        >>> s = pl.Series(
        ...     "a", [[1, 2, 3], [4, 5, 6], [7, 9, 8]], dtype=pl.Array(pl.Int32, 3)
        ... )
        >>> s.arr.last()
        shape: (3,)
        Series: 'a' [i32]
        [
            3
            6
            8
        ]

        Nr   r   r   r   r   last:  r   zArrayNameSpace.last)ignore_nulls	separatorr   rG   c                C  r   )a+  
        Join all string items in a sub-array and place a separator between them.

        This errors if inner type of array `!= String`.

        Parameters
        ----------
        separator
            string to separate the items with
        ignore_nulls
            Ignore null values (default).

            If set to ``False``, null values will be propagated.
            If the sub-list contains any null values, the output is ``None``.

        Returns
        -------
        Series
            Series of data type :class:`String`.

        Examples
        --------
        >>> s = pl.Series([["x", "y"], ["a", "b"]], dtype=pl.Array(pl.String, 2))
        >>> s.arr.join(separator="-")
        shape: (2,)
        Series: '' [str]
        [
            "x-y"
            "a-b"
        ]

        Nr   )r   rH   rG   r   r   r   joinN  r   zArrayNameSpace.join)empty_as_null
keep_nullsrJ   rK   c                C  r   )a  
        Returns a column with a separate row for every array element.

        Parameters
        ----------
        empty_as_null
            Explode an empty array into a `null`.
        keep_nulls
            Explode a `null` array into a `null`.

        Returns
        -------
        Series
            Series with the data type of the array elements.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2, 3], [4, 5, 6]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.explode()
        shape: (6,)
        Series: 'a' [i64]
        [
            1
            2
            3
            4
            5
            6
        ]
        Nr   )r   rJ   rK   r   r   r   explodep  r   zArrayNameSpace.explode)nulls_equalitemr   rM   c                C  r   )a  
        Check if sub-arrays contain the given item.

        Parameters
        ----------
        item
            Item that will be checked for membership
        nulls_equal : bool, default True
            If True, treat null as a distinct value. Null values will not propagate.

        Returns
        -------
        Series
            Series of data type :class:`Boolean`.

        Examples
        --------
        >>> s = pl.Series(
        ...     "a", [[3, 2, 1], [1, 2, 3], [4, 5, 6]], dtype=pl.Array(pl.Int32, 3)
        ... )
        >>> s.arr.contains(1)
        shape: (3,)
        Series: 'a' [bool]
        [
            true
            true
            false
        ]

        Nr   )r   rN   rM   r   r   r   contains  r   zArrayNameSpace.containselementc                 C  r   )a  
        Count how often the value produced by `element` occurs.

        Parameters
        ----------
        element
            An expression that produces a single value

        Examples
        --------
        >>> s = pl.Series("a", [[1, 2, 3], [2, 2, 2]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.count_matches(2)
        shape: (2,)
        Series: 'a' [u32]
        [
            1
            3
        ]

        Nr   )r   rP   r   r   r   count_matches  r   zArrayNameSpace.count_matchesfields+Callable[[int], str] | Sequence[str] | Nonec                 C  s,   t | j}| t|jj|	 S )u  
        Convert the series of type `Array` to a series of type `Struct`.

        Parameters
        ----------
        fields
            If the name and number of the desired fields is known in advance
            a list of field names can be given, which will be assigned by index.
            Otherwise, to dynamically assign field names, a custom function can be
            used; if neither are set, fields will be `field_0, field_1 .. field_n`.

        Examples
        --------
        Convert array to struct with default field name assignment:

        >>> s1 = pl.Series("n", [[0, 1, 2], [3, 4, 5]], dtype=pl.Array(pl.Int8, 3))
        >>> s2 = s1.arr.to_struct()
        >>> s2
        shape: (2,)
        Series: 'n' [struct[3]]
        [
            {0,1,2}
            {3,4,5}
        ]
        >>> s2.struct.fields
        ['field_0', 'field_1', 'field_2']

        Convert array to struct with field name assignment by function/index:

        >>> s3 = s1.arr.to_struct(fields=lambda idx: f"n{idx:02}")
        >>> s3.struct.fields
        ['n00', 'n01', 'n02']

        Convert array to struct with field name assignment by
        index from a list of names:

        >>> s1.arr.to_struct(fields=["one", "two", "three"]).struct.unnest()
        shape: (2, 3)
        ┌─────┬─────┬───────┐
        │ one ┆ two ┆ three │
        │ --- ┆ --- ┆ ---   │
        │ i8  ┆ i8  ┆ i8    │
        ╞═════╪═════╪═══════╡
        │ 0   ┆ 1   ┆ 2     │
        │ 3   ┆ 4   ┆ 5     │
        └─────┴─────┴───────┘
        )
r   r   Zto_frameselectFcolnamer   	to_structZ	to_series)r   rR   sr   r   r   rX     s   
3"zArrayNameSpace.to_structc                 C  r   )a  
        Shift array values by the given number of indices.

        Parameters
        ----------
        n
            Number of indices to shift forward. If a negative value is passed, values
            are shifted in the opposite direction instead.

        Notes
        -----
        This method is similar to the `LAG` operation in SQL when the value for `n`
        is positive. With a negative value for `n`, it is similar to `LEAD`.

        Examples
        --------
        By default, array values are shifted forward by one index.

        >>> s = pl.Series([[1, 2, 3], [4, 5, 6]], dtype=pl.Array(pl.Int64, 3))
        >>> s.arr.shift()
        shape: (2,)
        Series: '' [array[i64, 3]]
        [
            [null, 1, 2]
            [null, 4, 5]
        ]

        Pass a negative value to shift in the opposite direction instead.

        >>> s.arr.shift(-2)
        shape: (2,)
        Series: '' [array[i64, 3]]
        [
            [3, null, null]
            [6, null, null]
        ]
        Nr   )r   r5   r   r   r   shift  r   zArrayNameSpace.shift)as_listexprr   r[   c                C  r   )a  
        Run any polars expression against the arrays' elements.

        Parameters
        ----------
        expr
            Expression to run. Note that you can select an element with `pl.element()`
        as_list
            Collect the resulting data as a list. This allows for expressions which
            output a variable amount of data.

        Examples
        --------
        >>> s = pl.Series("a", [[1, 4], [8, 5], [3, 2]], pl.Array(pl.Int64, 2))
        >>> s.arr.eval(pl.element().rank())
        shape: (3,)
        Series: 'a' [array[f64, 2]]
        [
            [1.0, 2.0]
            [2.0, 1.0]
            [2.0, 1.0]
        ]
        Nr   )r   r\   r[   r   r   r   eval#  r   zArrayNameSpace.evalc                 C  r   )a  
        Run any polars aggregation expression against the arrays' elements.

        Parameters
        ----------
        expr
            Expression to run. Note that you can select an element with `pl.element()`.

        Examples
        --------
        >>> s = pl.Series(
        ...     "a", [[1, None], [42, 13], [None, None]], pl.Array(pl.Int64, 2)
        ... )
        >>> s.arr.agg(pl.element().null_count())
        shape: (3,)
        Series: 'a' [u32]
        [
            1
            0
            2
        ]
        >>> s.arr.agg(pl.element().drop_nulls())
        shape: (3,)
        Series: 'a' [list[i64]]
        [
            [1]
            [42, 13]
            []
        ]
        Nr   )r   r\   r   r   r   agg<  r   zArrayNameSpace.agg)r   r	   r   r   )r   r	   )r    )r!   r"   r   r	   )r'   r(   r   r	   r   )r/   r0   r1   r2   r.   r(   r   r	   )r4   )r5   r0   r.   r(   r   r	   )r:   r(   r;   r(   r<   r(   r   r	   )rB   rC   rA   r(   r   r	   )rH   r   rG   r(   r   r	   )rJ   r(   rK   r(   r   r	   )rN   r   rM   r(   r   r	   )rP   r   r   r	   )rR   rS   r   r	   )r5   rC   r   r	   )r\   r   r[   r(   r   r	   )r\   r   r   r	   )%__name__
__module____qualname____doc__	_accessorr   r   r   r   r   r$   r%   r&   r)   r*   r+   r,   r-   r3   r7   r8   r9   r=   r>   r?   r@   rD   rE   rF   rI   rL   rO   rQ   rX   rZ   r]   r^   r   r   r   r   r      sT    









5"
""
'


&
" 
 6'r   N)
__future__r   typingr   Zpolarsr   rU   Zpolars._utils.wrapr   Zpolars.series.utilsr   collections.abcr   r   r	   Zpolars._plrr
   Zpolars._typingr   r   Zpolars.expr.exprr   r   r   r   r   r   <module>   s    