U
    b                     @   s\   d Z ddlZddlZddlmZmZmZ ddlm	Z	 dd Z
ejejddd	Zd
d ZdS )aP  
Missing data handling for arithmetic operations.

In particular, pandas conventions regarding division by zero differ
from numpy in the following ways:
    1) np.array([-1, 0, 1], dtype=dtype1) // np.array([0, 0, 0], dtype=dtype2)
       gives [nan, nan, nan] for most dtype combinations, and [0, 0, 0] for
       the remaining pairs
       (the remaining being dtype1==dtype2==intN and dtype==dtype2==uintN).

       pandas convention is to return [-inf, nan, inf] for all dtype
       combinations.

       Note: the numpy behavior described here is py3-specific.

    2) np.array([-1, 0, 1], dtype=dtype1) % np.array([0, 0, 0], dtype=dtype2)
       gives precisely the same results as the // operation.

       pandas convention is to return [nan, nan, nan] for all dtype
       combinations.

    3) divmod behavior consistent with 1) and 2).
    N)is_float_dtypeis_integer_dtype	is_scalar)	roperatorc                 C   s   t | jr| S t|d}t|}|s,|s,| S |r:t|}t|jr|dk}| r|t|  @ }| j	ddd} t
| |tj | S )z
    If this is a reversed op, then flip x,y

    If we have an integer value (or array in y)
    and we have 0's, fill them with np.nan,
    return the result.

    Mask the nan's from x.
    dtyper   float64Fcopy)r   r   hasattrr   nparrayr   anyisnanastypeZputmasknan)resultxyZis_variable_typeZis_scalar_typeZymaskmask r   ;/tmp/pip-unpacked-wheel-ck39h295/pandas/core/ops/missing.py_fill_zeros%   s    




r   )r   returnc              	   C   s   t |dst|}t | ds(t| } |dk}| r|t|@ }|| @ }| dk }| dk}|| dk@ }tjdd& ||@ ||@ B }	||@ ||@ B }
W 5 Q R X | s|	 s|
 r|jddd}tj||< tj||
< tj ||	< |S )am  
    Set results of  0 // 0 to np.nan, regardless of the dtypes
    of the numerator or the denominator.

    Parameters
    ----------
    x : ndarray
    y : ndarray
    result : ndarray

    Returns
    -------
    ndarray
        The filled result.

    Examples
    --------
    >>> x = np.array([1, 0, -1], dtype=np.int64)
    >>> x
    array([ 1,  0, -1])
    >>> y = 0       # int 0; numpy behavior is different with float
    >>> result = x // y
    >>> result      # raw numpy result does not fill division by zero
    array([0, 0, 0])
    >>> mask_zero_div_zero(x, y, result)
    array([ inf,  nan, -inf])
    r   r   ignore)invalidr   Fr   )	r
   r   r   r   ZsignbitZerrstater   r   inf)r   r   r   ZzmaskZ	zneg_maskZ	zpos_maskZx_lt0Zx_gt0Znan_maskZneginf_maskZposinf_maskr   r   r   mask_zero_div_zeroL   s(    






r   c                 C   s   | t kr*t|||d t|d ||f}n| tjkrVt|||d t|d ||f}n^| tjkrnt|||}nF| tjkrt|||}n.| tjkrt|||}n| tj	krt|||}|S )a  
    Call _fill_zeros with the appropriate fill value depending on the operation,
    with special logic for divmod and rdivmod.

    Parameters
    ----------
    op : function (operator.add, operator.div, ...)
    left : object (np.ndarray for non-reversed ops)
    right : object (np.ndarray for reversed ops)
    result : ndarray

    Returns
    -------
    result : np.ndarray

    Notes
    -----
    For divmod and rdivmod, the `result` parameter and returned `result`
    is a 2-tuple of ndarray objects.
    r      )
divmodr   r   r   ZrdivmodoperatorfloordivZ	rfloordivmodZrmod)opleftrightr   r   r   r   dispatch_fill_zeros   s"    




r%   )__doc__r   Znumpyr   Zpandas.core.dtypes.commonr   r   r   Zpandas.core.opsr   r   Zndarrayr   r%   r   r   r   r   <module>   s   '>