U
    MZfD                     @  s   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	 ddl
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ dddddZG dd dZdddddddZddddZdddddZddd d!Zdddd"d#Zdddd$d%ZdS )&zn
Methods that can be shared by many array-like classes or subclasses:
    Series
    Index
    ExtensionArray
    )annotationsN)Any)lib)!maybe_dispatch_ufunc_to_dunder_op)
ABCNDFrame)	roperatorextract_array)unpack_zerodim_and_defermaxminsumprod)maximumZminimumaddmultiplyc                   @  s  e Zd Zdd Zeddd Zeddd Zed	d
d Zeddd Zeddd Z	eddd Z
dd Zeddd Zeddd Zeddd Zed d!d" Zed#d$d% Zed&d'd( Zd)d* Zed+d,d- Zed.d/d0 Zed1d2d3 Zed4d5d6 Zed7d8d9 Zed:d;d< Zed=d>d? Zed@dAdB ZedCdDdE ZedFdGdH ZedIdJdK ZedLdMdN ZedOdPdQ ZedRdSdT Z edUdVdW Z!edXdYdZ Z"d[S )\OpsMixinc                 C  s   t S NNotImplementedselfotherop r   9/tmp/pip-unpacked-wheel-nbcvw55c/pandas/core/arraylike.py_cmp_method#   s    zOpsMixin._cmp_method__eq__c                 C  s   |  |tjS r   )r   operatoreqr   r   r   r   r   r   &   s    zOpsMixin.__eq____ne__c                 C  s   |  |tjS r   )r   r   ner    r   r   r   r!   *   s    zOpsMixin.__ne____lt__c                 C  s   |  |tjS r   )r   r   ltr    r   r   r   r#   .   s    zOpsMixin.__lt____le__c                 C  s   |  |tjS r   )r   r   ler    r   r   r   r%   2   s    zOpsMixin.__le____gt__c                 C  s   |  |tjS r   )r   r   gtr    r   r   r   r'   6   s    zOpsMixin.__gt____ge__c                 C  s   |  |tjS r   )r   r   ger    r   r   r   r)   :   s    zOpsMixin.__ge__c                 C  s   t S r   r   r   r   r   r   _logical_methodA   s    zOpsMixin._logical_method__and__c                 C  s   |  |tjS r   )r+   r   and_r    r   r   r   r,   D   s    zOpsMixin.__and____rand__c                 C  s   |  |tjS r   )r+   r   Zrand_r    r   r   r   r.   H   s    zOpsMixin.__rand____or__c                 C  s   |  |tjS r   )r+   r   or_r    r   r   r   r/   L   s    zOpsMixin.__or____ror__c                 C  s   |  |tjS r   )r+   r   Zror_r    r   r   r   r1   P   s    zOpsMixin.__ror____xor__c                 C  s   |  |tjS r   )r+   r   xorr    r   r   r   r2   T   s    zOpsMixin.__xor____rxor__c                 C  s   |  |tjS r   )r+   r   Zrxorr    r   r   r   r4   X   s    zOpsMixin.__rxor__c                 C  s   t S r   r   r   r   r   r   _arith_method_   s    zOpsMixin._arith_method__add__c                 C  s   |  |tjS )a/  
        Get Addition of DataFrame and other, column-wise.

        Equivalent to ``DataFrame.add(other)``.

        Parameters
        ----------
        other : scalar, sequence, Series, dict or DataFrame
            Object to be added to the DataFrame.

        Returns
        -------
        DataFrame
            The result of adding ``other`` to DataFrame.

        See Also
        --------
        DataFrame.add : Add a DataFrame and another object, with option for index-
            or column-oriented addition.

        Examples
        --------
        >>> df = pd.DataFrame({'height': [1.5, 2.6], 'weight': [500, 800]},
        ...                   index=['elk', 'moose'])
        >>> df
               height  weight
        elk       1.5     500
        moose     2.6     800

        Adding a scalar affects all rows and columns.

        >>> df[['height', 'weight']] + 1.5
               height  weight
        elk       3.0   501.5
        moose     4.1   801.5

        Each element of a list is added to a column of the DataFrame, in order.

        >>> df[['height', 'weight']] + [0.5, 1.5]
               height  weight
        elk       2.0   501.5
        moose     3.1   801.5

        Keys of a dictionary are aligned to the DataFrame, based on column names;
        each value in the dictionary is added to the corresponding column.

        >>> df[['height', 'weight']] + {'height': 0.5, 'weight': 1.5}
               height  weight
        elk       2.0   501.5
        moose     3.1   801.5

        When `other` is a :class:`Series`, the index of `other` is aligned with the
        columns of the DataFrame.

        >>> s1 = pd.Series([0.5, 1.5], index=['weight', 'height'])
        >>> df[['height', 'weight']] + s1
               height  weight
        elk       3.0   500.5
        moose     4.1   800.5

        Even when the index of `other` is the same as the index of the DataFrame,
        the :class:`Series` will not be reoriented. If index-wise alignment is desired,
        :meth:`DataFrame.add` should be used with `axis='index'`.

        >>> s2 = pd.Series([0.5, 1.5], index=['elk', 'moose'])
        >>> df[['height', 'weight']] + s2
               elk  height  moose  weight
        elk    NaN     NaN    NaN     NaN
        moose  NaN     NaN    NaN     NaN

        >>> df[['height', 'weight']].add(s2, axis='index')
               height  weight
        elk       2.0   500.5
        moose     4.1   801.5

        When `other` is a :class:`DataFrame`, both columns names and the
        index are aligned.

        >>> other = pd.DataFrame({'height': [0.2, 0.4, 0.6]},
        ...                      index=['elk', 'moose', 'deer'])
        >>> df[['height', 'weight']] + other
               height  weight
        deer      NaN     NaN
        elk       1.7     NaN
        moose     3.0     NaN
        )r5   r   r   r    r   r   r   r6   b   s    XzOpsMixin.__add____radd__c                 C  s   |  |tjS r   )r5   r   Zraddr    r   r   r   r7      s    zOpsMixin.__radd____sub__c                 C  s   |  |tjS r   )r5   r   subr    r   r   r   r8      s    zOpsMixin.__sub____rsub__c                 C  s   |  |tjS r   )r5   r   Zrsubr    r   r   r   r:      s    zOpsMixin.__rsub____mul__c                 C  s   |  |tjS r   )r5   r   mulr    r   r   r   r;      s    zOpsMixin.__mul____rmul__c                 C  s   |  |tjS r   )r5   r   Zrmulr    r   r   r   r=      s    zOpsMixin.__rmul____truediv__c                 C  s   |  |tjS r   )r5   r   truedivr    r   r   r   r>      s    zOpsMixin.__truediv____rtruediv__c                 C  s   |  |tjS r   )r5   r   Zrtruedivr    r   r   r   r@      s    zOpsMixin.__rtruediv____floordiv__c                 C  s   |  |tjS r   )r5   r   floordivr    r   r   r   rA      s    zOpsMixin.__floordiv__Z__rfloordivc                 C  s   |  |tjS r   )r5   r   Z	rfloordivr    r   r   r   __rfloordiv__   s    zOpsMixin.__rfloordiv____mod__c                 C  s   |  |tjS r   )r5   r   modr    r   r   r   rD      s    zOpsMixin.__mod____rmod__c                 C  s   |  |tjS r   )r5   r   Zrmodr    r   r   r   rF      s    zOpsMixin.__rmod__
