U
    )3gZ                     @   sP  d dl mZmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZAmBZBmCZCmDZDmEZEmFZFmGZGmHZH d dlImJZJ d dlKZKd dlLmMZM d dlNZNd dlOZPd dlOmQZQ d dlRmSZS dd ZTeJd	d
ZUdd ZVdd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dd Z]e]  ddddfddZ^ddfddddZ_d dd!d"d#Z`d$d% Zadfddd&d'Zbdfddd(d)Zcd*ddd+d,d-Zdd.d/ Zed0d1 ZfdS )2    )HFunctionFunctionOptionsFunctionRegistryHashAggregateFunctionHashAggregateKernelKernelScalarAggregateFunctionScalarAggregateKernelScalarFunctionScalarKernelVectorFunctionVectorKernelArraySortOptionsAssumeTimezoneOptionsCastOptionsCountOptionsCumulativeOptionsCumulativeSumOptionsDayOfWeekOptionsDictionaryEncodeOptionsRunEndEncodeOptionsElementWiseAggregateOptionsExtractRegexOptionsFilterOptionsIndexOptionsJoinOptionsListSliceOptionsListFlattenOptionsMakeStructOptionsMapLookupOptionsMatchSubstringOptionsModeOptionsNullOptions
PadOptionsPairwiseOptionsPartitionNthOptionsQuantileOptionsRandomOptionsRankOptionsReplaceSliceOptionsReplaceSubstringOptionsRoundBinaryOptionsRoundOptionsRoundTemporalOptionsRoundToMultipleOptionsScalarAggregateOptionsSelectKOptionsSetLookupOptionsSliceOptionsSortOptionsSplitOptionsSplitPatternOptionsStrftimeOptionsStrptimeOptionsStructFieldOptionsTakeOptionsTDigestOptionsTrimOptionsUtf8NormalizeOptionsVarianceOptionsWeekOptionscall_functionfunction_registryget_functionlist_functionscall_tabular_functionregister_scalar_functionregister_tabular_functionregister_aggregate_functionregister_vector_function
UdfContext
Expression)
namedtupleN)dedent)_compute_docstrings)	docscrapec                 C   s   | j jS )N)_doc	arg_names)func rQ   3/tmp/pip-unpacked-wheel-0fuzq0j6/pyarrow/compute.py_get_arg_namesk   s    rS   _OptionsClassDoc)paramsc                 C   s"   | j s
d S t| j }t|d S )NZ
Parameters)__doc__rM   ZNumpyDocStringrT   )options_classdocrQ   rQ   rR   _scrape_options_class_docr   s    rY   c                 C   s  |j }t|j|j|j|jd| _|| _|| _g }|j	}|sZ|jdkrHdnd}d
|j|}|| d |j}|r|| d tj|j}	|td t|}
|
D ]8}|jd	krd
}nd}|| d| d |d q|d k	rt|}|rH|jD ]@}||j d|j d |jD ]}|d| d q(qnLtd|j dt t|}|j D ] }|td
|j|j qr|td|j d |td |	d k	r|d
t|	d d|| _ | S )N)namearityrW   options_required   	argumentsargumentz,Call compute function {!r} with the given {}z.

z

z.        Parameters
        ----------
        )Zvectorscalar_aggregatez
