
    &`i5                        d dl Z d dlZd dlmZmZmZmZmZmZm	Z	 d dl
Zd dlZd dlZd dlmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZ d dlmZmZ d d	lm Z  	 d d
lm!Z! n# e"$ r	 d dlm#Z! Y nw xY wdee$         fdZ%dee$         fdZ&dZ' ej(        e'd          dk    r eej)                   ed          k     r	d dl*m+Z+ e+Z,nd dl*m-Z- e-Z,e,j.        e,_/         ed           eej)                  cxk    r ed          k     rn ne%e,_.        ne&e,_.        de,_0         e d          ej1        j2        j3         G d dej1        j2        j4                                          Z5 G d dej1        j2        j6                  Z7 G d d          Z8 e d           G d d e7e8                      Z9 e d           G d! d"ej1        j2        j:        e7e8                      Z;e9<                                 e9=                                 e9>                                 e;<                                 e;=                                 e;>                                  e d          d#ej?        de@fd$            ZAdS )%    N)AnyCallableListOptionalSequenceTupleUnion)Version)Dtype)set_function_name)ABCDataFrame	ABCSeries)check_array_indexervalidate_indices)_create_possibly_ragged_ndarray"_is_ndarray_variable_shaped_tensor)	PublicAPI)ABCIndex)ABCIndexClassreturnc                 $   
 ddl m} ddlm
 t	           j        t                    s                                 S  | j        d          }t          j	        |          }|j
        dk    r                                 S 
 fd} j        }||                    d          }|                    d	          }t          j	         |||                    }|j        j        rd
nd}|                    |j        |          }	 ||	d           S )Nr   extract_arrayformat_arrayTextract_numpy   c                      | |j         j        j        j        j        j        j        j        
  
        }|S )Nfloat_formatna_repdigitsspacejustifydecimalleading_spacequotingr    array_
formatter_
fmt_valuesr   selfs      y/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/air/util/tensor_extensions/pandas.pyformat_array_wrapz2_format_strings_patched.<locals>.format_array_wrapM   sQ    !\*;;*LL,L
 
 

     boxedKFCorder)pandas.core.constructionr   pandas.io.formats.formatr   
isinstancevaluesTensorArray_format_strings_orignpasarrayndim	formatter
_formatterravelflagsf_contiguousreshapeshape)r-   r   r;   arrayr/   flat_formatter
flat_arrayfmt_flat_arrayr7   	fmt_arrayr   s   `         @r.   _format_strings_patchedrM   @   sE   666666555555dk;// +((***]4;d;;;FJvEzQ((***      ^N***66 S!!JZ 1 1*n M MNNN;+4CCE&&u{%&@@I Y---r0   c                 V    ddl m ddlm} ddlm ddlm t           j	        t                    s                                 S  | j	        d          }t          j        |          }|j        dk    r                                 S  fd	} j        }||                    d
          }|                    d          }t          j         |||                    }|j        j        rdnd}|                    |j        |          }	fd}
 |
|	 j                  S )Nr   )partialr   r   )pprint_thingTr   r   c                 v     | |j         j        j        j        j        j        j        	  	        }|S )Nr!   r"   r#   r$   r%   r&   r'   rR   r)   s      r.   r/   z9_format_strings_patched_v1_0_0.<locals>.format_array_wrapz   sL    !\*;;*LL,

 

 


 r0   r1   r3   r4   r5   r6   c                      d          fd}g }| D ]<}|du rdnd}|                     |                     ||                               =|S )N)	
)escape_charsc                 4    t           |                     S Nstr)xrA   s    r.   _formatzL_format_strings_patched_v1_0_0.<locals>.format_strings_slim.<locals>._format   s    yy||$$$r0   Fz{v}z {v})v)appendformat)	r*   r'   r]   r,   r^   tplrA   rO   rP   s	         @r.   format_strings_slimz;_format_strings_patched_v1_0_0.<locals>.format_strings_slim   s    G+
 
 
	
	% 	% 	% 	% 	% 
 	8 	8A(E11%%vCcjj771::j667777r0   )	functoolsrO   r8   r   r9   r   pandas.io.formats.printingrP   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   r'   )r-   r   r;   rH   r/   rI   rJ   rK   r7   rL   rb   r   rO   rP   s   `          @@@r.   _format_strings_patched_v1_0_0re   j   s   !!!!!!666666555555777777dk;// +((***]4;d;;;FJvEzQ((***      ^N***66 S!!JZ 1 1*n M MNNN;+4CCE&&u{%&@@I      y$*<===r0   )TENSOR_COLUMN_EXTENSION_FORMATTER_ENABLED1z2.2.0)ExtensionArrayFormatter)_ExtensionArrayFormatterz1.1.0z1.3.0Tbeta)	stabilityc                   P   e Zd ZdZdZdeee         df         dej	        fdZ
