
    &`i0              	       2   d dl Z d dlmZ d dlmZmZmZmZ d dlZ	d dl
mZ d dlmZ d dlmZmZ erd dlZ	 d dlZn# e$ r dZY nw xY wdad Ze G d d	ee                      Ze G d
 dee                      Z	 ddededdfdZ	 ddddededefdZe	 ddedefd            Ze	 ddddedefd            Zdedee	j        eee	j        f         f         fdZ de	j        dedee	j                 f         fdZ!de	j        de	j        fdZ"ddZ#ddZ$dS )     N)Enum)TYPE_CHECKINGDictListUnion)TENSOR_COLUMN_NAME)DataBatchType)
DeprecatedDeveloperAPIc                  *    t           dd l} | a t           S )Nr   )_pandaspandas)r   s    v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/air/util/data_batch_conversion.py_lazy_import_pandasr      s    N    c                       e Zd ZdZdZdZdS )BatchFormatr   arrownumpyN)__name__
__module____qualname__PANDASARROWNUMPY r   r   r   r   "   s        FEEEEr   r   c                       e Zd ZdZdZdZdZdS )BlockFormatz#Internal Dataset block format enum.r   r   simpleN)r   r   r   __doc__r   r   SIMPLEr   r   r   r   r   *   s#        --FEFFFr   r   Fdatacast_tensor_columnsreturnpd.DataFramec                    t                      }t          | t          j                  r& |j        t
          t          |           i          } nt          | t                    r|i }|                                 D ]T\  }}t          |t          j                  s#t          dt          |           d| d          t          |          ||<   U |j        |          } nrt          /t          | t          j                  r|                                 } n<t          | |j                  s't          dt          |            dt                     |rt          |           } | S )a  Convert the provided data to a Pandas DataFrame.

    Args:
        data: Data of type DataBatchType
        cast_tensor_columns: Whether tensor columns should be cast to NumPy ndarrays.

    Returns:
        A pandas Dataframe representation of the input data.

    GAll values in the provided dict must be of type np.ndarray. Found type 	 for key 	 instead.NReceived data of type: , but expected it to be one of )r   
isinstancenpndarray	DataFramer   _ndarray_to_columndictitems
ValueErrortypepyarrowTable	to_pandasr	    _cast_tensor_columns_to_ndarrays)r"   r#   pdtensor_dictcol_namecols         r   _convert_batch_type_to_pandasr=   3   s    
		B$
## 
r|/1CD1I1IJKK	D$		 
!ZZ\\ 	< 	<MHcc2:..   .23ii   BJ       
 %7s$;$;K!!r|K((		D'-!@!@	~~bl++ 
"d4jj " "" "
 
 	
  6/55Kr   r4   c                    |rt          |           } |t          j        k    r| S |t          j        k    rat	          | j                  dk    r#| j        dddf                                         S i }| D ]}| |                                         ||<    |S |t          j        k    r5t          st          d          t          j                            |           S t          d| dt                     )`  Convert the provided Pandas dataframe to the provided ``type``.

    Args:
        data: A Pandas DataFrame
        type: The specific ``BatchFormat`` to convert to.
        cast_tensor_columns: Whether tensor columns should be cast to our tensor
            extension type.

    Returns:
        The input data represented with the provided type.
       Nr   z|Attempted to convert data to Pyarrow Table but Pyarrow is not installed. Please do `pip install pyarrow` to install Pyarrow.zReceived type r+   ))_cast_ndarray_columns_to_tensor_extensionr   r   r   lencolumnsilocto_numpyr   r5   r3   r6   from_pandasr	   )r"   r4   r#   output_dictcolumns        r   _convert_pandas_to_batch_typerI   \   s      ?8>>{!!!	"	"	"t|!!9QQQT?++--- K > >&*6l&;&;&=&=F##	"	"	" 	#  
 }((... QTQQ-QQ
 
 	
r   c                 X    t          j        dt                     t          | |          S )a5  Convert the provided data to a Pandas DataFrame.

    This API is deprecated from Ray 2.4.

    Args:
        data: Data of type DataBatchType
        cast_tensor_columns: Whether tensor columns should be cast to NumPy ndarrays.

    Returns:
        A pandas Dataframe representation of the input data.

    z`convert_batch_type_to_pandas` is deprecated as a developer API starting from Ray 2.4. All batch format conversions should be done manually instead of relying on this API.r"   r#   )warningswarnPendingDeprecationWarningr=   rK   s     r   convert_batch_type_to_pandasrO      sA    " M	8 	"	   )':   r   c                 Z    t          j        dt                     t          | ||          S )r?   z`convert_pandas_to_batch_type` is deprecated as a developer API starting from Ray 2.4. All batch format conversions should be done manually instead of relying on this API.r"   r4   r#   )rL   rM   rN   rI   rQ   s      r   convert_pandas_to_batch_typerR      sC    " M	8 	"	   )2E   r   c                    t                      }t          | t          j                  r| S t          | t                    rY|                                 D ]B\  }}t          |t          j                  s#t          dt          |           d| d          C| S t          t          | t          j	                  rddl
m} ddlm} g }| j        D ]A}|                    |          }|                    |                    |d	                     B |            }| j        t&          gk    r(t          | j        j        d         |          r|d         S t	          t-          | j        |                    S t          | |j                  rt1          | t2          j                  S t          d
t          |            dt6                     )zConvert the provided data to a NumPy ndarray or dict of ndarrays.

    Args:
        data: Data of type DataBatchType

    Returns:
        A numpy representation of the input data.
    r'   r(   r)   Nr   ),get_arrow_extension_fixed_shape_tensor_types)transform_pyarrowF)zero_copy_onlyr*   r+   )r   r,   r-   r.   r1   r2   r3   r4   r5   r6   $ray.air.util.tensor_extensions.arrowrT   ray.data._internal.arrow_opsrU   rC   combine_chunked_arrayappendrE   column_namesr   schematypeszipr/   rI   r   r   r	   )	r"   r9   r;   r<   rT   rU   column_values_ndarrayscombined_arrayarrow_fixed_shape_tensor_typess	            r   _convert_batch_type_to_numpyrb      s#    
		B$
