o
    bi                      @   s   d dl Z d dlmZ d dlmZmZ d dlZd dlmZm	Z	 d dl
mZ e eZdedefdd	Zd
ee defddZdededejfddZd
ee dejfddZdedejfddZdS )    N)datetime)AnyList)create_ragged_ndarrayis_ndarray_like)_truncated_reprcolumn_valuesreturnc                 C   s   t | tpt | tjpt| S )zxCheck whether a UDF column is valid.

    Valid columns must either be a list of elements, or an array-like object.
    )
isinstancelistnpndarrayr   )r    r   T/home/ubuntu/.local/lib/python3.10/site-packages/ray/data/_internal/numpy_support.py_is_valid_column_values   s
   

r   datetime_listc                 C   s   g d}d}| D ]L}t |dd}|dkrd}n&|jdkr)|jd dkr&d}nd}n|jdks8|jdks8|jdkr;d}nd	}||}t||}|t|d
 krT || S q|| S )a   Detect the highest precision for a list of datetime objects.

    Args:
        datetime_list: List of datetime objects.

    Returns:
        A string representing the highest precision among the datetime objects
        ('D', 's', 'ms', 'us', 'ns').
    )Dsmsusnsr   
nanosecondr     r   r   r   r      )getattrmicrosecondsecondminutehourindexmaxlen)r   precision_hierarchyhighest_precision_indexdtr   current_precisioncurrent_indexr   r   r   "_detect_highest_datetime_precision   s(   


r'   r$   	precisionc                 C   s^   |dkr#| j d }t| dd}|| }t| d}|t|| d S t| d| dS )a}  
    Converts a datetime object to a numpy datetime64 object with the specified
    precision.

    Args:
        dt: A datetime object to be converted.
        precision: The desired precision for the datetime64 conversion. Possible
           values are 'D', 's', 'ms', 'us', 'ns'.

    Returns:
        np.datetime64: A numpy datetime64 object with the specified precision.
    r   r   r   r   r   zdatetime64[])r   r   r   
datetime64timedelta64astype)r$   r(   microseconds_as_nsnanosecondstotal_nanosecondsbase_dtr   r   r   _convert_to_datetime64G   s   
r1   c                    s    t |  t fdd| D S )a  Convert a list of datetime objects to a NumPy array of datetime64 with nanosecond
    precision.

    Args:
        datetime_list (List[datetime]): A list of `datetime` objects to be converted.
            Each `datetime` object represents a specific point in time.

    Returns:
        np.ndarray: A NumPy array containing the `datetime64` values of the datetime
           objects from the input list, with the appropriate precision (e.g., nanoseconds,
           microseconds, milliseconds, etc.).
    c                    s   g | ]}t | qS r   )r1   ).0r$   r(   r   r   
<listcomp>u       z4_convert_datetime_to_np_datetime.<locals>.<listcomp>)r'   r   asarray)r   r   r3   r    _convert_datetime_to_np_datetimec   s   r7   c              
   C   s  t | tjr| S t | trt| dkr$t | d tjr$tj| d ddS tdd | D r1t| S zLtdd | D rBdd | D } t }d	}| D ]!}t |tjr[|	|j
|jf qIt |trcd
}qIt|sjd
}qI|sst|dkrxt| W S t| W S  ty } ztjdt|  |d tdt|  d| d|d}~ww t| rzt| W S  ty } ztjdt|  |d tdt|  d| d|d}~ww | S )a  Convert UDF columns (output of map_batches) to numpy, if possible.

    This includes lists of scalars, objects supporting the array protocol, and lists
    of objects supporting the array protocol, such as `[1, 2, 3]`, `Tensor([1, 2, 3])`,
    and `[array(1), array(2), array(3)]`.

    Returns:
        The input as an np.ndarray if possible, otherwise the original input.

    Raises:
        ValueError: If an input was array-like but we failed to convert it to an array.
    r   r   )axisc                 s   s    | ]}t |tV  qd S N)r
   r   )r2   elemr   r   r   	<genexpr>   s    z#convert_to_numpy.<locals>.<genexpr>c                 s   s    | ]}t |V  qd S r9   )r   r2   er   r   r   r;      s    c                 S   s   g | ]}t |qS r   )r   r6   r<   r   r   r   r4      r5   z$convert_to_numpy.<locals>.<listcomp>FTz0Failed to convert column values to numpy array: )exc_infoz1Failed to convert column values to numpy array: (z): .N)r
   r   r   r   r!   expand_dimsallr7   setadddtypeshapebytesisscalarr   r6   	Exceptionloggererrorr   
ValueErrorr   )r   shapes
has_objectr=   r   r   r   convert_to_numpyx   s~   


	
rN   )loggingr   typingr   r   numpyr   $ray.air.util.tensor_extensions.utilsr   r   ray.data._internal.utilr   	getLogger__name__rI   boolr   strr'   r*   r1   r   r7   rN   r   r   r   r   <module>   s    
*