ed             Zed             Zed	             Zed
             Zedefd            Zedefd            Zed             Zdeej        ej        f         fdZdefdZdefdZed             ZdS )TensorDtypea  
    Pandas extension type for a column of homogeneous-typed tensors.

    This extension supports tensors in which the elements have different shapes.
    However, each tensor element must be non-ragged, i.e. each tensor element must have
    a well-defined, non-ragged shape.

    See:
    https://github.com/pandas-dev/pandas/blob/master/pandas/core/dtypes/base.py
    for up-to-date interface documentation and the subclassing contract. The
    docstrings of the below properties and methods were copied from the base
    ExtensionDtype.

    Examples:
        >>> # Create a DataFrame with a list of ndarrays as a column.
        >>> import pandas as pd
        >>> import numpy as np
        >>> import ray
        >>> df = pd.DataFrame({
        ...     "one": [1, 2, 3],
        ...     "two": list(np.arange(24).reshape((3, 2, 2, 2)))})
        >>> # Note the opaque np.object dtype for this column.
        >>> df.dtypes # doctest: +SKIP
        one     int64
        two    object
        dtype: object
        >>> # Cast column to our TensorDtype extension type.
        >>> from ray.data.extensions import TensorDtype
        >>> df["two"] = df["two"].astype(TensorDtype(np.int64, (3, 2, 2, 2)))
        >>> # Note that the column dtype is now TensorDtype instead of
        >>> # np.object.
        >>> df.dtypes # doctest: +SKIP
        one          int64
        two    TensorDtype(shape=(3, 2, 2, 2), dtype=int64)
        dtype: object
        >>> # Pandas is now aware of this tensor column, and we can do the
        >>> # typical DataFrame operations on this column.
        >>> col = 2 * (df["two"] + 10)
        >>> # The ndarrays underlying the tensor column will be manipulated,
        >>> # but the column itself will continue to be a Pandas type.
        >>> type(col) # doctest: +SKIP
        pandas.core.series.Series
        >>> col # doctest: +SKIP
        0   [[[ 2  4]
              [ 6  8]]
             [[10 12]
               [14 16]]]
        1   [[[18 20]
              [22 24]]
             [[26 28]
              [30 32]]]
        2   [[[34 36]
              [38 40]]
             [[42 44]
              [46 48]]]
        Name: two, dtype: TensorDtype(shape=(3, 2, 2, 2), dtype=int64)
        >>> # Once you do an aggregation on that column that returns a single
        >>> # row's value, you get back our TensorArrayElement type.
        >>> tensor = col.mean()
        >>> type(tensor) # doctest: +SKIP
        ray.data.extensions.tensor_extension.TensorArrayElement
        >>> tensor # doctest: +SKIP
        array([[[18., 20.],
                [22., 24.]],
               [[26., 28.],
                [30., 32.]]])
        >>> # This is a light wrapper around a NumPy ndarray, and can easily
        >>> # be converted to an ndarray.
        >>> type(tensor.to_numpy()) # doctest: +SKIP
        numpy.ndarray
        >>> # In addition to doing Pandas operations on the tensor column,
        >>> # you can now put the DataFrame into a Dataset.
        >>> ds = ray.data.from_pandas(df) # doctest: +SKIP
        >>> # Internally, this column is represented the corresponding
        >>> # Arrow tensor extension type.
        >>> ds.schema() # doctest: +SKIP
        one: int64
        two: extension<arrow.py_extension_type<ArrowTensorType>>
        >>> # You can write the dataset to Parquet.
        >>> ds.write_parquet("/some/path") # doctest: +SKIP
        >>> # And you can read it back.
        >>> read_ds = ray.data.read_parquet("/some/path") # doctest: +SKIP
        >>> read_ds.schema() # doctest: +SKIP
        one: int64
        two: extension<arrow.py_extension_type<ArrowTensorType>>
        >>> read_df = ray.get(read_ds.to_pandas_refs())[0] # doctest: +SKIP
        >>> read_df.dtypes # doctest: +SKIP
        one          int64
        two    TensorDtype(shape=(3, 2, 2, 2), dtype=int64)
        dtype: object
        >>> # The tensor extension type is preserved along the
        >>> # Pandas --> Arrow --> Parquet --> Arrow --> Pandas
        >>> # conversion chain.
        >>> read_df.equals(df) # doctest: +SKIP
        True
    NrG   .dtypec                 "    || _         || _        d S rY   _shape_dtype)r-   rG   rn   s      r.   __init__zTensorDtype.__init__&  s    r0   c                     t           S )a   
        The scalar type for the array, e.g. ``int``
        It's expected ``ExtensionArray[item]`` returns an instance
        of ``ExtensionDtype.type`` for scalar ``item``, assuming
        that value is valid (not NA). NA values do not need to be
        instances of `type`.
        )TensorArrayElementr-   s    r.   typezTensorDtype.type*  s
     "!r0   c                     | j         S )z>
        The dtype of the underlying tensor elements.
        )rr   rv   s    r.   element_dtypezTensorDtype.element_dtype5  s    
 {r0   c                     | j         S )z
        The shape of the underlying tensor elements. This will be a tuple of Nones if
        the corresponding TensorArray for this TensorDtype holds variable-shaped tensor
        elements.
        )rq   rv   s    r.   element_shapezTensorDtype.element_shape<  s     {r0   c                 >    t          d | j        D                       S )z{
        Whether the corresponding TensorArray for this TensorDtype holds variable-shaped
        tensor elements.
        c              3      K   | ]}|d u V  	d S rY    ).0dim_sizes     r.   	<genexpr>z1TensorDtype.is_variable_shaped.<locals>.<genexpr>K  s'      ??8t#??????r0   )allrG   rv   s    r.   is_variable_shapedzTensorDtype.is_variable_shapedE  s#     ??DJ??????r0   r   c                 (    d| j          d| j         dS )zp
        A string identifying the data type.
        Will be used for display in, e.g. ``Series.dtype``
        zTensorDtype(shape=z, dtype=)rp   rv   s    r.   namezTensorDtype.nameM  s"     HDKGGGGGGr0   stringc                    ddl }ddl}t          |t                    st	          dt          |                     d}|                    ||          }d| j         d| d}|t	          |          |                                }t          |          dk    rt	          |          |\  }}	}
|
                    |	          }	t          j        |
          }
 | |	|
          S )	a=  
        Construct this type from a string.

        This is useful mainly for data types that accept parameters.
        For example, a period dtype accepts a frequency parameter that
        can be set as ``period[H]`` (where H means hourly frequency).

        By default, in the abstract class, just the name of the type is
        expected. But subclasses can overwrite this method to accept
        parameters.

        Parameters
        ----------
        string : str
            The name of the type, for example ``category``.

        Returns
        -------
        ExtensionDtype
            Instance of the dtype.

        Raises
        ------
        TypeError
            If a class cannot be constructed from this 'string'.

        Examples
        --------
        For extension dtypes with arguments the following may be an
        adequate implementation.

        >>> import re
        >>> @classmethod
        ... def construct_from_string(cls, string):
        ...     pattern = re.compile(r"^my_type\[(?P<arg_name>.+)\]$")
        ...     match = pattern.match(string)
        ...     if match:
        ...         return cls(**match.groupdict())
        ...     else:
        ...         raise TypeError(
        ...             f"Cannot construct a '{cls.__name__}' from '{string}'"
        ...         )
        r   Nz.'construct_from_string' expects a string, got zP^(TensorDtype|numpy.ndarray)\(shape=(\((?:(?:\d+|None),?\s?)*\)), dtype=(\w+)\)$zCannot construct a 'z' from 'zF'; expected a string like 'TensorDtype(shape=(1, 2, 3), dtype=int64)'.   )astrer:   r[   	TypeErrorrw   search__name__groupslenliteral_evalr>   rn   )clsr   r   r   regexmerr_msgr   _rG   rn   s              r.   construct_from_stringz!TensorDtype.construct_from_stringU  s   Z 	


			&#&& 	OfOO  D 	 IIeV$$@3< @ @ @ @ @ 	 9G$$$v;;!G$$$ 5%  ''s5%   r0   c                     t           S )zq
        Return the array type associated with this dtype.

        Returns
        -------
        type
        )r<   )r   s    r.   construct_array_typez TensorDtype.construct_array_type  s
     r0   rH   c                 D   t          |t          j                  rd|j        dk    r1t	          j        d |                                D                       }n<|                    d                                          }n|                                }t          |          S )a|  
        Convert a pyarrow (chunked) array to a TensorArray.

        This and TensorArray.__arrow_array__ make up the
        Pandas extension type + array <--> Arrow extension type + array
        interoperability protocol. See
        https://pandas.pydata.org/pandas-docs/stable/development/extending.html#compatibility-with-apache-arrow
        for more information.
        r   c                 6    g | ]}|                                 S r~   )to_numpy)r   chunks     r.   