__divmod__c                 C  s   |  |tS r   )r5   divmodr    r   r   r   rG      s    zOpsMixin.__divmod____rdivmod__c                 C  s   |  |tjS r   )r5   r   Zrdivmodr    r   r   r   rI      s    zOpsMixin.__rdivmod____pow__c                 C  s   |  |tjS r   )r5   r   powr    r   r   r   rJ      s    zOpsMixin.__pow____rpow__c                 C  s   |  |tjS r   )r5   r   Zrpowr    r   r   r   rL      s    zOpsMixin.__rpow__N)#__name__
__module____qualname__r   r
   r   r!   r#   r%   r'   r)   r+   r,   r.   r/   r1   r2   r4   r5   r6   r7   r8   r:   r;   r=   r>   r@   rA   rC   rD   rF   rG   rI   rJ   rL   r   r   r   r   r      sv   












Y













r   znp.ufuncstrr   )ufuncmethodinputskwargsc                   s<  ddl m}m} ddlm ddlm  t}tf |}t	f||}|t
k	rZ|S tjj|jf}	|D ]P}
t|
do|
jjk}t|
dot|
j|	kot|
j }|s|rlt
  S qltdd |D }fd	d
t||D tdkrt|}t|dkr,||h|r,td dj}dd D ]4}tt||jD ]\}\}}||||< qRq>ttj|tfddt||D }nttjjjdkrdd
 |D }tt|dkr|d nd}d|ini fdd} fddd|krFtf||}||S dkrrtf||}|t
k	rr|S jdkrt|dksj dkrtdd |D }t!||}ntjdkrtdd |D }t!||}nDdkr|s|d j"}|#t!}nt$|d f||}||}|S )z
    Compatibility with numpy ufuncs.

    See also
    --------
    numpy.org/doc/stable/reference/arrays.classes.html#numpy.class.__array_ufunc__
    r   )	DataFrameSeriesNDFrame)BlockManager__array_priority____array_ufunc__c                 s  s   | ]}t |V  qd S r   )type.0xr   r   r   	<genexpr>)  s     zarray_ufunc.<locals>.<genexpr>c                   s   g | ]\}}t | r|qS r   )
