
    hJ                       d Z ddlmZ ddlZddlmZ ddlZddlmZm	Z	 ddl
Z
ddlZddlmZmZmZmZmZ ddlmZmZmZmZ ddlmZ dd	lmZmZ dd
lmZm Z m!Z!m"Z"m#Z#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 erddl8m9Z9m:Z: d Z;d Z<ddZ=d d!dZ>d"dZ?d#dZ@ddZAd#dZBd ZCd$dZDej                  e-j                  ej                  e-j                  ej                  e-j                  hZKd%dZLy)&zY
Functions for arithmetic and comparison operations on NumPy arrays and
ExtensionArrays.
    )annotationsN)partial)TYPE_CHECKINGAny)NaT	Timedelta	Timestamplibops)
BaseOffsetget_supported_dtypeis_supported_dtypeis_unitless)find_stack_level)'construct_1d_object_array_from_listlikefind_common_type)ensure_objectis_bool_dtypeis_list_likeis_numeric_v_string_likeis_object_dtype	is_scalar)ABCExtensionArrayABCIndex	ABCSeries)isnanotna)	roperator)expressions)ensure_wrapped_if_datetimelike)missing)should_extension_dispatch)invalid_comparison)	ArrayLikeShapec                    |kt        |       }t        |      }||z  }|j                         r| j                         } || ||z  <   |j                         r|j                         }||||z  <   | |fS )a  
    If a non-None fill_value is given, replace null entries in left and right
    with this value, but only in positions where _one_ of left/right is null,
    not both.

    Parameters
    ----------
    left : array-like
    right : array-like
    fill_value : object

    Returns
    -------
    left : array-like
    right : array-like

    Notes
    -----
    Makes copies if fill_value is not None and NAs are present.
    )r   anycopy)leftright
fill_value	left_mask
right_maskmasks         >D:\jyotish\venv\Lib\site-packages\pandas/core/ops/array_ops.py
fill_binopr0   H   sy    * J	%[
 :%==?99;D%/DT!">>JJLE'1E*t#$;    c                   t        |t              rt        |      }t        |t        j                  t
        t        f      rt        |j                        s|j                  t        j                        }t        |t
        t        f      r|j                  }|j                  |j                  k7  r!t        d|j                  |j                        t        j                  |j!                         |j!                         |       }n%t        j"                  |j!                         ||       }|j%                  |j                        S )NzShapes must match)
isinstancelistr   npndarrayr   r   r   dtypeastypeobject__valuesshape
ValueErrorlibopsvec_compareravelscalar_comparereshape)opxyresults       r/   comp_method_OBJECT_ARRAYrF   q   s    !T3A6!bjj)X67qww'$Aa)X./		A77agg0!''177CC##AGGIqwwy"=&&qwwy!R8>>!''""r1   c                   | j                         }t        |t        j                        rt	        | j
                  |j
                  g      }t        j                  | j                  |      }t        |       t        |      k7  r t        | j                  |j                        t        |      }|j                         }t        |      |j                         z  }|j                         r |||   ||         ||<   nt        |      st        dt        |             t        j                  | j                  | j
                        }t        |      }|t         u rt        j"                  | dk(  d|      }n,|t$        j&                  u rt        j"                  |dk(  d|      }|j                         r |||   |      ||<   t        j(                  || t        j*                         |j-                  | j                        }|S )z
    If the given arithmetic operation fails, attempt it again on
    only the non-null elements of the input array(s).

    Parameters
    ----------
    x : np.ndarray
    y : np.ndarray, Series, Index
    op : binary operator
    r7   z1Cannot broadcast np.ndarray with operand of type    F)r?   r3   r5   r6   r   r7   emptysizelenr<   r;   r   r'   r   	TypeErrortypepowwherer   rpowputmasknanrA   )	rC   rD   rB   xravr7   rE   ymaskyravr.   s	            r/   _masked_arith_oprW      s    779D!RZZ  !''177!34!&&.q6SVQWWagg..a wwyT{U[[]* 88:d4j$t*5F4L |CT!W;O 
 !&&0T{ 988AFE40D9>>!88AFE40D88:d4j!,F4LJJvubff%^^AGG$FMr1   c                r   t        |t              r|}nt        t        j                  |      }	  || |      }|r t        |      s|t        u rt        | ||      S t        j                  || ||      S # t
        $ r; |s5| j                  t        k(  st        |dd      t        k(  rt        | ||      }n Y }w xY w)a  
    Return the result of evaluating op on the passed in values.

    If native types are not compatible, try coercion to object dtype.

    Parameters
    ----------
    left : np.ndarray
    right : np.ndarray or scalar
        Excludes DataFrame, Series, Index, ExtensionArray.
    is_cmp : bool, default False
        If this a comparison operation.

    Returns
    -------
    array-like

    Raises
    ------
    TypeError : invalid operation
    r7   N)r3   strr   r   evaluaterM   r7   objectgetattrrW   r   NotImplementedr#   r!   dispatch_fill_zeros)r)   r*   rB   is_cmpfuncrE   s         r/   _na_arithmetic_opra      s    , %{++R0dE" 9V$.(@ "$r22&&r4??%  