Array-likezArray-like or scalar-likez : 
z"    Argument to compute function.
z    zOptions class z does not have a docstringz                {0} : optional
                    Parameter for {1} constructor. Either `options`
                    or `{0}` can be passed, but not both at the same time.
                z&            options : pyarrow.compute.zK, optional
                Alternative way of passing options.
            z        memory_pool : pyarrow.MemoryPool, optional
            If not passed, will allocate memory from the default memory pool.
        z
{}
 )!rN   dictrZ   r[   rW   r\   Z__arrow_compute_function____name____qualname__summaryformatappenddescriptionrL   Zfunction_doc_additionsgetrK   rS   kindrY   rU   typedescwarningswarnRuntimeWarninginspect	signature
parametersvaluesstripjoinrV   )wrapperZexposed_namerP   rW   Zcpp_docZ
doc_piecesrf   Zarg_strri   Zdoc_additionrO   Zarg_nameZarg_typeZoptions_class_docpsoptions_sigrQ   rQ   rR   _decorate_compute_functiony   sl     





 
r{   c                 C   sJ   | j j}|sd S zt | W S  tk
rD   td|t Y d S X d S )Nz!Python binding for {} not exposed)rN   rW   globalsKeyErrorrn   ro   rg   rp   )rP   
class_namerQ   rQ   rR   _get_options_class   s    r   c                 C   sl   |s|r(|d k	rt d| |||S |d k	rht|trD|f |S t||rR|S t d| |t|d S )NzMFunction {!r} called with both an 'options' argument and additional argumentsz-Function {!r} expected a {} parameter, got {})	TypeErrorrg   
isinstancerc   rl   )rZ   rW   optionsargskwargsrQ   rQ   rR   _handle_options   s*    



  r   c                    s>   d kr d d fdd
}nd d d fdd
}|S )Nmemory_poolc                    sb    t k	r2t| kr2t d  dt| d|rTt|d trTtt|S |d | S )N takes  positional argument(s), but  were givenr   )Ellipsislenr   r   rI   _calllistcall)r   r   )r[   rP   	func_namerQ   rR   rw      s    z&_make_generic_wrapper.<locals>.wrapper)r   r   c                    s    t k	rLt| k r2t d  dt| d| d  }|d   }nd}t|||}|rt|d trtt||S ||| S )Nr   r   r   rQ   r   )	r   r   r   r   r   rI   r   r   r   )r   r   r   r   Zoption_argsr[   rP   r   rW   rQ   rR   rw      s     rQ   )r   rP   rW   r[   rw   rQ   r   rR   _make_generic_wrapper   s    
r   c                 C   s   ddl m} g }| D ]}||||j q|D ]}||||j q0|d k	rt |}|j D ]6}|j|j	|j
fks~t|r|j|j
d}|| qd||d|j
d d ||d|j
d d t |S )Nr   )	Parameter)rk   r   )defaultr   )rq   r   rh   POSITIONAL_ONLYVAR_POSITIONALrr   rs   rt   rk   POSITIONAL_OR_KEYWORDKEYWORD_ONLYAssertionErrorreplace	Signature)rO   var_arg_namesrW   r   rU   rZ   rz   rx   rQ   rQ   rR   _make_signature  s,    

r   c                 C   sj   t |}t|}|o |d d}|r8| dg}ng }t| |||jd}t||||_t	|| ||S )N*)r[   )
r   rS   
startswithpoplstripr   r[   r   __signature__r{   )rZ   rP   rW   rO   Z
has_varargr   rw   rQ   rQ   rR   _wrap_function#  s        r   c                  C   s   t  } t }ddd}| D ]b}|||}||}|jdkrDq|jdkrZ|jdkrZq|| ksjt|t|| | |< | |< qdS )z
    Make global functions wrapping each compute function.

    Note that some of the automatically-generated wrappers may be overridden
    by custom versions below.
    and_or_)andorZhash_aggregater`   r   N)	r|   r@   rB   rj   rA   rk   r[   r   r   )gregZrewritesZcpp_namerZ   rP   rQ   rQ   rR   _make_global_functions3  s    

r   c                 C   sh   |dk	p|dk	}|r$|dk	r$t d|dkrXtjj|}|dkrNt|}n
t|}td| g||S )a  
    Cast array values to another data type. Can also be invoked as an array
    instance method.

    Parameters
    ----------
    arr : Array-like
    target_type : DataType or str
        Type to cast to
    safe : bool, default True
        Check for overflows or other unsafe conversions
    options : CastOptions, default None
        Additional checks pass by CastOptions
    memory_pool : MemoryPool, optional
        memory pool to use for allocations during function execution.

    Examples
    --------
    >>> from datetime import datetime
    >>> import pyarrow as pa
    >>> arr = pa.array([datetime(2010, 1, 1), datetime(2015, 1, 1)])
    >>> arr.type
    TimestampType(timestamp[us])

    You can use ``pyarrow.DataType`` objects to specify the target type:

    >>> cast(arr, pa.timestamp('ms'))
    <pyarrow.lib.TimestampArray object at ...>
    [
      2010-01-01 00:00:00.000,
      2015-01-01 00:00:00.000
    ]

    >>> cast(arr, pa.timestamp('ms')).type
    TimestampType(timestamp[ms])

    Alternatively, it is also supported to use the string aliases for these
    types:

    >>> arr.cast('timestamp[ms]')
    <pyarrow.lib.TimestampArray object at ...>
    [
      2010-01-01 00:00:00.000,
      2015-01-01 00:00:00.000
    ]
    >>> arr.cast('timestamp[ms]').type
    TimestampType(timestamp[ms])

    Returns
    -------
    casted : Array
        The cast result as a new Array
    NzRMust either pass values for 'target_type' and 'safe' or pass a value for 'options'Fcast)	
ValueErrorpatypeslibensure_typer   Zunsafesafer?   )ZarrZtarget_typer   r   r   Zsafe_vars_passedrQ   rQ   rR   r   S  s    6
r   r   c                C   s   |dk	r.|dk	r"|  ||| } qB|  |} n|dk	rB|  d|} t|tjs`tj|| jd}n | j|jkrtj| | jd}t|d}td| g||}|dk	r| dkrtj| | t	 d}|S )a  
    Find the index of the first occurrence of a given value.

    Parameters
    ----------
    data : Array-like
    value : Scalar-like object
        The value to search for.
    start : int, optional
    end : int, optional
    memory_pool : MemoryPool, optional
        If not passed, will allocate memory from the default memory pool.

    Returns
    -------
    index : int
        the index, or -1 if not found
    Nr   rl   valueindex)
