U
    f/e6                     @   s   d Z ddlZddlmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZmZ ddlmZ G dd	 d	Zd
d ZejeeedddZejeeedddZdS )zn
Methods that can be shared by many array-like classes or subclasses:
    Series
    Index
    ExtensionArray
    N)Any)libextract_array)!maybe_dispatch_ufunc_to_dunder_op	roperator)unpack_zerodim_and_deferc                   @   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-tiezk1ph/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   *   s    zOpsMixin.__le____gt__c                 C   s   |  |tjS r
   )r   r   gtr   r   r   r   r   .   s    zOpsMixin.__gt____ge__c                 C   s   |  |tjS r
   )r   r   ger   r   r   r   r    2   s    zOpsMixin.__ge__c                 C   s   t S r
   r   r   r   r   r   _logical_method9   s    zOpsMixin._logical_method__and__c                 C   s   |  |tjS r
   )r"   r   and_r   r   r   r   r#   <   s    zOpsMixin.__and____rand__c                 C   s   |  |tjS r
   )r"   r   Zrand_r   r   r   r   r%   @   s    zOpsMixin.__rand____or__c                 C   s   |  |tjS r
   )r"   r   or_r   r   r   r   r&   D   s    zOpsMixin.__or____ror__c                 C   s   |  |tjS r
   )r"   r   Zror_r   r   r   r   r(   H   s    zOpsMixin.__ror____xor__c                 C   s   |  |tjS r
   )r"   r   xorr   r   r   r   r)   L   s    zOpsMixin.__xor____rxor__c                 C   s   |  |tjS r
   )r"   r   Zrxorr   r   r   r   r+   P   s    zOpsMixin.__rxor__c                 C   s   t S r
   r   r   r   r   r   _arith_methodW   s    zOpsMixin._arith_method__add__c                 C   s   |  |tjS r
   )r,   r   addr   r   r   r   r-   Z   s    zOpsMixin.__add____radd__c                 C   s   |  |tjS r
   )r,   r   Zraddr   r   r   r   r/   ^   s    zOpsMixin.__radd____sub__c                 C   s   |  |tjS r
   )r,   r   subr   r   r   r   r0   b   s    zOpsMixin.__sub____rsub__c                 C   s   |  |tjS r
   )r,   r   Zrsubr   r   r   r   r2   f   s    zOpsMixin.__rsub____mul__c                 C   s   |  |tjS r
   )r,   r   mulr   r   r   r   r3   j   s    zOpsMixin.__mul____rmul__c                 C   s   |  |tjS r
   )r,   r   Zrmulr   r   r   r   r5   n   s    zOpsMixin.__rmul____truediv__c                 C   s   |  |tjS r
   )r,   r   truedivr   r   r   r   r6   r   s    zOpsMixin.__truediv____rtruediv__c                 C   s   |  |tjS r
   )r,   r   Zrtruedivr   r   r   r   r8   v   s    zOpsMixin.__rtruediv____floordiv__c                 C   s   |  |tjS r
   )r,   r   floordivr   r   r   r   r9   z   s    zOpsMixin.__floordiv__Z__rfloordivc                 C   s   |  |tjS r
   )r,   r   Z	rfloordivr   r   r   r   __rfloordiv__~   s    zOpsMixin.__rfloordiv____mod__c                 C   s   |  |tjS r
   )r,   r   modr   r   r   r   r<      s    zOpsMixin.__mod____rmod__c                 C   s   |  |tjS r
   )r,   r   Zrmodr   r   r   r   r>      s    zOpsMixin.__rmod__
__divmod__c                 C   s   |  |tS r
   )r,   divmodr   r   r   r   r?      s    zOpsMixin.__divmod____rdivmod__c                 C   s   |  |tjS r
   )r,   r   Zrdivmodr   r   r   r   rA      s    zOpsMixin.__rdivmod____pow__c                 C   s   |  |tjS r
   )r,   r   powr   r   r   r   rB      s    zOpsMixin.__pow____rpow__c                 C   s   |  |tjS r
   )r,   r   Zrpowr   r   r   r   rD      s    zOpsMixin.__rpow__N)#__name__
__module____qualname__r   r   r   r   r   r   r   r    r"   r#   r%   r&   r(   r)   r+   r,   r-   r/   r0   r2   r3   r5   r6   r8   r9   r;   r<   r>   r?   rA   rB   rD   r   r   r   r   r	      sv   


























r	   c                 C   s2   ddl m} t||r | |S | j|jS dS )zU
    Helper to check if a DataFrame is aligned with another DataFrame or Series.
    r   	DataFrameN)pandasrI   
isinstanceZ_indexed_samecolumnsequalsindex)framer   rI   r   r   r   _is_aligned   s    