JJ& GE7D$AV$K &dE26F 
s   	A2 2AB65B6c                    t        | |      s#t        |t        t        t        f      s|t
        u r || |      }|S t        || |       t        | ||      }|S )aO  
    Evaluate an arithmetic operation `+`, `-`, `*`, `/`, `//`, `%`, `**`, ...

    Note: the caller is responsible for ensuring that numpy warnings are
    suppressed (with np.errstate(all="ignore")) if needed.

    Parameters
    ----------
    left : np.ndarray or ExtensionArray
    right : object
        Cannot be a DataFrame or Index.  Series is *not* excluded.
    op : {operator.add, operator.sub, ...}
        Or one of the reversed variants from roperator.

    Returns
    -------
    ndarray or ExtensionArray
        Or a 2-tuple of these in the case of divmod or rdivmod.
    )r"   r3   r   r   r	   r   _bool_arith_checkra   )r)   r*   rB   
res_valuess       r/   arithmetic_opre      s_    6 	"$.eiY?@C< e_
  	"dE* 'tUB7
r1   c                   t        |       }t        |      }t        j                  |      }t        |t              rt        j                  |      }t        |t
        j                  t        f      r8t        |      t        |      k7  r!t        d|j                  |j                        t        ||      s6t        |t        t        t        f      s|t         u r|j"                  t$        k7  r |||      }|S t'        |      rkt)        |      r`|t*        j,                  u r't        j.                  |j                  t0              }|S t        j2                  |j                  t0              }|S t5        ||      rt7        |||      S |j"                  t$        k(  st        |t8              rt;        |||      }|S t=        |||d      }|S )a  
    Evaluate a comparison operation `=`, `!=`, `>=`, `>`, `<=`, or `<`.

    Note: the caller is responsible for ensuring that numpy warnings are
    suppressed (with np.errstate(all="ignore")) if needed.

    Parameters
    ----------
    left : np.ndarray or ExtensionArray
    right : object
        Cannot be a DataFrame, Series, or Index.
    op : {operator.eq, operator.ne, operator.gt, operator.ge, operator.lt, operator.le}

    Returns
    -------
    ndarray or ExtensionArray
    zLengths must match to comparerH   T)r_   )r    r
   item_from_zerodimr3   r4   r5   asarrayr6   r   rL   r<   r;   r"   r   r   r	   r   r7   r[   r   r   operatorneonesboolzerosr   r#   rY   rF   ra   )r)   r*   rB   lvaluesrvaluesrd   s         r/   comparison_oprp      s}   & -T2G,U3G##G,G'4  **W%'BJJ(9:; w<3w<'/  !'2	GiY?	@ESLMMV# )
& # 
7	Wd;J  '--t<J  
"'7	3!'7B77	&	 Jw$<-b'7C

  'wDI