<listcomp>z.TensorDtype.__from_arrow__.<locals>.<listcomp>  s"    FFF%U^^%%FFFr0   r   )
r:   paChunkedArray
num_chunksr>   concatenate
iterchunksr   r   r<   )r-   rH   r;   s      r.   __from_arrow__zTensorDtype.__from_arrow__  s     eR_-- 
	&!## FF53C3C3E3EFFF  Q0022^^%%F6"""r0   c                     | j         S rY   )r   rv   s    r.   __str__zTensorDtype.__str__  s
    yr0   c                      t          |           S rY   rZ   rv   s    r.   __repr__zTensorDtype.__repr__  s    4yyr0   c                 .    ddl m}  || j                  S )B  
        Whether this extension array should be considered boolean.

        By default, ExtensionArrays are assumed to be non-numeric.
        Setting this to True will affect the behavior of several places,
        e.g.

        * is_bool
        * boolean indexing

        Returns
        -------
        bool
        r   )is_bool_dtype)pandas.core.dtypes.commonr   rr   )r-   r   s     r.   _is_booleanzTensorDtype._is_boolean  s(    " 	<;;;;;}T[)))r0   )r   
__module____qualname____doc__baser   r   intr>   rn   rs   propertyrw   ry   r{   r   r[   r   classmethodr   r   r	   r   Arrayr   r   r   r   r   r~   r0   r.   rm   rm      s       _ _H DeHSM3$67      " " X"   X   X @ @ X@ Hc H H H XH F!3 F! F! F! [F!P   [#E"(BO*C$D # # # #0    #     * * X* * *r0   rm   c                   @    e Zd ZdZedd            Zed             ZdS )_TensorOpsMixinze
    Mixin for TensorArray operator support, applying operations on the
    underlying ndarrays.
    TNc                 N      fd}dj          dt          |           S )zc
        Add support for binary operators by unwrapping, applying, and
        rewrapping.
        c                    | j         }t          |t          t          t          f          rt
          S dv rt          t          |t          t          f          r|j         }n|} ||          }t          | t                    r9t          |t                    rt          j
        |          st          |          }n |          }|S )N)
