
    h                        d Z ddlmZ ddlZ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mZ ddlmZ er
ddlmZmZmZ dd	Z	 	 	 	 	 	 	 	 	 	 dd
Z	 	 	 	 	 	 	 	 ddZy)z4
Methods used by Block.replace and related methods.
    )annotationsN)Pattern)TYPE_CHECKINGAny)is_boolis_reis_re_compilable)isna)	ArrayLikeScalarnptc                    t        |      rd} | xr t        |      } | xr" t        j                  |      j                  dk7  } | S )zG
    Decide whether to treat `to_replace` as a regular expression.
    T )r   r	   recompilepattern)regex
to_replaces     DD:\jyotish\venv\Lib\site-packages\pandas/core/array_algos/replace.pyshould_use_regexr      sF     Z2&z2E :bjj,44:EL    c                   t              r| S 	 	 	 	 	 	 dd}|rt        |      sfd}nt        j                  fd      }t	        | t        j
                        rr|p| |   }  ||       }t	        |t        j
                        rQt        j                  |j                  t        j                        }t        j                  |||       |}n ||       } |||        |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
    c           	        t        |       rt        |t        j                        rft	        |      j
                  t	        |      j
                  g}d|j                   d|d<   t        dt        |d          dt        |d                yy)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)resultab
type_namess       r   _check_comparison_typesz8compare_or_regex_search.<locals>._check_comparison_typesD   s     6?z!RZZ8q'**DG,<,<=J,QWWIQ7JqM'Z](;'<E$zRS}BUAVW   9?r   c                0    t        j                  |       S N)operatoreqxr&   s    r   <lambda>z)compare_or_regex_search.<locals>.<lambda>V   s    x{{1a(r   c                    t        | t              r5t        t        t        f      rt        t	        j
                  |             S dS )NF)r   strr   boolr   searchr-   s    r   r/   z)compare_or_regex_search.<locals>.<lambda>Y   s;    !S!jS'N&C 299Q?+ r   )r!   )r$   zArrayLike | boolr%   r   r&   Scalar | Pattern)
r
   r   r   	vectorizer   r   zerosshapebool_place)r%   r&   r   maskr(   opr$   tmps    `      r   compare_or_regex_searchr=   -   s    ( Awu %.3C  (2(\\
 !RZZ T%5dGAfbjj) ((4::RXX6CHHS$'FAFAq)Mr   c                N   t              st        t              sfd}nfd}t        j                  |t        j
                  g      }| ||       | dd y| j                  |j                  k7  r t        j                  || j                        } || |         | |<   y)z
    Parameters
    ----------
    values : ArrayLike
        Object dtype.
    rx : re.Pattern
    value : Any
    mask : np.ndarray[bool], optional

    Notes
    -----
    Alters values in-place.
    c                h    t              r%t        | t              rj                  |       S | S | S r*   )r   r   r1   r3   srxvalues    r   re_replacerz"replace_regex.<locals>.re_replacer   s0    RyZ3/ "		! 8u?a?r   c                b    t              r"t        | t              rj                  |       S | S r*   )r   r   r1   subr@   s    r   rD   z"replace_regex.<locals>.re_replacer   s)    RyZ3/vveQ''r   )otypesN)	r
   r   r1   r   r5   object_ndimbroadcast_tor7   )valuesrB   rC   r:   rD   fs    ``   r   replace_regexrM   p   s~    & E{*UC0		 	["**6A|fIq	;;$))#??46Dtr   )r   r2   r   r   returnr2   )
r%   r   r&   r4   r   r2   r:   znpt.NDArray[np.bool_]rN   r   )rK   r   rB   z
re.Patternr:   znpt.NDArray[np.bool_] | NonerN   None)__doc__
__future__r   r+   r   r   typingr   r   numpyr   pandas.core.dtypes.commonr   r   r	   pandas.core.dtypes.missingr
   pandas._typingr   r   r   r   r=   rM    r   r   <module>rX      s    #  	 
  
 , @@%@.2@:O@@F+'+'%+'4P+'	+'r   