r1   c                   	  || |      }|j+                  | j,                        S # t         $ rG t        |t        j                        r~| j                  j
                  dk(  r|j                  j
                  dk(  rJ t        |       } t        |      }t        j                  | j                         |j                         |      }nt        j                  |      sJ t        |      st        |      }	 t        j                  | ||      }ne# t         t        t         t"        t$        f$ rD}t'        |      j(                  }t        d|j(                   d| j                   d| d      |d }~ww xY wY kw xY w)NbzCannot perform 'z' with a dtyped [z] array and scalar of type [])rM   r3   r5   r6   r7   kindr   r=   	vec_binopr?   r
   r   r   rl   scalar_binopr<   AttributeErrorOverflowErrorNotImplementedErrorrN   __name__rA   r;   )rC   rD   rB   rE   errtyps         r/   na_logical_opr}   `  sJ   # Aq8 >>!''""7  a$+0CDDa Aa A%%aggiB?F ==###7G,,Q26#  1g&&&r{{m3DQWWI N++.%q2 s4   	& CE76DE7E0,?E++E00E76E7c                   dd}t        j                  |      }t        |      r;t        |d      s/t	        j
                  dt        t                      t        |      }t        |       }|}t        ||      r |||      }|S t        |t        j                        r$|j                  j                  dv }|s |||      }nt        j                   |      }t#        |||      }| j                  j                  dv r|s ||      }|S )ag  
    Evaluate a logical operation `|`, `&`, or `^`.

    Parameters
    ----------
    left : np.ndarray or ExtensionArray
    right : object
        Cannot be a DataFrame, Series, or Index.
    op : {operator.and_, operator.or_, operator.xor}
        Or one of the reversed variants from roperator.

    Returns
    -------
    ndarray or ExtensionArray
    c                    | j                   j                  dv r5t        |       }|j                         r| j	                  t
              } d| |<   ||j                   j                  dk(  r| j	                  t              } | S )NcfOFrr   )r7   rt   r   r'   r8   r[   rl   )rC   r)   r.   s      r/   	fill_boolzlogical_op.<locals>.fill_bool  sa    77<<5 7DxxzHHV$$<4::??c1Ar1   r7   zLogical ops (and, or, xor) between Pandas objects and dtype-less sequences (e.g. list, tuple) are deprecated and will raise in a future version. Wrap the object in a Series, Index, or np.array before operating instead.)
stackleveliu)N)r
   rg   r   hasattrwarningswarnFutureWarningr   r   r    r"   r3   r5   r6   r7   rt   
is_integerr}   )r)   r*   rB   r   rn   ro   rd   is_other_int_dtypes           r/   
logical_opr     s    " !!%(EE75'#:( ')	
 8> -T2GG '2)
& ! grzz*!(!3!3t!;%#GW5 "%!8"7GR8
 

4',>":.Jr1   c                   t        | t              r| S | j                  j                  d      j	                  d      }|dk(  r| S |dv rt        t
        |       S |dv rt        t        |       S |dv rt        t        |       S t        |      )z
    Return a binary array operation corresponding to the given operator op.

    Parameters
    ----------
    op : function
        Binary operator from operator or roperator module.

    Returns
    -------
    functools.partial
    _rarith_op>   eqgegtleltrj   )rB   >   orandrorxorrandrxor>   addmodmulrO   subdivmodtruedivfloordiv)	r3   r   rz   striplstriprp   r   re   ry   )rB   op_names     r/   get_array_opr     s     "g 	kk$++C0G* 	66},,	?	?zb))	 	 	
 },,!'**r1   c                ^   t        |       t        j                  u rt        |       S t        |       t        j                  u rt	        |       S t        | t        j                        rt        |       rddl	m
} t        | j                        r| j                  d      } n;t        | j                        s&t        | j                        }| j                  |      } t        j                   | |      }|j#                  ||j                        S t	        |       S t        | t        j$                        rt        |       rddl	m} t        | j                        r| j                  d      } n;t        | j                        s&t        | j                        }| j                  |      } t        j                   | |      }|j#                  ||j                        S t        |       S t        | t        j(                        rt+        |       S t        | t        j,                        rt/        |       S | S )ax  
    Cast non-pandas objects to pandas types to unify behavior of arithmetic
    and comparison operations.

    Parameters
    ----------
    obj: object
    shape : tuple[int]

    Returns
    -------
    out : object

    Notes
    -----
    Be careful to call this *after* determining the `name` attribute to be
    attached to the result of the arithmetic operation.
    r   )DatetimeArrayzdatetime64[ns]rH   )TimedeltaArrayztimedelta64[ns])rN   datetime	timedeltar   r	   r3   r5   
datetime64r   pandas.core.arraysr   r   r7   r8   r   r   broadcast_to_simple_newtimedelta64r   integerintfloatingfloat)objr;   r   	new_dtyper*   r   s         r/   maybe_prepare_scalar_for_opr     s   & CyH&&& ~	ch''	'~	C	' 98 399%jj!12'		2/		:	jj+OOC/E ,,U%++,FF~	C	(99 399%jj!23'		2/		:	jj+OOC/E!--e5;;-GG
 ~ 
C	$3x	C	%SzJr1   c                    | t         v rt|j                  j                  dk(  rZt        |      st	        j
                  |      r9| j                  j                  d      j                  d      }t        d| d      yyy)z`
    In contrast to numpy, pandas raises an error for certain operations
    with booleans.
    rr   r   r   z
operator 'z!' not implemented for bool dtypesN)
_BOOL_OP_NOT_ALLOWEDr7   rt   r   r
   is_boolrz   r   r   ry   )rB   arr   r   s       r/   rc   rc   R  su    
 
!!77<<3M!$4Akk'',33C8G%WI%FG  9G "r1   )rC   
np.ndarray)F)r)   r   r_   rl   )r)   r$   r*   r   )r)   r$   r*   r   returnr$   )r;   r%   )r   r   )M__doc__
__future__r   r   	functoolsr   ri   typingr   r   r   numpyr5   pandas._libsr   r   r	   r
   r   r=   pandas._libs.tslibsr   r   r   r   pandas.util._exceptionsr   pandas.core.dtypes.castr   r   pandas.core.dtypes.commonr   r   r   r   r   r   pandas.core.dtypes.genericr   r   r   pandas.core.dtypes.missingr   r   pandas.corer   pandas.core.computationr   pandas.core.constructionr    pandas.core.opsr!   pandas.core.ops.dispatchr"   pandas.core.ops.invalidr#   pandas._typingr$   r%   r0   rF   rW   ra   re   rp   r}   r   r   r   r   rtruedivr   	rfloordivrO   rQ   r   rc    r1   r/   <module>r      s    #        5  

 " / C # > 6&R#(5p0@f-`=@&#RDN(+VJ\ LLNN 
r1   