__divmod____rdivmod__)_tensorr:   r   r   r   NotImplementedNotImplementedErrorr<   ru   r>   isscalar)	r-   otherlvaluesrvaluesresultresult_wrappedr   opop_names	         r.   _binopz._TensorOpsMixin._create_method.<locals>._binop  s    lG%,	8!DEE &%% 777 *)%+/A!BCC  -R))F $ 233 -u&899-ACUASAS- "-V!4!4!$V!!r0   __)r   r   )r   r   coerce_to_dtyperesult_dtyper   r   s   ``   @r.   _create_methodz_TensorOpsMixin._create_method  sN    	" 	" 	" 	" 	" 	" 	"< 'r{&&& #666r0   c                 ,    |                      |          S rY   )r   )r   r   s     r.   _create_logical_methodz&_TensorOpsMixin._create_logical_method  s    !!"%%%r0   )TN)r   r   r   r   r   r   r   r~   r0   r.   r   r     sZ         
 (7 (7 (7 [(7T & & [& & &r0   r   c                   N    e Zd ZdZdeegef         fdZd Zd Zd Z	d Z
d Zd	S )
_TensorScalarCastMixinzH
    Mixin for casting scalar tensors to a particular numeric type.
    funcc                 "     || j                   S rY   r   )r-   r   s     r.   _scalarfuncz"_TensorScalarCastMixin._scalarfunc  s    tDL!!!r0   c                 6    |                      t                    S rY   )r   complexrv   s    r.   __complex__z"_TensorScalarCastMixin.__complex__  s    (((r0   c                 6    |                      t                    S rY   )r   floatrv   s    r.   	__float__z _TensorScalarCastMixin.__float__  s    &&&r0   c                 6    |                      t                    S rY   )r   r   rv   s    r.   __int__z_TensorScalarCastMixin.__int__!      $$$r0   c                 6    |                      t                    S rY   )r   hexrv   s    r.   __hex__z_TensorScalarCastMixin.__hex__$  r   r0   c                 6    |                      t                    S rY   )r   octrv   s    r.   __oct__z_TensorScalarCastMixin.__oct__'  r   r0   N)r   r   r   r   r   r   r   r   r   r   r   r   r~   r0   r.   r   r     s         "# 4 " " " ") ) )' ' '% % %% % %% % % % %r0   r   c                       e Zd ZdZdej        fdZd Zd Ze	d             Z
e	d             Ze	d             Ze	d	             Zd
 Zddej        dej        fdZdS )ru   zJ
    Single element of a TensorArray, wrapping an underlying ndarray.
    r;   c                     || _         dS )z
        Construct a TensorArrayElement from a NumPy ndarray.

        Args:
            values: ndarray that underlies this TensorArray element.
        Nr   r-   r;   s     r.   rs   zTensorArrayElement.__init__1  s     r0   c                 4    | j                                         S rY   r   r   rv   s    r.   r   zTensorArrayElement.__repr__:      |$$&&&r0   c                 4    | j                                         S rY   r   r   rv   s    r.   r   zTensorArrayElement.__str__=      |##%%%r0   c                     | j         j        S zf
        Get the dtype of the tensor.
        :return: The numpy dtype of the backing ndarray
        r   rn   rv   s    r.   numpy_dtypezTensorArrayElement.numpy_dtype@       |!!r0   c                     | j         j        S zl
        Get the number of tensor dimensions.
        :return: integer for the number of dimensions
        r   r@   rv   s    r.   
numpy_ndimzTensorArrayElement.numpy_ndimH       |  r0   c                     | j         j        S z~
        Get the shape of the tensor.
        :return: A tuple of integers for the numpy shape of the backing ndarray
        r   rG   rv   s    r.   numpy_shapezTensorArrayElement.numpy_shapeP  r   r0   c                     | j         j        S zo
        Get the size of the tensor.
        :return: integer for the number of elements in the tensor
        r   sizerv   s    r.   
numpy_sizezTensorArrayElement.numpy_sizeX  r   r0   c                 4    t          j        | j                  S )zG
        Return the values of this element as a NumPy ndarray.
        r>   r?   r   rv   s    r.   r   zTensorArrayElement.to_numpy`  s     z$,'''r0   Nrn   r   c                 4    t          j        | j        fd|i|S Nrn   r   r-   rn   kwargss      r.   	__array__zTensorArrayElement.__array__f  !    z$,>>e>v>>>r0   rY   )r   r   r   r   r>   ndarrayrs   r   r   r   r   r   r   r   r   rn   r  r~   r0   r.   ru   ru   +  s         rz    ' ' '& & & " " X" ! ! X! " " X" ! ! X!( ( (? ?rx ?RZ ? ? ? ? ? ?r0   ru   c                       e Zd ZdZej        ej        ej        ej        ej	        ej
        ej        ej        ej        ej        d
Zdeej        eeeej        ef                  eef         fdZeddddee         d	efd
            Zedej        dej        j        j        fd            Z dee!e"ej        f         ded         fdZ#de!fdZ$e%dej        j        j&        fd            Z'e%d             Z(e%de!fd            Z)d7dZ*	 d8dee!         dededd fdZ+d7dZ,eded          dd fd            Z-dee!ej        f         deddfdZ.def fd Z/d! Z0d" Z1de2ej        ef         fd#Z3d9d%e4d&efd'Z5d:dej'        dej        fd(Z6d)e7d*e4fd+Z8ddej        j        j9        fdej'        d	ed,efd-Z:e%d.             Z;e%d/             Z<e%d0             Z=e%d1             Z>d9d2Z?d;d3Zd;d4Zd:d5Z@e%d6             ZA xZBS )<r<   a  
    Pandas `ExtensionArray` representing a tensor column, i.e. a column
    consisting of ndarrays as elements.

    This extension supports tensors in which the elements have different shapes.
    However, each tensor element must be non-ragged, i.e. each tensor element must have
    a well-defined, non-ragged shape.

    Examples:
        >>> # Create a DataFrame with a list of ndarrays as a column.
        >>> import pandas as pd
        >>> import numpy as np
        >>> import ray
        >>> from ray.data.extensions import TensorArray
        >>> df = pd.DataFrame({
        ...     "one": [1, 2, 3],
        ...     "two": TensorArray(np.arange(24).reshape((3, 2, 2, 2)))})
        >>> # Note that the column dtype is TensorDtype.
        >>> df.dtypes # doctest: +SKIP
        one          int64
        two    TensorDtype(shape=(3, 2, 2, 2), dtype=int64)
        dtype: object
        >>> # Pandas is aware of this tensor column, and we can do the
        >>> # typical DataFrame operations on this column.
        >>> col = 2 * (df["two"] + 10)
        >>> # The ndarrays underlying the tensor column will be manipulated,
        >>> # but the column itself will continue to be a Pandas type.
        >>> type(col) # doctest: +SKIP
        pandas.core.series.Series
        >>> col # doctest: +SKIP
        0   [[[ 2  4]
              [ 6  8]]
             [[10 12]
               [14 16]]]
        1   [[[18 20]
              [22 24]]
             [[26 28]
              [30 32]]]
        2   [[[34 36]
              [38 40]]
             [[42 44]
              [46 48]]]
        Name: two, dtype: TensorDtype(shape=(3, 2, 2, 2), dtype=int64)
        >>> # Once you do an aggregation on that column that returns a single
        >>> # row's value, you get back our TensorArrayElement type.
        >>> tensor = col.mean() # doctest: +SKIP
        >>> type(tensor) # doctest: +SKIP
        ray.data.extensions.tensor_extension.TensorArrayElement
        >>> tensor # doctest: +SKIP
        array([[[18., 20.],
                [22., 24.]],
               [[26., 28.],
                [30., 32.]]])
        >>> # This is a light wrapper around a NumPy ndarray, and can easily
        >>> # be converted to an ndarray.
        >>> type(tensor.to_numpy()) # doctest: +SKIP
        numpy.ndarray
        >>> # In addition to doing Pandas operations on the tensor column,
        >>> # you can now put the DataFrame into a Dataset.
        >>> ds = ray.data.from_pandas(df) # doctest: +SKIP
        >>> # Internally, this column is represented the corresponding
        >>> # Arrow tensor extension type.
        >>> ds.schema() # doctest: +SKIP
        one: int64
        two: extension<arrow.py_extension_type<ArrowTensorType>>
        >>> # You can write the dataset to Parquet.
        >>> ds.write_parquet("/some/path") # doctest: +SKIP
        >>> # And you can read it back.
        >>> read_ds = ray.data.read_parquet("/some/path") # doctest: +SKIP
        >>> read_ds.schema() # doctest: +SKIP
        one: int64
        two: extension<arrow.py_extension_type<ArrowTensorType>>

        >>> read_df = ray.get(read_ds.to_pandas_refs())[0] # doctest: +SKIP
        >>> read_df.dtypes # doctest: +SKIP
        one          int64
        two    TensorDtype(shape=(3, 2, 2, 2), dtype=int64)
        dtype: object
        >>> # The tensor extension type is preserved along the
        >>> # Pandas --> Arrow --> Parquet --> Arrow --> Pandas
        >>> # conversion chain.
        >>> read_df.equals(df) # doctest: +SKIP
        True
    )
sumr   anyminmaxmeanmedianprodstdvarr;   c                 ^   t          |t                    rt          |          }not          |t                    rd |D             }t          |          }n>t          |t                    r)t          j        t          j        |          gd          }t          |t
          j                  r|j	        j
        t
          j        u rot          |          dk    rnt          d |D                       rd |D             }t          |          }nst          dt          |d                    d          nLt          |t                    rt          d	          t          d
| dt          |          j         d          t          |t
          j                  sJ || _        d| _        dS )zy
        Args:
            values: A NumPy ndarray or sequence of NumPy ndarrays of equal
                shape.
        c                 d    g | ]-}t          |t                    rt          j        |          n|.S r~   r:   ru   r>   r?   r   r^   s     r.   r   z(TensorArray.__init__.<locals>.<listcomp>  sD        ",A/A!B!BI
1  r0   Fcopyr   c              3      K   | ]A}t          |t          j        t          t          f          ot          |t
                     V  Bd S rY   )r:   r>   r  ru   r   r[   r  s     r.   r   z'TensorArray.__init__.<locals>.<genexpr>  sa          q2:/A8"LMM /&q#...     r0   c                 6    g | ]}t          j        |          S r~   )r>   r?   r  s     r.   r   z(TensorArray.__init__.<locals>.<listcomp>  s     <<<bjmm<<<r0   zExpected a well-typed ndarray or an object-typed ndarray of ndarray pointers, but got an object-typed ndarray whose subndarrays are of type .z8Use the copy() method to create a copy of a TensorArray.zDExpected a numpy.ndarray or sequence of numpy.ndarray, but received z	 of type z	 instead.N)r:   r   r   r   ru   r>   rH   r?   r  rn   rw   object_r   r   r   r<   r   r   _is_variable_shapedr   s     r.   rs   zTensorArray.__init__  s    " fi(( 		@4V<<FF)) 	@   F 5V<<FF 233 	@Xrz&112???Ffbj)) 	| BJ..v;;!##   $     
 =<V<<<F =VDDFF#F37q	??F F F   /& ,, 	VWWWR &R R15f1FR R R   &"*-----#'   r0   NFrn   r  rn   r  c                    |r/t          |t          j                  r|                                }n7t          |t                    r"|r|j                                        n|j        }t	          |          S )aZ  
        Construct a new ExtensionArray from a sequence of scalars.

        Parameters
        ----------
        scalars : Sequence
            Each element will be an instance of the scalar type for this
            array, ``cls.dtype.type`` or be converted into this type in this
            method.
        dtype : dtype, optional
            Construct for this particular dtype. This should be a Dtype
            compatible with the ExtensionArray.
        copy : bool, default False
            If True, copy the underlying data.

        Returns
        -------
        ExtensionArray
        )r:   r>   r  r  r<   r   )r   scalarsrn   r  s       r.   _from_sequencezTensorArray._from_sequence  sp    .  	JJw
33 	JllnnGG-- 	J04Igo**,,,'/G7###r0   originalc                     t           )a  
        Reconstruct an ExtensionArray after factorization.

        Parameters
        ----------
        values : ndarray
            An integer ndarray with the factorized values.
        original : ExtensionArray
            The original ExtensionArray that factorize was called on.

        See Also
        --------
        factorize : Top-level factorize method that dispatches here.
        ExtensionArray.factorize : Encode the extension array as an enumerated
            type.
        r   )r   r;   r!  s      r.   _from_factorizedzTensorArray._from_factorized,  s
    ( "!r0   itemr   )r<   ru   c                    t          |t                    r2| j        |         }t          j        |          r|S t          |          S t          |t                    rNt          |          dk    r;|d         t          k    r*t          |          dk    rt          d          |d         }t          |t                    rt          j        |          }t          | |          }t          | j        |                   S )aA  
        Select a subset of self.

        Parameters
        ----------
        item : int, slice, or ndarray
            * int: The position in 'self' to get.
            * slice: A slice object, where 'start', 'stop', and 'step' are
              integers or None
            * ndarray: A 1-d boolean NumPy ndarray the same length as 'self'

        Returns
        -------
        item : scalar or ExtensionArray

        Notes
        -----
        For scalar ``item``, return a scalar value suitable for the array's
        type. This should be an instance of ``self.dtype.type``.
        For slice ``key``, return an instance of ``ExtensionArray``, even
        if the slice is length 0 or 1.
        For a boolean mask, return an instance of ``ExtensionArray``, filtered
        to the values where ``item`` is True.
        r   r      zCWorkaround Pandas issue #42430 not implemented for tuple length > 2)r:   r   r   r>   r   ru   tupler   Ellipsis
ValueErrorr<   r?   r   )r-   r%  values      r.   __getitem__zTensorArray.__getitem__B  s    : dC   	3L&E{5!! 1)%000 $&& 3t99q==T!W=P=Pt99q==$;   Aw$,, (z$''&tT22Dt|D1222r0   c                 *    t          | j                  S )z]
        Length of this array.

        Returns
        -------
        length : int
        )r   r   rv   s    r.   __len__zTensorArray.__len__u  s     4<   r0   c                     | j         r(| j        d         j        }d| j        d         j        z  }n| j        }| j        dd         }t          ||          S )z2
        An instance of 'ExtensionDtype'.
        r   rY   r   N)r   r   rn   r@   r   r   rm   )r-   rn   rG   s      r.   rn   zTensorArray.dtype  s]    
 " 	) LO)Edl1o22EE$E$QRR(E5%(((r0   c                 P    | j         t          | j                  | _         | j         S )zQ
        Whether this TensorArray holds variable-shaped tensor elements.
        )r  r   r   rv   s    r.   r   zTensorArray.is_variable_shaped  s(    
 #+'I$,'W'WD$''r0   c                     | j         j        S )zL
        The number of bytes needed to store this object in memory.
        )r   nbytesrv   s    r.   r2  zTensorArray.nbytes  s    
 |""r0   c           	      &     j         j        j        t          j        u rAt          j         fdt          t                               D             t                    S  j         j        j        t          j	        u rDt          j
         j         dk    t          t          d j         j                                      S t          j
        t          j         j                   t          t          d j         j                                      S )a  
        A 1-D array indicating if each value is missing.

        Returns
        -------
        na_values : Union[np.ndarray, ExtensionArray]
            In most cases, this should return a NumPy ndarray. For
            exceptional cases like ``SparseArray``, where returning
            an ndarray would be expensive, an ExtensionArray may be
            returned.

        Notes
        -----
        If returning an ExtensionArray, then

        * ``na_values._is_boolean`` should be True
        * `na_values` should implement :func:`ExtensionArray._reduce`
        * ``na_values.any`` and ``na_values.all`` should be implemented
        c                 .    g | ]}j         |         d u S rY   r   )r   ir-   s     r.   r   z$TensorArray.isna.<locals>.<listcomp>  s$    CCCQaD(CCCr0   rn    r   axis)r   rn   rw   r>   r  rH   ranger   boolstr_r   r(  r@   isnanrv   s   `r.   isnazTensorArray.isna  s    ( <"bj00 8CCCC%D		2B2BCCC4    \$//6$,",5q$,BS9T9T3U3UVVVV6&&U5DL<M3N3N-O-O   r0   indices
allow_fill
fill_valuec                 H   |rt          j        |t           j                  }t          |t	          | j                             t          j        |dk               }|r|t           j        }t          j        t	          |          f| j        j	        dd         z   |          }|dk    }t          j
        |t          j        |          d         | j        ||                             t          |          S | j                            |d          }t          |          S )a  
        Take elements from an array.

        Parameters
        ----------
        indices : sequence of int
            Indices to be taken.
        allow_fill : bool, default False
            How to handle negative values in `indices`.

            * False: negative values in `indices` indicate positional indices
              from the right (the default). This is similar to
              :func:`numpy.take`.

            * True: negative values in `indices` indicate
              missing values. These values are set to `fill_value`. Any other
              other negative values raise a ``ValueError``.

        fill_value : any, optional
            Fill value to use for NA-indices when `allow_fill` is True.
            This may be ``None``, in which case the default NA value for
            the type, ``self.dtype.na_value``, is used.

            For many ExtensionArrays, there will be two representations of
            `fill_value`: a user-facing "boxed" scalar, and a low-level
            physical NA value. `fill_value` should be the user-facing version,
            and the implementation should handle translating that to the
            physical version for processing the take if necessary.

        Returns
        -------
        ExtensionArray

        Raises
        ------
        IndexError
            When the indices are out of bounds for the array.
        ValueError
            When `indices` contains negative values other than ``-1``
            and `allow_fill` is True.

        See Also
        --------
        numpy.take : Take elements from an array along an axis.
        api.extensions.take : Take elements from an array.

        Notes
        -----
        ExtensionArray.take is called by ``Series.__getitem__``, ``.loc``,
        ``iloc``, when `indices` is a sequence of values. Additionally,
        it's called by :meth:`Series.reindex`, or any other method
        that causes realignment, with a `fill_value`.

        Examples
        --------
        Here's an example implementation, which relies on casting the
        extension array to object dtype. This uses the helper method
        :func:`pandas.api.extensions.take`.

        .. code-block:: python

           def take(self, indices, allow_fill=False, fill_value=None):
               from pandas.core.algorithms import take

               # If the ExtensionArray is backed by an ndarray, then
               # just pass that here instead of coercing to object.
               data = self.astype(object)

               if allow_fill and fill_value is None:
                   fill_value = self.dtype.na_value

               # fill value should always be translated from the scalar
               # type for the array, to the physical storage type for
               # the data, before passing to take.

               result = take(data, indices, fill_value=fill_value,
                             allow_fill=allow_fill)
               return self._from_sequence(result, dtype=self.dtype)
        r6  r   Nr   r8  )r>   r?   intpr   r   r   r
  nanfullrG   putwherer<   take)r-   r?  r@  rA  has_missingr;   	is_nonnegs          r.   rH  zTensorArray.take  s	   d  	+ j888GWc$,&7&7888 &1--K +%!#J #g,,4<3Eabb3I!I:VV $qL	vrx	2215t|GIDV7WXXX"6*** ""7"336"""r0   c                 N    t          | j                                                  S )ze
        Return a copy of the array.

        Returns
        -------
        ExtensionArray
        )r<   r   r  rv   s    r.   r  zTensorArray.copy,  s      4<,,..///r0   	to_concatc                 *   d}d}|D ]+}||j         j        }|j        s|j         j        |k    rd} n,|r3t          t	          j        d |D             t                              }n+t          t	          j        d |D                                 }|S )z
        Concatenate multiple array of this dtype.

        Parameters
        ----------
        to_concat : sequence of this type

        Returns
        -------
        ExtensionArray
        FNTc                 &    g | ]}|j         D ]}|S r~   r   )r   aes      r.   r   z1TensorArray._concat_same_type.<locals>.<listcomp>N  s'    BBB	BB1!BBBBr0   r6  c                     g | ]	}|j         