## +
	D$		 )
!ZZ\\ 	 	MHcc2:..   .23ii   BJ        		D'-!@!@		
 	
 	
 	
 	
 	
 	CBBBBB!#< 	 	C /DDSIIN"))!**>%*PP    *V)U)W)W& !3 444t{(+-KLL 5 *!,,C)+ABBCCC	D",	'	' 
,T;3DEEE"d4jj " "" "
 
 	
r   arrz	pd.Seriesc                 ~    t                      }	  |j        |           S # t          $ r t          |           cY S w xY w)zConvert a NumPy ndarray into an appropriate column format for insertion into a
    pandas DataFrame.

    If conversion to a pandas Series fails (e.g. if the ndarray is multi-dimensional),
    fall back to a list of NumPy ndarrays.
    )r   Seriesr3   list)rc   r9   s     r   r0   r0      sS     
		B ry~~   Cyys     <<c                     | j         j        t          j        u r0	 t          j        d | D                       } n# t
          $ r Y nw xY w| S )zUnwrap an object-dtyped NumPy ndarray containing ndarray pointers into a single
    contiguous ndarray, if needed/possible.
    c                 6    g | ]}t          j        |          S r   )r-   asarray).0vs     r   
<listcomp>z9_unwrap_ndarray_object_type_if_needed.<locals>.<listcomp>  s     777aBJqMM777r   )dtyper4   r-   object_array	Exception)rc   s    r   %_unwrap_ndarray_object_type_if_neededrq     sa     y~##	(77377788CC 	 	 	D	 Js   9 
AAdfc                    t                      }	 |j        j        j        }n# t          $ r |j        j        }Y nw xY wddlm}m} | 	                                D ]\  }} ||          r	 t          j                    5  t          j        dt                     t          j        d|            ||          | |<   ddd           n# 1 swxY w Y   |# t          $ r}t          d| d          |d}~ww xY w| S )zY
    Cast all NumPy ndarray columns in df to our tensor extension type, TensorArray.
    r   )TensorArraycolumn_needs_tensor_extensionignorecategoryNzTried to cast column z to the TensorArray tensor extension type but the conversion failed. To disable automatic casting to this tensor extension, set ctx = DataContext.get_current(); ctx.enable_tensor_extension_casting = False.)r   corecommonSettingWithCopyWarningAttributeErrorerrors%ray.air.util.tensor_extensions.pandasrt   ru   r2   rL   catch_warningssimplefilterFutureWarningrp   r3   )rr   r9   r{   rt   ru   r;   r<   es           r   rA   rA     s    
		BB!#!F B B B!#!AB          #((-- 	
 ,.. 4 4)(]KKKK)(=STTTT#.;s#3#3BxL4 4 4 4 4 4 4 4 4 4 4 4 4 4 4     CH C C C  	$ IsI   " ;;+C>A C
>C
C	CC	C
C9 C44C9c                    t                      }	 |j        j        j        }n# t          $ r |j        j        }Y nw xY wddlm} |                                 D ]\  }}t          |j
        |          rt          j                    5  t          j        dt                     t          j        d|           t          |                                          | |<   ddd           n# 1 swxY w Y   | S )z:Cast all tensor extension columns in df to NumPy ndarrays.r   )TensorDtyperv   rw   N)r   ry   rz   r{   r|   r}   r~   r   r2   r,   rm   rL   r   r   r   rf   rE   )rr   r9   r{   r   r;   r<   s         r   r8   r8   I  sM   			BB!#!F B B B!#!AB BAAAAA  	4 	4#ci-- 	4
 (** 4 4%hGGGG%h9OPPPP#CLLNN3384 4 4 4 4 4 4 4 4 4 4 4 4 4 4 Is   " ;;AC''C+	.C+	)F)rr   r%   r$   r%   )%rL   enumr   typingr   r   r   r   r   r-   ray.air.constantsr   ray.air.data_batch_typer	   ray.util.annotationsr
   r   r   r9   r5   ImportErrorr   r   strr   r   boolr=   rI   rO   rR   r.   rb   r0   rq   rA   r8   r   r   r   <module>r      s          3 3 3 3 3 3 3 3 3 3 3 3     0 0 0 0 0 0 1 1 1 1 1 1 9 9 9 9 9 9 9 9 NNNN   GGG
        #t        #t    !&& &
&& & & & &X !&,
 ,

,

,
 ,
 	,
 ,
 ,
 ,
^  !& 
   6  !& 

    68

8

2:tCO,,-8
 8
 8
 8
vBJ 5d2:>N1N+O     rz bj    * * * *Z     s   ; AA