slicer   r   Scalarscalarrl   as_pyr   r?   Zint64)datar   startendr   r   resultrQ   rQ   rR   r     s    
r   T)boundscheckr   c                C   s   t |d}td| |g||S )a  
    Select values (or records) from array- or table-like data given integer
    selection indices.

    The result will be of the same type(s) as the input, with elements taken
    from the input array (or record batch / table fields) at the given
    indices. If an index is null then the corresponding value in the output
    will be null.

    Parameters
    ----------
    data : Array, ChunkedArray, RecordBatch, or Table
    indices : Array, ChunkedArray
        Must be of integer type
    boundscheck : boolean, default True
        Whether to boundscheck the indices. If False and there is an out of
        bounds index, will likely cause the process to crash.
    memory_pool : MemoryPool, optional
        If not passed, will allocate memory from the default memory pool.

    Returns
    -------
    result : depends on inputs
        Selected values for the given indices

    Examples
    --------
    >>> import pyarrow as pa
    >>> arr = pa.array(["a", "b", "c", None, "e", "f"])
    >>> indices = pa.array([0, None, 4, 3])
    >>> arr.take(indices)
    <pyarrow.lib.StringArray object at ...>
    [
      "a",
      null,
      "e",
      null
    ]
    )r   take)r9   r?   )r   indicesr   r   r   rQ   rQ   rR   r     s    (
r   c                 C   sV   t |tjtjtjfs(tj|| jd}n | j|jkrHtj| | jd}td| |gS )ae  Replace each null element in values with a corresponding
    element from fill_value.

    If fill_value is scalar-like, then every null element in values
    will be replaced with fill_value. If fill_value is array-like,
    then the i-th element in values will be replaced with the i-th
    element in fill_value.

    The fill_value's type must be the same as that of values, or it
    must be able to be implicitly casted to the array's type.

    This is an alias for :func:`coalesce`.

    Parameters
    ----------
    values : Array, ChunkedArray, or Scalar-like object
        Each null element is replaced with the corresponding value
        from fill_value.
    fill_value : Array, ChunkedArray, or Scalar-like object
        If not same type as values, will attempt to cast.

    Returns
    -------
    result : depends on inputs
        Values with all null elements replaced

    Examples
    --------
    >>> import pyarrow as pa
    >>> arr = pa.array([1, 2, None, 3], type=pa.int8())
    >>> fill_value = pa.scalar(5, type=pa.int8())
    >>> arr.fill_null(fill_value)
    <pyarrow.lib.Int8Array object at ...>
    [
      1,
      2,
      5,
      3
    ]
    >>> arr = pa.array([1, 2, None, 4, None])
    >>> arr.fill_null(pa.array([10, 20, 30, 40, 50]))
    <pyarrow.lib.Int64Array object at ...>
    [
      1,
      2,
      30,
      4,
      50
    ]
    r   Zcoalesce)	r   r   ArrayChunkedArrayr   r   rl   r   r?   )rt   Z
fill_valuerQ   rQ   rR   	fill_null  s
    3r   c                C   sR   |dkrg }t | tjtjfr*|d ntdd |}t||}td| g||S )a  
    Select the indices of the top-k ordered elements from array- or table-like
    data.

    This is a specialization for :func:`select_k_unstable`. Output is not
    guaranteed to be stable.

    Parameters
    ----------
    values : Array, ChunkedArray, RecordBatch, or Table
        Data to sort and get top indices from.
    k : int
        The number of `k` elements to keep.
    sort_keys : List-like
        Column key names to order by when input is table-like data.
    memory_pool : MemoryPool, optional
        If not passed, will allocate memory from the default memory pool.

    Returns
    -------
    result : Array
        Indices of the top-k ordered elements

    Examples
    --------
    >>> import pyarrow as pa
    >>> import pyarrow.compute as pc
    >>> arr = pa.array(["a", "b", "c", None, "e", "f"])
    >>> pc.top_k_unstable(arr, k=3)
    <pyarrow.lib.UInt64Array object at ...>
    [
      5,
      4,
      2
    ]
    N)dummy