S r~   r   )r   rO  s     r.   r   z1TensorArray._concat_same_type.<locals>.<listcomp>Q  s    2P2P2P192P2P2Pr0   )rn   r{   r   r<   r>   rH   objectr   )r   rL  should_flattenrG   rO  concateds         r.   _concat_same_typezTensorArray._concat_same_type7  s      	 	A}-# qw'<'E'E!% (F  	S"BBYBBB&QQQ HH #2>2P2Pi2P2P2P#Q#QRRHr0   keyr+  c                    t          | |          }t          |t                    st          j        |          rt          j        |          }t          |t                    rd |D             }t          |t                    r!t          |j        t                    r|j
        }|(t          |t                    rGt          |          dk    r4t          j        | j        |         t          j                  | j        |<   dS t          |t           t"          t          j        f          r|| j        |<   dS t'          dt)          |           d          )a^  
        Set one or more values inplace.

        This method is not required to satisfy the pandas extension array
        interface.

        Parameters
        ----------
        key : int, ndarray, or slice
            When called from, e.g. ``Series.__setitem__``, ``key`` will be
            one of

            * scalar int
            * ndarray of integers.
            * boolean ndarray
            * slice object

        value : ExtensionDtype.type, Sequence[ExtensionDtype.type], or object
            value or values to be set of ``key``.

        Returns
        -------
        None
        c                 d    g | ]-}t          |t                    rt          j        |          n|.S r~   r  r  s     r.   r   z+TensorArray.__setitem__.<locals>.<listcomp>q  sA       NOA/A!B!BI