rP   )ufuncmethodinputskwargsc           	         s   ddl m  ddlm tfdd|D }t fdd|D }|dkr|dkrt fd	d|D tfd
d|D }|rtjdtdd g }|D ]>}|kr|	| qt
|r|	t| q|	| qt| |||S tS )a  
    In the future DataFrame, inputs to ufuncs will be aligned before applying
    the ufunc, but for now we ignore the index but raise a warning if behaviour
    would change in the future.
    This helper detects the case where a warning is needed and then fallbacks
    to applying the ufunc on arrays to avoid alignment.

    See https://github.com/pandas-dev/pandas/pull/39239
    r   rH   NDFramec                 3   s   | ]}t | V  qd S r
   rK   .0xrU   r   r   	<genexpr>   s     z"_maybe_fallback.<locals>.<genexpr>c                 3   s   | ]}t | V  qd S r
   rW   rX   rH   r   r   r[      s           c                 3   s   | ]}t | r|V  qd S r
   rW   rX   rH   r   r   r[      s     
 c                 3   s$   | ]}t | rt| V  qd S r
   )rK   rP   rX   )rV   first_framer   r   r[      s    
 a  Calling a ufunc on non-aligned DataFrames (or DataFrame/Series combination). Currently, the indices are ignored and the result takes the index/columns of the first DataFrame. In the future , the DataFrames/Series will be aligned before applying the ufunc.
Convert one of the arguments to a NumPy array (eg 'ufunc(df1, np.asarray(df2)') to keep the current behaviour, or align manually (eg 'df1, df2 = df1.align(df2)') before passing to the ufunc to obtain the future behaviour and silence this warning.   
stacklevel)rJ   rI   pandas.core.genericrV   sumnextwarningswarnFutureWarningappendrK   npasarraygetattrr   )	rQ   rR   rS   rT   Zn_alignableZn_framesZnon_alignedZ
new_inputsrZ   r   )rI   rV   r^   r   _maybe_fallback   s0    

rl   c                    s  ddl m ddlm  t}tf||}|tk	r>|S tf||}|tk	r^|S tj	j
|j
f}|D ]P}t|do|jjk}	t|dot|j
|kot|j }
|	s|
rpt  S qptdd |D }fdd	t||D td
krtt|d
krtdj}d
d D ]4}tt||jD ]\}\}}||||< q@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jd
krLt|d
ks(jd
krLtdd |D }t||}nxjd
kr|tdd |D }t||}nHdkr|s|d j}|t}ntt|d f|}jd
krtfdd|D }n|}|S )z
    Compatibility with numpy ufuncs.

    See also
    --------
    numpy.org/doc/stable/reference/arrays.classes.html#numpy.class.__array_ufunc__
    r   rU   )BlockManager__array_priority____array_ufunc__c                 s   s   | ]}t |V  qd S r
   )typerX   r   r   r   r[     s     zarray_ufunc.<locals>.<genexpr>c                    s   g | ]\}}t | r|qS r   )
issubclassrY   rZ   trU   r   r   
<listcomp>  s     
 zarray_ufunc.<locals>.<listcomp>r]   z;Cannot apply ufunc {} to mixed DataFrame and Series inputs.Nc                 3   s,   | ]$\}}t | r |jf n|V  qd S r
   )rq   Zreindexrr   )rV   reconstruct_axesr   r   r[   0  s   c                 S   s    g | ]}t |d rt|d qS )name)hasattrrk   rX   r   r   r   rt   8  s     
 rv   c                    s   t | r| S | jjkrRdkrNjdkrJd}tj|tdd | S t| S t|  rtj	| fddi} nj	| fddi} t
dkr| } | S )	Nouterr\   zouter method for ufunc {} is not implemented on pandas objects. Returning an ndarray, but in the future this will raise a 'NotImplementedError'. Consider explicitly converting the DataFrame to an array with '.to_numpy()' first.r_   r`   copyFr]   )r   Z	is_scalarndimre   rf   formatrg   NotImplementedErrorrK   Z_constructorlenZ__finalize__)resultmsg)rm   	alignablerR   ru   reconstruct_kwargsr   rQ   r   r   reconstruct>  s0    


 
z array_ufunc.<locals>.reconstructc                 s   s   | ]}t |V  qd S r
   )ri   rj   rX   r   r   r   r[   g  s     c                 s   s   | ]}t |d dV  qdS )T)Zextract_numpyNr   rX   r   r   r   r[   k  s     __call__c                 3   s   | ]} |V  qd S r
   r   rX   )r   r   r   r[   {  s     ) rb   rV   Zpandas.core.internalsrm   rp   rl   r   r   ri   Zndarrayro   rw   rn   rK   Z_HANDLED_TYPEStuplezipr}   setr|   r{   axes	enumerateuniondictZ_AXIS_ORDERSrz   Znoutrk   Z_mgrapplyrj   )r   rQ   rR   rS   rT   clsr~   Zno_deferitemZhigher_priorityZhas_array_ufunctypesr   objiZax1Zax2namesrv   Zmgrr   )	rm   rV   r   rR   r   ru   r   r   rQ   r   array_ufunc   sz    





"&
r   )__doc__r   typingr   re   Znumpyri   Zpandas._libsr   Zpandas.core.constructionr   Zpandas.core.opsr   r   Zpandas.core.ops.commonr   r	   rP   rQ   strrl   r   r   r   r   r   <module>   s    	>