o
    5c                     @  s   d Z ddlmZ ddlZddlZddlmZmZ ddlZ	ddl
mZmZmZ ddlmZmZmZmZmZ ddlmZ dddZd ddZd!ddZdS )"z4
Methods used by Block.replace and related methods.
    )annotationsN)AnyPattern)	ArrayLikeScalarnpt)is_datetimelike_v_numericis_numeric_v_string_likeis_reis_re_compilable	is_scalar)isnaregexbool
to_replacer   returnc                 C  s0   t |rd} | ot|} | ot|jdk} | S )zG
    Decide whether to treat `to_replace` as a regular expression.
    T )r
   r   recompilepattern)r   r    r   U/var/www/html/gps/gps/lib/python3.10/site-packages/pandas/core/array_algos/replace.pyshould_use_regex   s
   r   ar   bScalar | Patternmasknpt.NDArray[np.bool_]ArrayLike | boolc                   s   t  r| S ddd}|rt| s fd	d
}n	t fdd
}t| tjr-| | } t|  r:tj| jt	dS t
|  rG|d|   dS || }t|tjrg|durgtj|jtjd}t||| |}|||   |S )a  
    Compare two array-like inputs of the same shape or two scalar values

    Calls operator.eq or re.search, depending on regex argument. If regex is
    True, perform an element-wise regex matching.

    Parameters
    ----------
    a : array-like
    b : scalar or regex pattern
    regex : bool
    mask : np.ndarray[bool]

    Returns
    -------
    mask : array-like of bool
    resultr   r   r   r   r   c                 S  sf   t | r/t|tjr1t|jt|jg}d|j d|d< tdt|d  dt|d  dS dS )z
        Raises an error if the two arrays (a,b) cannot be compared.
        Otherwise, returns the comparison result as expected.
        zndarray(dtype=)r   zCannot compare types z and    N)	r   
isinstancenpndarraytype__name__dtype	TypeErrorrepr)r   r   r   
type_namesr   r   r   _check_comparison_typesD   s   z8compare_or_regex_search.<locals>._check_comparison_typesc                   s   t |  S N)operatoreqxr   r   r   <lambda>V   s    z)compare_or_regex_search.<locals>.<lambda>c                   s,   t | trt  ttfrtt | S dS )NF)r"   strr   r   r   searchr/   r1   r   r   r2   Y   s
   )r'   FN)r   r   r   r   r   r   )r   r   r#   	vectorizer"   r$   r	   zerosshaper   r   bool_place)r   r   r   r   r+   opr   tmpr   r1   r   compare_or_regex_search-   s,   



r<   valuesrx
re.Patternnpt.NDArray[np.bool_] | NoneNonec                   sr   t s	tts fdd}n fdd}tj|tjgd}|du r/|| | dd< dS || | | |< dS )z
    Parameters
    ----------
    values : ArrayLike
        Object dtype.
    rx : re.Pattern
    value : Any
    mask : np.ndarray[bool], optional

    Notes
    -----
    Alters values in-place.
    c                   s,   t  rt| tr | d urS | S | S r,   )r
   r"   r3   r4   sr>   valuer   r   re_replacer   s   z"replace_regex.<locals>.re_replacerc                   s"   t  rt| tr | S | S r,   )r
   r"   r3   subrB   rD   r   r   rF      s   )otypesN)r   r"   r3   r#   r5   object_)r=   r>   rE   r   rF   fr   rD   r   replace_regexx   s   	rK   )r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   )r=   r   r>   r?   r   r@   r   rA   )__doc__
__future__r   r-   r   typingr   r   numpyr#   pandas._typingr   r   r   pandas.core.dtypes.commonr   r	   r
   r   r   pandas.core.dtypes.missingr   r   r<   rK   r   r   r   r   <module>   s    

K