1  r0   Nr   z__setitem__ with key type 'z' not implemented)r   r:   ru   r>   r   r?   listr   rn   rm   r;   r   r   	full_liker   rD  r   slicer  r   rw   )r-   rV  r+  s      r.   __setitem__zTensorArray.__setitem__T  s?   2 "$,,e/00 	&BK4F4F 	&Ju%%EeT"" 	 SX  E eY'' 	!Ju{K,P,P 	!LE=Juh77=CJJ!OO "T\#-> G GDLc5"*566 	 %DL%Jd3iiJJJ  r0   c                 F   t          |t                    rkt          j        |          }|j        dk    rLt          j        |                                          r&|                                                                 S t                      
                    |          S )z,
        Return for `item in self`.
        r   )r:   ru   r>   r?   r   r=  r   r>  r
  super__contains__)r-   r%  np_item	__class__s      r.   r_  zTensorArray.__contains__  s     d.// 	)j&&G|q  RXg%6%6%:%:%<%< yy{{(((ww##D)))r0   c                 4    | j                                         S rY   r   rv   s    r.   r   zTensorArray.__repr__  r   r0   c                 4    | j                                         S rY   r   rv   s    r.   r   zTensorArray.__str__  r   r0   c                     t           rY   r#  rv   s    r.   _values_for_factorizez!TensorArray._values_for_factorize  s    !!r0   Tr   skipnac                     dg}i }|D ]}	 ||         ||<   # t           $ r Y w xY w	 t           | j        |         | j        fddi|          S # t           $ r t	          d| d          dw xY w)a  
        Return a scalar result of performing the reduction operation.

        Parameters
        ----------
        name : str
            Name of the function, supported values are:
            { any, all, min, max, sum, mean, median, prod,
            std, var, sem, kurt, skew }.
        skipna : bool, default True
            If True, skip NaN values.
        **kwargs
            Additional keyword arguments passed to the reduction function.
            Currently, `ddof` is the only supported kwarg.

        Returns
        -------
        scalar

        Raises
        ------
        TypeError : subclass does not define reductions
        ddofr9  r   'z' aggregate not implemented.N)KeyErrorru   SUPPORTED_REDUCERSr   r   )r-   r   rf  r  supported_kwargsreducer_kwargskws          r.   _reducezTensorArray._reduce  s    0 #8" 	 	B%+BZr""   	X%-'-dlUUUnUU    	X 	X 	X%&L$&L&L&LMMSWW	Xs   