issubclassr^   r_   trW   r   r   
<listcomp>*  s     
 zarray_ufunc.<locals>.<listcomp>   zCannot apply ufunc z& to mixed DataFrame and Series inputs.Nc                 3  s,   | ]$\}}t | r |jf n|V  qd S r   )ra   Zreindexrb   )rX   reconstruct_axesr   r   r`   A  s   c                 S  s    g | ]}t |d rt|d qS )name)hasattrgetattrr]   r   r   r   rd   I  s     
 rg   c                   s(   j dkr t fdd| D S  | S )Nre   c                 3  s   | ]} |V  qd S r   r   r]   )_reconstructr   r   r`   R  s     z3array_ufunc.<locals>.reconstruct.<locals>.<genexpr>)nouttupleresult)rj   rQ   r   r   reconstructO  s    
z array_ufunc.<locals>.reconstructc                   s~   t | r| S | jjkr*dkr&t| S t|  rLj| fddi} nj| fddi} tdkrz| } | S )NoutercopyFre   )r   Z	is_scalarndimNotImplementedError
isinstanceZ_constructorlenZ__finalize__rm   )rY   	alignablerR   rf   reconstruct_kwargsr   r   r   rj   V  s&    

 
z!array_ufunc.<locals>._reconstructoutreducec                 s  s   | ]}t |V  qd S r   npZasarrayr]   r   r   r   r`     s     c                 s  s   | ]}t |d dV  qdS )T)Zextract_numpyNr   r]   r   r   r   r`     s     __call__)%Zpandas.core.framerU   rV   Zpandas.core.genericrX   Zpandas.core.internalsrY   r\   _standardize_out_kwargr   r   r{   Zndarrayr[   rh   rZ   rt   Z_HANDLED_TYPESrl   zipru   setissubsetrs   axes	enumerateuniondictZ_AXIS_ORDERSrr   dispatch_ufunc_with_outdispatch_reduction_ufuncrk   ri   Z_mgrapplydefault_array_ufunc)r   rQ   rR   rS   rT   rU   rV   clsrn   Zno_deferitemZhigher_priorityZhas_array_ufunctypesZ	set_typesr   objiZax1Zax2namesrg   ro   Zmgrr   )	rY   rX   rj   rv   rR   rf   rw   r   rQ   r   array_ufunc   s    










&	
r   r   )returnc                  K  s@   d| kr<d| kr<d| kr<|  d}|  d}||f}|| d< | S )z
    If kwargs contain "out1" and "out2", replace that with a tuple "out"

    np.divmod, np.modf, np.frexp can have either `out=(out1, out2)` or
    `out1=out1, out2=out2)`
    rx   out1out2)pop)rT   r   r   rx   r   r   r   r}     s    

r}   )rQ   rR   c           
      O  s   | d}| dd}t||||}|tkr2tS t|tr~t|trVt|t|krZtt||D ]\}}	t||	| qd|S t|trt|dkr|d }ntt||| |S )zz
    If we have an `out` keyword, then call the ufunc without `out` and then
    set the result into the given `out`.
    rx   whereNre   r   )	r   ri   r   rt   rl   ru   rs   r~   _assign_where)
r   rQ   rR   rS   rT   rx   r   rn   Zarrresr   r   r   r     s"    



r   Nonec                 C  s(   |dkr|| dd< nt | || dS )zV
    Set a ufunc result into 'out', masking with a 'where' argument if necessary.
    N)r{   Zputmask)rx   rn   r   r   r   r   r     s    r   c                   s<   t  fdd|D st fdd|D }t||||S )z
    Fallback to the behavior we would get if we did not define __array_ufunc__.

    Notes
    -----
    We are assuming that `self` is among `inputs`.
    c                 3  s   | ]}| kV  qd S r   r   r]   r   r   r   r`     s     z&default_array_ufunc.<locals>.<genexpr>c                   s"   g | ]}| k	r|nt |qS r   rz   r]   r   r   r   rd     s     z'default_array_ufunc.<locals>.<listcomp>)anyrs   ri   )r   rQ   rR   rS   rT   Z
new_inputsr   r   r   r     s    r   c                 O  s   |dkst t|dks$|d | k	r(tS |jtkr6tS t|j }t| |sNtS | jdkrzt| trjd|d< d|krzd|d< t	| |f ddi|S )z@
    Dispatch ufunc reductions to self's reduction methods.
    ry   re   r   FZnumeric_onlyZaxisZskipna)
AssertionErrorru   r   rM   REDUCTION_ALIASESrh   rr   rt   r   ri   )r   rQ   rR   rS   rT   method_namer   r   r   r     s    




r   )__doc__
__future__r   r   typingr   Znumpyr{   Zpandas._libsr   Zpandas._libs.ops_dispatchr   Zpandas.core.dtypes.genericr   Zpandas.corer   Zpandas.core.constructionr	   Zpandas.core.ops.commonr
   r   r   r   r}   r   r   r   r   r   r   r   r   <module>   s0    _ ##