descendingc                 S   s   | dfS )Nr   rQ   Zkey_namerQ   rQ   rR   <lambda>O      z top_k_unstable.<locals>.<lambda>select_k_unstabler   r   r   r   rh   mapr0   r?   rt   k	sort_keysr   r   rQ   rQ   rR   top_k_unstable%  s    %
r   c                C   sR   |dkrg }t | tjtjfr*|d ntdd |}t||}td| g||S )a  
    Select the indices of the bottom-k ordered elements from
    array- or table-like data.

    This is a specialization for :func:`select_k_unstable`. Output is not
    guaranteed to be stable.

    Parameters
    ----------
    values : Array, ChunkedArray, RecordBatch, or Table
        Data to sort and get bottom indices from.
    k : int
        The number of `k` elements to keep.
    sort_keys : List-like
        Column key names to order by when input is table-like data.
    memory_pool : MemoryPool, optional
        If not passed, will allocate memory from the default memory pool.

    Returns
    -------
    result : Array of indices
        Indices of the bottom-k ordered elements

    Examples
    --------
    >>> import pyarrow as pa
    >>> import pyarrow.compute as pc
    >>> arr = pa.array(["a", "b", "c", None, "e", "f"])
    >>> pc.bottom_k_unstable(arr, k=3)
    <pyarrow.lib.UInt64Array object at ...>
    [
      0,
      1,
      2
    ]
    N)r   	ascendingc                 S   s   | dfS )Nr   rQ   r   rQ   rQ   rR   r   ~  r   z#bottom_k_unstable.<locals>.<lambda>r   r   r   rQ   rQ   rR   bottom_k_unstableT  s    %
r   system)initializerr   r   c                C   s   t |d}tdg ||| dS )aB  
    Generate numbers in the range [0, 1).

    Generated values are uniformly-distributed, double-precision
    in range [0, 1). Algorithm and seed can be changed via RandomOptions.

    Parameters
    ----------
    n : int
        Number of values to generate, must be greater than or equal to 0
    initializer : int or str
        How to initialize the underlying random generator.
        If an integer is given, it is used as a seed.
        If "system" is given, the random generator is initialized with
        a system-specific source of (hopefully true) randomness.
        Other values are invalid.
    options : pyarrow.compute.RandomOptions, optional
        Alternative way of passing options.
    memory_pool : pyarrow.MemoryPool, optional
        If not passed, will allocate memory from the default memory pool.
    )r   random)length)r'   r?   )nr   r   r   rQ   rQ   rR   r     s    
r   c                  G   sr   t | }|dkrdt| d ttfr0t| d S t| d trLt| d S tdt	| d  n
t| S dS )a  Reference a column of the dataset.

    Stores only the field's name. Type and other information is known only when
    the expression is bound to a dataset having an explicit scheme.

    Nested references are allowed by passing multiple names or a tuple of
    names. For example ``('foo', 'bar')`` references the field named "bar"
    inside the field named "foo".

    Parameters
    ----------
    *name_or_index : string, multiple strings, tuple or int
        The name or index of the (possibly nested) field the expression
        references to.

    Returns
    -------
    field_expr : Expression
        Reference to the given field

    Examples
    --------
    >>> import pyarrow.compute as pc
    >>> pc.field("a")
    <pyarrow.compute.Expression a>
    >>> pc.field(1)
    <pyarrow.compute.Expression FieldPath(1)>
    >>> pc.field(("a", "b"))
    <pyarrow.compute.Expression FieldRef.Nested(FieldRef.Name(a) ...
    >>> pc.field("a", "b")
    <pyarrow.compute.Expression FieldRef.Nested(FieldRef.Name(a) ...
    r]   r   zCfield reference should be str, multiple str, tuple or integer, got N)
r   r   strintrI   Z_fieldtupleZ_nested_fieldr   rl   )Zname_or_indexr   rQ   rQ   rR   field  s    !r   c                 C   s
   t | S )aB  Expression representing a scalar value.

    Parameters
    ----------
    value : bool, int, float or string
        Python value of the scalar. Note that only a subset of types are
        currently supported.

    Returns
    -------
    scalar_expr : Expression
        An Expression representing the scalar value
    )rI   Z_scalarr   rQ   rQ   rR   r     s    r   )gZpyarrow._computer   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rG   rH   rI   collectionsrJ   rq   textwraprK   rn   Zpyarrowr   rL   Zpyarrow.vendoredrM   rS   rT   rY   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rQ   rQ   rQ   rR   <module>   s8    )O
SE&,;//1