$$'A A/c                 4    t          j        | j        fd|i|S r  r   r  s      r.   r  zTensorArray.__array__  r  r0   ufuncmethodc                     |                     dd          }||z   D ]7}t          |t          t          j        t
          j        f          s	t          c S 8t          d |D                       }|rt          d |D                       |d<    t          ||          |i |}t          |          t          u rt           fd|D                       S |dk    rdS  t                     |          S )z`
        Supports NumPy ufuncs without requiring sloppy coercion to an
        ndarray.
        outr~   c              3   R   K   | ]"}t          |t                    r|j        n|V  #d S rY   r:   r<   r   r   r\   s     r.   r   z.TensorArray.__array_ufunc__.<locals>.<genexpr>  s7      VV!Jq+$>$>EqyyAVVVVVVr0   c              3   R   K   | ]"}t          |t                    r|j        n|V  #d S rY   rv  rw  s     r.   r   z.TensorArray.__array_ufunc__.<locals>.<genexpr>  sH       " "CDZ;77>		Q" " " " " "r0   c              3   H   K   | ]} t                    |          V  d S rY   )rw   )r   r\   r-   s     r.   r   z.TensorArray.__array_ufunc__.<locals>.<genexpr>  s1      771dA777777r0   atN)getr:   r<   r>   r  numbersNumberr   r(  getattrrw   )r-   rq  rr  inputsr  rt  r\   r   s   `       r.   __array_ufunc__zTensorArray.__array_ufunc__  s(   
 jj### 	& 	&Aa+rz7>!JKK &%%%%& VVvVVVVV 	! " "HK" " "  F5M ('':6::<<5  7777777777t^^4 4::f%%%r0   na_valuec                    |^t           j        j                            |          }|rt	          j        | j        |d          }n>| j                            |          }n#|r| j                                        }n| j        }|S )aj  
        Convert to a NumPy ndarray.

        .. versionadded:: 1.0.0

        This is similar to :meth:`numpy.asarray`, but may provide additional
        control over how the conversion is done.

        Parameters
        ----------
        dtype : str or numpy.dtype, optional
            The dtype to pass to :meth:`numpy.asarray`.
        copy : bool, default False
            Whether to ensure that the returned value is a not a view on
            another array. Note that ``copy=False`` does not *ensure* that
            ``to_numpy()`` is no-copy. Rather, ``copy=True`` ensure that
            a copy is made, even if not strictly necessary.
        na_value : Any, optional
            The value to use for missing values. The default value depends
            on `dtype` and the type of the array.

        Returns
        -------
        numpy.ndarray
        NTr  )	pdapitypespandas_dtyper>   rH   r   astyper  )r-   rn   r  r  r;   s        r.   r   zTensorArray.to_numpy  s    > FL--e44E 4$,e$GGG,,U33 	"\&&((FF\Fr0   c                     | j         j        S r   r   rv   s    r.   r   zTensorArray.numpy_dtype  r   r0   c                     | j         j        S r   r   rv   s    r.   r   zTensorArray.numpy_ndim  r   r0   c                     | j         j        S r   r   rv   s    r.   r   zTensorArray.numpy_shape  r   r0   c                     | j         j        S r   r   rv   s    r.   r   zTensorArray.numpy_size  r   r0   c                 `   t           j        j                            |          }t	          |t
                    r,|r&t          | j                                                  n| }nHt           j        j        	                    |          r$t           j        j        
                    |          sht          j        d | j        D                       }t	          |t           j                  r)|                                                    |d          S |S t           j        j        	                    |          rXt          j        t#          |           t$                    }t'          t#          |                     D ]}| j        |         ||<   n| j                            ||          }|S )a  
        Cast to a NumPy array with 'dtype'.

        Parameters
        ----------
        dtype : str or dtype
            Typecode or data-type to which the array is cast.
        copy : bool, default True
            Whether to copy the data, even if not necessary. If False,
            a copy is made only if the old dtype does not match the
            new dtype.

        Returns
        -------
        array : ndarray
            NumPy ndarray with 'dtype' for its dtype.
        c                 ,    g | ]}t          |          S r~   rZ   )r   ts     r.   r   z&TensorArray.astype.<locals>.<listcomp><  s    <<<!s1vv<<<r0   Fr  r6  )r  r  r  r  r:   rm   r<   r   r  is_object_dtypeis_string_dtyper>   rH   StringDtyper   r   emptyr   rR  r:  r  )r-   rn   r  r;   r5  s        r.   r  zTensorArray.astype#  sr   $ ))%00e[)) 	;9=G[!2!2!4!45554FFFL((//	;46FL4P4PQV4W4W	; X<<t|<<<==F%00 1133BB6PUBVVVV\))%00 	;Xc$iiv666F3t99%% , , LOq		, \((T(::Fr0   c                 b    | j                             |||          }||nt          |          S )ab  
        Test whether any array element along a given axis evaluates to True.

        See numpy.any() documentation for more information
        https://numpy.org/doc/stable/reference/generated/numpy.any.html#numpy.any

        :param axis: Axis or axes along which a logical OR reduction is
            performed.
        :param out: Alternate output array in which to place the result.
        :param keepdims: If this is set to True, the axes which are reduced are
            left in the result as dimensions with size one.
        :return: single boolean unless axis is not None else TensorArray
        r9  rt  keepdims)r   r
  r<   r-   r9  rt  r  r   s        r.   r
  zTensorArray.anyJ  s6     !!tx!HHvv;v+>+>>r0   c                 b    | j                             |||          }||nt          |          S )a  
        Test whether all array elements along a given axis evaluate to True.

        :param axis: Axis or axes along which a logical AND reduction is
            performed.
        :param out: Alternate output array in which to place the result.
        :param keepdims: If this is set to True, the axes which are reduced are
            left in the result as dimensions with size one.
        :return: single boolean unless axis is not None else TensorArray
        r  )r   r   r<   r  s        r.   r   zTensorArray.all[  s6     !!tx!HHvv;v+>+>>r0   c                     ddl m}m} | j        r|                    | j                  S |                    | j                  S )a  
        Convert this TensorArray to an ArrowTensorArray extension array.

        This and TensorDtype.__from_arrow__ make up the
        Pandas extension type + array <--> Arrow extension type + array
        interoperability protocol. See
        https://pandas.pydata.org/pandas-docs/stable/development/extending.html#compatibility-with-apache-arrow
        for more information.
        r   )ArrowTensorArrayArrowVariableShapedTensorArray)$ray.air.util.tensor_extensions.arrowr  r  r   
from_numpyr   )r-   rw   r  r  s       r.   __arrow_array__zTensorArray.__arrow_array__i  sc    	
 	
 	
 	
 	
 	
 	
 	

 " 	=1<<T\JJJ#..t|<<<r0   c                 4    | j                                         S )r   )rn   r   rv   s    r.   r   zTensorArray._is_boolean}  s    " z%%'''r0   )r   r<   )FN)TrY   )NNF)Cr   r   r   r   r>   r	  r   r
  r  r  r  r  r  r  r  rk  r	   r  r   r   ru   r   rs   r   r   r   r;  r   r  r  
extensionsExtensionArrayr$  r   r[  r,  r.  r   ExtensionDtypern   r   r2  r>  rH  r  rU  r\  r_  r   r   r   re  r[   ro  r  r   r  
no_defaultr   r   r   r   r   r  r  r   __classcell__)ra  s   @r.   r<   r<   j  s       S Sl vvvvv)vv 9(JU2:'99:;	
9( 9( 9( 9(v 26U$ $ $ ($>B$ $ $ [$8 "Z"+-6+<+K" " " ["*13#ubj0113	2	313 13 13 13f! ! ! ! ! )rv(7 ) ) ) X) ( ( X( # # # # X#   D SWk# k#}k#26k#LOk#	k# k# k# k#Z	0 	0 	0 	0 (=*A m    [8)uS"*_5 )c )d ) ) ) )V*D * * * * * *' ' '& & &"uRZ_'= " " " "$X $XC $X $X $X $X $XL? ?rx ?RZ ? ? ? ?&X &s & & & &< )4	) )x) ) 	) ) ) )V " " X" ! ! X! " " X" ! ! X!% % % %N? ? ? ?"? ? ? ?= = = =( ( ( X( ( ( ( (r0   r<   sc                     | j         j        t          j        u o,| j         o$t          | j        d         t          j                  S )a  Return whether the provided pandas Series column needs a tensor extension
    representation. This tensor extension representation provides more efficient slicing
    and interop with ML frameworks.

    Args:
        s: The pandas Series column that may need to be represented using the tensor
            extension.

    Returns:
        Whether the provided Series needs a tensor extension representation.
    r   )rn   rw   r>   r  r  r:   ilocr  )r  s    r.   column_needs_tensor_extensionr    s7     	

"X17{Xz!&)RZ7X7Xr0   )Br|  ostypingr   r   r   r   r   r   r	   numpyr>   pandasr  pyarrowr   packaging.versionr
   pandas._typingr   pandas.compatr   pandas.core.dtypes.genericr   r   pandas.core.indexersr   r   $ray.air.util.tensor_extensions.utilsr   r   ray.util.annotationsr   r   ImportErrorr   r[   rM   re   _FORMATTER_ENABLED_ENV_VARgetenv__version__r9   rh   formatter_clsri   _format_stringsr=   _patched_by_ray_datasetsr  r  register_extension_dtyper  rm   ExtensionScalarOpsMixinr   r   ru   r  r<   _add_arithmetic_ops_add_comparison_ops_add_logical_opsSeriesr;  r  r~   r0   r.   <module>r     s  B  				 H H H H H H H H H H H H H H H H H H             % % % % % %             + + + + + + > > > > > > > > F F F F F F F F        + * * * * *E3333333 E E EDDDDDDDDE'.T#Y '. '. '. '.T9>DI 9> 9> 9> 9>x I 29'--44wr~!1!111DDDDDD/EEEEEE0)6)FM&ww772>22EEEEWWW5E5EEEEEE(?%%(F%-1M* V+[* [* [* [* [*"&#2 [* [* ,+ [*|3& 3& 3& 3& 3&bf'? 3& 3& 3&l% % % % % % % %0 V;? ;? ;? ;? ;?*@ ;? ;? ;?| Vc( c( c( c( c(F$c( c( c(P  & & ( ( (  & & ( ( (  # # % % %    ! ! !    ! ! !       VRY 4      s   A A-,A-