HOME


sh-3ll 1.0
DIR:/usr/local/lib64/python3.6/site-packages/pandas/core/__pycache__/
Upload File :
Current File : //usr/local/lib64/python3.6/site-packages/pandas/core/__pycache__/nanops.cpython-36.pyc
3

���ho��@slddlZddlZddlZddlmZmZmZmZmZddl	Z
ddlmZddl
mZmZmZmZmZddlmZmZmZmZmZddlmZddlmZmZddlmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-dd	l.m/Z/dd
l0m1Z1m2Z2m3Z3ddl4m5Z5edd
dd�Z6e6dk	Z7d
a8d�e9dd�dd�Z:e:ed��Gdd�d�Z;Gdd�d�Z<ee=e9d�dd�Z>e9d�dd�Z?d�eeed�d d!�Z@e
jAe9ee
jAee
jAd"�d#d$�ZBd�e
jAe9eee=ee
jAee
jAee
jAe
jCe
jCefd%�d&d'�ZDee9d(�d)d*�ZEd�ed+�d,d-�ZFe
jAeeGeee
jAfd.�d/d0�ZHd�e
jAeeGe9ee
jAe9d1�d2d3�ZId�e
jAeeGe9ee
jAe9d1�d4d5�ZJe;d6�d�e
jAeeGe9eGee
jAeKd7�d8d9��ZLe;e/�e<�d�e
jAeeGe9ee
jAeKd1�d:d;���ZMe<�d�d<d=��ZNeKfeeGee
jAeeGeGeeeeGe
jAfeeGe
jAffd>�d?d@�ZOe;d6�e<dAdB�d�dCdD���ZPe;d6dE�e<dAdB�d�dFdG���ZQe;d6dE�d�e
jAeeGe9eGee
jAeKdH�dIdJ��ZRdKdL�ZSeSdMdNdO�ZTeSdPdQdO�ZUe;dR�d�e
jAeeGe9ee
jAeeGe
jAfd1�dSdT��ZVe;dR�d�e
jAeeGe9ee
jAeeGe
jAfd1�dUdV��ZWe;d6dE�d�e
jAeeGe9ee
jAeKd1�dWdX��ZXe;d6dE�d�e
jAeeGe9ee
jAeKd1�dYdZ��ZYe;d6dE�d�e
jAeeGe9eGee
jAeKd7�d[d\��ZZe
jAeeGee
jAe9ee
jAeGfd]�d^d_�Z[eKfeeGd`fee
jAeeGeeeGeKe
jAfda�dbdc�Z\d�e
jAeeGee
jAeeGd`feGeKdd�dedf�Z]eeGd`fee
jAeGe9dg�dhdi�Z^djdk�Z_e;d6dE�d�e
jAe
jAeeGdm�dndo��Z`dpdq�Zae;d6dE�d�e
jAe
jAeeGeeGdr�dsdt��Zbdudv�Zcdwdx�Zdedeje�Zfedejg�Zhedeji�Zjedejk�Zledejm�Znedejo�Zpe
jAe
jAeeee
jAfdy�dzd{�Zqe
jAeGe
jAeGd|�d}d~�Zree9ed�d�d��ZsdS)��N)�Any�Optional�Tuple�Union�cast)�
get_option)�NaT�	Timedelta�	Timestamp�iNaT�lib)�	ArrayLike�Dtype�DtypeObj�F�Scalar)�import_optional_dependency)�
_int64_max�maybe_upcast_putmask)�
_get_dtype�is_any_int_dtype�
is_bool_dtype�
is_complex�is_datetime64_any_dtype�is_float�is_float_dtype�
is_integer�is_integer_dtype�is_numeric_dtype�is_object_dtype�	is_scalar�is_timedelta64_dtype�needs_i8_conversion�pandas_dtype)�PeriodDtype)�isna�na_value_for_dtype�notna)�
extract_arrayZ
bottleneckF�warn)Zraise_on_missingZ
on_versionT)�v�returncCstr|adS)N)�_BOTTLENECK_INSTALLED�_USE_BOTTLENECK)r*�r.�4/tmp/pip-build-5_djhm0z/pandas/pandas/core/nanops.py�set_use_bottleneck*sr0zcompute.use_bottleneckcs:eZdZ�fdd�Zed�dd�Zeed�dd�Z�ZS)	�disallowcs"t�j�tdd�|D��|_dS)Ncss|]}t|�jVqdS)N)r#�type)�.0�dtyper.r.r/�	<genexpr>7sz$disallow.__init__.<locals>.<genexpr>)�super�__init__�tuple�dtypes)�selfr9)�	__class__r.r/r75s
zdisallow.__init__)r+cCst|d�ot|jj|j�S)Nr4)�hasattr�
issubclassr4r2r9)r:�objr.r.r/�check9szdisallow.check)�fr+cs"tj����fdd��}tt|�S)Ncs�tj||j��}t�fdd�|D��rD�jjdd�}td|�d���y"tjdd���||�SQRXWn:t	k
r�}zt
|d	�r�t|�|��WYdd}~XnXdS)
Nc3s|]}�j|�VqdS)N)r?)r3r>)r:r.r/r5@sz0disallow.__call__.<locals>._f.<locals>.<genexpr>�nan�zreduction operation 'z' not allowed for this dtype�ignore)�invalidr)�	itertools�chain�values�any�__name__�replace�	TypeError�np�errstate�
ValueErrorr)�args�kwargsZobj_iterZf_name�e)r@r:r.r/�_f=s
zdisallow.__call__.<locals>._f)�	functools�wrapsrr)r:r@rRr.)r@r:r/�__call__<szdisallow.__call__)	rI�
__module__�__qualname__r7�boolr?rrU�
__classcell__r.r.)r;r/r14sr1c@s&eZdZddd�Zeed�dd�ZdS)�bottleneck_switchNcKs||_||_dS)N)�namerP)r:r[rPr.r.r/r7Uszbottleneck_switch.__init__)�altr+csp�jp
�j�ytt���Wnttfk
r6d�YnXtj��dtj	t
ttd�����fdd�
�}t
t|�S)NT)rG�axis�skipnacs�t�j�dkr6x&�jj�D]\}}||kr|||<qW|jdkrX|jd�dkrXt||�Str�|r�t|j��r�|jdd�dkr�|j	dd��|fd|i|��}t
|�r҈|f||d�|��}q�|f||d�|��}n�|f||d�|��}|S)Nr�	min_count�maskr])r]r^)�lenrP�items�size�get�_na_for_min_countr-�_bn_ok_dtyper4�pop�	_has_infs)rGr]r^�kwds�kr*�result)r\�bn_func�bn_namer:r.r/r@as
z%bottleneck_switch.__call__.<locals>.f)NT)r[rI�getattr�bn�AttributeError�	NameErrorrSrTrL�ndarrayr�intrXrr)r:r\r@r.)r\rlrmr:r/rUYs
&"zbottleneck_switch.__call__)N)rIrVrWr7rrUr.r.r.r/rZTs
rZ)r4r[r+cCs(t|�r$t|�r$|dkr dSdSdS)N�nansum�nanprodFT)rtru)rr")r4r[r.r.r/rf�s

rf)r+cCslt|tj�r@|jdkr&tj|jd��S|jdkr@tj|jd��Sytj|�j	�St
tfk
rfdSXdS)N�f8�KZf4F)�
isinstancerLrrr4rZhas_infs_f8�ravelZhas_infs_f4�isinfrHrK�NotImplementedError)rkr.r.r/rh�s

rh)r4�
fill_valuecCsZ|dk	r|St|�r:|dkr"tjS|dkr0tjStjSn|dkrFtS|dkrRtStSdS)z; return the correct fill value for the dtype of the values Nz+inf)�_na_ok_dtyperLrA�infrr)r4r|�fill_value_typr.r.r/�_get_fill_value�s
r�)rGr^r`r+cCs0|dkr,t|j�st|j�r dS|r,t|�}|S)a�
    Compute a mask if and only if necessary.

    This function will compute a mask iff it is necessary. Otherwise,
    return the provided mask (potentially None) when a mask does not need to be
    computed.

    A mask is never necessary if the values array is of boolean or integer
    dtypes, as these are incapable of storing NaNs. If passing a NaN-capable
    dtype that is interpretable as either boolean or integer data (eg,
    timedelta64), a mask must be provided.

    If the skipna parameter is False, a new mask will not be computed.

    The mask is computed using isna() by default. Setting invert=True selects
    notna() as the masking function.

    Parameters
    ----------
    values : ndarray
        input array to potentially compute mask for
    skipna : bool
        boolean for whether NaNs should be skipped
    mask : Optional[ndarray]
        nan-mask if known

    Returns
    -------
    Optional[np.ndarray]
    N)rr4rr%)rGr^r`r.r.r/�_maybe_get_mask�s!r�)rGr^r|rr`r+c	Cs�t|�st�t|dd�}t|||�}|j}t|j�rDtj|jd��}t	|�}t
|||d�}|r�|dk	r�|dk	r�|j�}|r�|j�r�tj
|||�nt|||�\}}|}t|�s�t|�r�tjtj�}nt|�r�tjtj�}|||||fS)a1
    Utility to get the values view, mask, dtype, dtype_max, and fill_value.

    If both mask and fill_value/fill_value_typ are not None and skipna is True,
    the values array will be copied.

    For input arrays of boolean or integer dtypes, copies will only occur if a
    precomputed mask, a fill_value/fill_value_typ, and skipna=True are
    provided.

    Parameters
    ----------
    values : ndarray
        input array to potentially compute mask for
    skipna : bool
        boolean for whether NaNs should be skipped
    fill_value : Any
        value to fill NaNs with
    fill_value_typ : str
        Set to '+inf' or '-inf' to handle dtype-specific infinities
    mask : Optional[np.ndarray]
        nan-mask if known

    Returns
    -------
    values : ndarray
        Potential copy of input value array
    mask : Optional[ndarray[bool]]
        Mask for values, if deemed necessary to compute
    dtype : np.dtype
        dtype for values
    dtype_max : np.dtype
        platform independent dtype
    fill_value : Any
        fill value used
    T)�
extract_numpy�i8)r|rN)r �AssertionErrorr(r�r4r"rL�asarray�viewr}r��copyrH�putmaskrrr�int64r�float64)	rGr^r|rr`r4Zdtype_ok�_�	dtype_maxr.r.r/�_get_values�s(.
r�)r4r+cCst|�rdSt|jtj�S)NF)r"r=r2rL�integer)r4r.r.r/r}Bsr})r4cCs�t|�rf|dkrt}t|tj�sZt|dd�}t|�s>td��||krLtj}t	||d�}q�|j
|�}nVt|�r�t|tj�s�||kr�tj}tj|�t
kr�td��t|dd�}n|j
d�j|�}|S)	z wrap our results if needed N�tzzExpected non-null fill_value)r�zoverflow in timedelta operation�ns)�unitzm8[ns])rrrxrLrrrnr%r�rAr
�astyper!�fabsrrNr	r�)rkr4r|r�r.r.r/�
_wrap_resultsHs&r�)rGr]r+cCs�t|�r|jd�}t|j�}|jdkr*|S|dk	s6t�|jd|�|j|dd�}ytj|||jd�}Wn t	k
r�tj||�}YnX|SdS)a�
    Return the missing value for `values`.

    Parameters
    ----------
    values : ndarray
    axis : int or None
        axis for the reduction, required if values.ndim > 1.

    Returns
    -------
    result : scalar or ndarray
        For 1-D values, returns a scalar of the correct missing type.
        For 2-D values, returns a 1-D array where each element is missing.
    r��N)r4)
rr�r&r4�ndimr��shaperL�fullrN)rGr]r|Zresult_shaperkr.r.r/regs


 re)rGr]r^r`r+cCs$t||d|d�\}}}}}|j|�S)a
    Check if any elements along an axis evaluate to True.

    Parameters
    ----------
    values : ndarray
    axis : int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : bool

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, 2])
    >>> nanops.nanany(s)
    True

    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([np.nan])
    >>> nanops.nanany(s)
    False
    F)r|r`)r�rH)rGr]r^r`r�r.r.r/�nanany�s!r�cCs$t||d|d�\}}}}}|j|�S)a
    Check if all elements along an axis evaluate to True.

    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : bool

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, 2, np.nan])
    >>> nanops.nanall(s)
    True

    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, 0])
    >>> nanops.nanall(s)
    False
    T)r|r`)r��all)rGr]r^r`r�r.r.r/�nanall�s!r�ZM8)rGr]r^r_r`r+c
Csft||d|d�\}}}}}|}t|�r,|}nt|�r:tj}|j||d�}	t|	|||j|d�}	t|	|�S)a�
    Sum the elements along an axis ignoring NaNs

    Parameters
    ----------
    values : ndarray[dtype]
    axis: int, optional
    skipna : bool, default True
    min_count: int, default 0
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : dtype

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, 2, np.nan])
    >>> nanops.nansum(s)
    3.0
    r)r|r`)r4)r_)	r�rr!rLr��sum�_maybe_null_outr�r�)
rGr]r^r_r`r4r�r��	dtype_sum�the_sumr.r.r/rt�srtc

Cs�t||d|d�\}}}}}|}tj}t|�s<t|�s<t|�rDtj}nt|�rT|}|}t|j|||d�}	t	|j
||d��}
|dk	r�t|
dd�r�ttj
|	�}	tjdd��|
|	}WdQRX|	dk}|j�r�tj||<n|	dkr�|
|	ntj}t||�S)	a
    Compute the mean of the element along an axis ignoring NaNs

    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    float
        Unless input is a float array, in which case use the same
        precision as the input array.

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, 2, np.nan])
    >>> nanops.nanmean(s)
    1.5
    r)r|r`)r4Nr�FrC)r�)r�rLr�rrr!r�_get_countsr��_ensure_numericr�rnrrrrMrHrAr�)
rGr]r^r`r4r�r�r�Zdtype_count�countr�Zthe_meanZct_maskr.r.r/�nanmeans, r�cs$�fdd�}t|�|d�\}}}}}t|j�sxy|jd�}Wn(tk
rd}zt|�WYdd}~XnX|dk	rxtj||<|dkr�|jd�}|j	}|j
dk�r
|rʈs�ttj|||�|�Sttj
||�|�Stj|j�}	tj|j
�}
tj|	|
|k�}|jtj�t||�St|�r||�ntj|�S)a�
    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : float
        Unless input is a float array, in which case use the same
        precision as the input array.

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, np.nan, 2, 2])
    >>> nanops.nanmedian(s)
    2.0
    cs,t|�}�r|j�rtjStj||�S)N)r'r�rLrA�	nanmedian)�xr`)r^r.r/�
get_median\sznanmedian.<locals>.get_median)r`rvNrwr�)r�rr4r�rNrKrLrAryrcr�r�Zapply_along_axisr��arrayr�Zarange�empty�fill)rGr]r^r`r�r4r��errZnotemptyZshpZdims�retr.)r^r/r�Cs0



r�)�value_countsr`r]�ddofr4r+cCs|t|�}t||||d�}||j|�}t|�rD||krttj}tj}n0||k}|j�rttj||tj�tj||tj�||fS)a!
    Get the count of non-null values along an axis, accounting
    for degrees of freedom.

    Parameters
    ----------
    values_shape : Tuple[int]
        shape tuple from values ndarray, used if mask is None
    mask : Optional[ndarray[bool]]
        locations in values that should be considered missing
    axis : Optional[int]
        axis to count along
    ddof : int
        degrees of freedom
    dtype : type, optional
        type to use for count

    Returns
    -------
    count : scalar or array
    d : scalar or array
    )r4)rr�r2r rLrArHr�)r�r`r]r�r4r��dZmask2r.r.r/�_get_counts_nanvar�sr�r�)r�cCs@|j}t|||d�\}}}}}tjt|||||d��}t||�S)a�
    Compute the standard deviation along given axis while ignoring NaNs

    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    ddof : int, default 1
        Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
        where N represents the number of elements.
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : float
        Unless input is a float array, in which case use the same
        precision as the input array.

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, np.nan, 2, 3])
    >>> nanops.nanstd(s)
    1.0
    )r`)r]r^r�r`)r4r�rL�sqrt�nanvarr�)rGr]r^r�r`�
orig_dtyper�rkr.r.r/�nanstd�sr�Zm8cCs"t|dd�}|j}t|||�}t|�rB|jd�}|dk	rBtj||<t|j�rft|j	||||j�\}}nt|j	|||�\}}|r�|dk	r�|j
�}tj||d�t|j
|tjd��|}|dk	r�tj||�}t||d�}	|dk	r�tj|	|d�|	j
|tjd�|}
t|��r|
j|�}
t|
|j�S)a�
    Compute the variance along given axis while ignoring NaNs

    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    ddof : int, default 1
        Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
        where N represents the number of elements.
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : float
        Unless input is a float array, in which case use the same
        precision as the input array.

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, np.nan, 2, 3])
    >>> nanops.nanvar(s)
    1.0
    T)r�rvNr)r]r4�)r(r4r�rr�rLrArr�r�r�r�r�r�r��expand_dimsr�)rGr]r^r�r`r4r�r��avgZsqrrkr.r.r/r��s.




r�)rGr]r^r�r`r+cCsnt|||||d�t|||�}t|j�s2|jd�}t|j||||j�\}}t||||d�}tj|�tj|�S)a�
    Compute the standard error in the mean along given axis while ignoring NaNs

    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    ddof : int, default 1
        Delta Degrees of Freedom. The divisor used in calculations is N - ddof,
        where N represents the number of elements.
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : float64
        Unless input is a float array, in which case use the same
        precision as the input array.

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, np.nan, 2, 3])
    >>> nanops.nansem(s)
     0.5773502691896258
    )r�r`rv)r�)	r�r�rr4r�r�r�rLr�)rGr]r^r�r`r�r��varr.r.r/�nansem$s%

r�cs<td�d�dtjtttttjtd���fdd�
�}|S)NrA)r[T)rGr]r^r`r+c
s�t||�|d�\}}}}}|dk	r0|j|dks:|jdkr�y"t|��||d�}|jtj�Wq�ttt	fk
r|tj}Yq�Xnt|��|�}t
|||�}t||||j�S)N)rr`r)r4)r�r�rcrnr�rLrArprKrNr�r�)rGr]r^r`r4r�r|rk)r�methr.r/�	reductionVs z_nanminmax.<locals>.reduction)NTN)rZrLrrrrsrXr)r�rr�r.)rr�r/�
_nanminmaxUsr��minz+inf)r�maxz-inf�OcCs6t|dd|d�\}}}}}|j|�}t||||�}|S)a�
    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : int or ndarray[int]
        The index/indices  of max value in specified axis or -1 in the NA case

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> arr = np.array([1, 2, 3, np.nan, 4])
    >>> nanops.nanargmax(arr)
    4

    >>> arr = np.array(range(12), dtype=np.float64).reshape(4, 3)
    >>> arr[2:, 2] = np.nan
    >>> arr
    array([[ 0.,  1.,  2.],
           [ 3.,  4.,  5.],
           [ 6.,  7., nan],
           [ 9., 10., nan]])
    >>> nanops.nanargmax(arr, axis=1)
    array([2, 2, 1, 1], dtype=int64)
    Tz-inf)rr`)r�Zargmax�_maybe_arg_null_out)rGr]r^r`r�rkr.r.r/�	nanargmaxus&
r�cCs6t|dd|d�\}}}}}|j|�}t||||�}|S)a�
    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : int or ndarray[int]
        The index/indices of min value in specified axis or -1 in the NA case

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> arr = np.array([1, 2, 3, np.nan, 4])
    >>> nanops.nanargmin(arr)
    0

    >>> arr = np.array(range(12), dtype=np.float64).reshape(4, 3)
    >>> arr[2:, 0] = np.nan
    >>> arr
    array([[ 0.,  1.,  2.],
           [ 3.,  4.,  5.],
           [nan,  7.,  8.],
           [nan, 10., 11.]])
    >>> nanops.nanargmin(arr, axis=1)
    array([0, 0, 1, 1], dtype=int64)
    Tz+inf)rr`)r�Zargminr�)rGr]r^r`r�rkr.r.r/�	nanargmin�s&
r�c
Cs�t|dd�}t|||�}t|j�s<|jd�}t|j||�}nt|j|||jd�}|rr|dk	rr|j�}tj	||d�|j
|tjd�|}|dk	r�tj||�}||}|r�|dk	r�tj	||d�|d}||}|j
|tjd�}	|j
|tjd�}
t
|	�}	t
|
�}
tjddd	��*||d
d|d|
|	d}WdQRX|j}t|��rT|j|�}t|tj��r�tj|	dkd|�}tj||d
k<|S|	dk�r�dn|}|d
k�r�tjS|SdS)a�
    Compute the sample skewness.

    The statistic computed here is the adjusted Fisher-Pearson standardized
    moment coefficient G1. The algorithm computes this coefficient directly
    from the second and third central moment.

    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : float64
        Unless input is a float array, in which case use the same
        precision as the input array.

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, np.nan, 1, 2])
    >>> nanops.nanskew(s)
    1.7320508075688787
    T)r�rv)r4Nrr�rC)rD�divider�g�?g�?�)r(r�rr4r�r�r�r�rLr�r�r�r��_zero_out_fperrrMrxrr�whererA)
rGr]r^r`r��mean�adjusted�	adjusted2Z	adjusted3�m2Zm3rkr4r.r.r/�nanskew�sD#

.


r�cCst|dd�}t|||�}t|j�s<|jd�}t|j||�}nt|j|||jd�}|rr|dk	rr|j�}tj	||d�|j
|tjd�|}|dk	r�tj||�}||}|r�|dk	r�tj	||d�|d}|d}|j
|tjd�}	|j
|tjd�}
tj
ddd	��Vd
|dd|d|d
}||d|d|
}|d|d
|	d}
WdQRXt|�}t|
�}
t|
tj��s�|dk�r�tjS|
dk�r�dStj
ddd	��||
|}WdQRX|j}t|��r�|j|�}t|tj��rtj|
dkd|�}tj||dk<|S)
a�
    Compute the sample excess kurtosis

    The statistic computed here is the adjusted Fisher-Pearson standardized
    moment coefficient G2, computed directly from the second and fourth
    central moment.

    Parameters
    ----------
    values : ndarray
    axis: int, optional
    skipna : bool, default True
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    result : float64
        Unless input is a float array, in which case use the same
        precision as the input array.

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, np.nan, 1, 3, 2])
    >>> nanops.nankurt(s)
    -1.2892561983471076
    T)r�rv)r4Nrr�rC)rDr�r�r��)r(r�rr4r�r�r�r�rLr�r�r�r�rMr�rxrrrAr�)rGr]r^r`r�r�r�r�Z	adjusted4r�Zm4ZadjZnumerZdenomrkr4r.r.r/�nankurt!sN#

 "



r�cCsFt|||�}|r(|dk	r(|j�}d||<|j|�}t||||j|d�S)a�
    Parameters
    ----------
    values : ndarray[dtype]
    axis: int, optional
    skipna : bool, default True
    min_count: int, default 0
    mask : ndarray[bool], optional
        nan-mask if known

    Returns
    -------
    Dtype
        The product of all elements on a given axis. ( NaNs are treated as 1)

    Examples
    --------
    >>> import pandas.core.nanops as nanops
    >>> s = pd.Series([1, 2, 3, np.nan])
    >>> nanops.nanprod(s)
    6.0
    Nr�)r_)r�r��prodr�r�)rGr]r^r_r`rkr.r.r/ru~s
ru)rkr]r`r^r+cCsp|dkr|S|dks"t|dd�rB|r4|j�r@d}ql|j�rld}n*|rR|j|�}n
|j|�}|j�rld||<|S)Nr�Fr����r�r�)rnr�rH)rkr]r`r^Zna_maskr.r.r/r��s
r�.)�values_shaper`r]r4r+cCs�t|�}|dkr<|dk	r(|j|j�}n
tj|�}|j|�S|dk	rZ|j||j|�}n||}t|�rt|j|�Sy
|j|�St	k
r�tj
||d�SXdS)a�
    Get the count of non-null values along an axis

    Parameters
    ----------
    values_shape : tuple of int
        shape tuple from values ndarray, used if mask is None
    mask : Optional[ndarray[bool]]
        locations in values that should be considered missing
    axis : Optional[int]
        axis to count along
    dtype : type, optional
        type to use for count

    Returns
    -------
    count : scalar or array
    N)r4)rrcr�rLr�r2r�r r�rpr�)r�r`r]r4�nr�r.r.r/r��s



r�)rkr]r`r�r_r+cCs�|dk	r�|dk	r�t|dd�r�|j||j|�|dk}tj|�r�t|�rvtj|�r`|jd�}n
|jd�}tj||<q�d||<n|t	k	r�t
|||�r�tj}|S)zu
    Returns
    -------
    Dtype
        The product of all elements on a given axis. ( NaNs are treated as 1)
    Nr�FrZc16rv)rnr�r�rLrHrZiscomplexobjr�rAr�check_below_min_count)rkr]r`r�r_Z	null_maskr.r.r/r��s




r�)r�r`r_r+cCs:|dkr6|dkrtj|�}n|j|j�}||kr6dSdS)a�
    Check for the `min_count` keyword. Returns True if below `min_count` (when
    missing value should be returned from the reduction).

    Parameters
    ----------
    shape : tuple
        The shape of the values (`values.shape`).
    mask : ndarray or None
        Boolean numpy array (typically of same shape as `shape`) or None.
    min_count : int
        Keyword passed through from sum/prod call.

    Returns
    -------
    bool
    rNTF)rLr�rcr�)r�r`r_Z	non_nullsr.r.r/r�
sr�cCs\t|tj�r:tjdd��tjtj|�dkd|�SQRXntj|�dkrT|jjd�S|SdS)NrC)rDg�+����=r)rxrLrrrMr��absr4r2)�argr.r.r/r�)s r��pearson)�a�b�min_periodscCspt|�t|�krtd��|dkr$d}t|�t|�@}|j�sL||}||}t|�|kr^tjSt|�}|||�S)z
    a, b: ndarrays
    z'Operands to nancorr must have same sizeNr�)rar�r'r�rLrA�
get_corr_func)r�r��methodr��validr@r.r.r/�nancorr2sr�cs||dkr$ddlm��fdd�}|S|dkrHddlm��fdd�}|S|d	kr\d
d�}|St|�rh|Std|�d���dS)
NZkendallr)�
kendalltaucs�||�dS)Nrr.)r�r�)r�r.r/�funcOszget_corr_func.<locals>.funcZspearman)�	spearmanrcs�||�dS)Nrr.)r�r�)r�r.r/r�Vsr�cSstj||�dS)Nrr�)rr�)rLZcorrcoef)r�r�r.r.r/r�\szUnknown method 'z@', expected one of 'kendall', 'spearman', 'pearson', or callable)Zscipy.statsr�r��callablerN)r�r�r.)r�r�r/r�Ksr�)r�r�r�r�cCsrt|�t|�krtd��|dkr$d}t|�t|�@}|j�sL||}||}t|�|kr^tjStj|||d�dS)Nz&Operands to nancov must have same sizer�)r�r)rr�)rar�r'r�rLrAZcov)r�r�r�r�r�r.r.r/�nancovisr�cCs@t|tj�r�t|�st|�r*|jtj�}n�t|�r�y|jtj�}Wn^t	t
fk
r�y|jtj�}Wn4t
k
r�}zt	d|�d��|�WYdd}~XnXYnXtjtj|��s�|j
}n�t|�p�t|�p�t|��s<yt|�}WnZt
k
�r:yt|�}Wn6t
k
�r4}zt	d|�d��|�WYdd}~XnXYnX|S)NzCould not convert z to numeric)rxrLrrrrr�r�rZ
complex128rKrNrH�imag�realrrr�float�complex)r�r�r.r.r/r��s,**r�cs�fdd�}|S)Ncsht|�}t|�}||B}tjdd���||�}WdQRX|j�rdt|�rT|jd�}tj||tj�|S)NrC)r�r�)r%rLrMrHrr�r�rA)r��yZxmaskZymaskr`rk)�opr.r/r@�s
zmake_nancomp.<locals>.fr.)r�r@r.)r�r/�make_nancomp�sr�)rGr`�na_valuer+cCsN||}t|�dkr>tj|�r$|Stj|gt|�|jd�Stj|||d�S)a�
    Wrapper for np.percentile that skips missing values, specialized to
    1-dimensional case.

    Parameters
    ----------
    values : array over which to find quantiles
    mask : ndarray[bool]
        locations in values that should be considered missing
    q : scalar or array of quantile indices to find
    na_value : scalar
        value to return for empty or all-null values
    interpolation : str

    Returns
    -------
    quantiles : scalar or array
    r)r4)�
interpolation)rarr rLr�r4�
percentile)rGr`�qr�r�r.r.r/�_nanpercentile_1d�s

r�)rGr]r`r�cs�|jjd
kr8t|jd��|�jd�||��}|j|j�Stj|�r�|j�r�|dkrft||���d�S|j	|j	kr~|j
|j�}|dkr�|j}|j}���fdd�t
t|�t|��D�}tj||jd	d
�j}|Sntj|�|�d�SdS)a�
    Wrapper for np.percentile that skips missing values.

    Parameters
    ----------
    values : array over which to find quantiles
    q : scalar or array of quantile indices to find
    axis : {0, 1}
    na_value : scalar
        value to return for empty or all-null values
    mask : ndarray[bool]
        locations in values that should be considered missing
    ndim : {1, 2}
    interpolation : str

    Returns
    -------
    quantiles : scalar or array
    �m�Mr�r�)r�rc	s"g|]\}}t||���d��qS))r�)r�)r3�valr�)r�r�r�r.r/�
<listcomp>sz!nanpercentile.<locals>.<listcomp>F)r4r�)r]r�N)r�r�)r4�kind�
nanpercentiler�r�rr rHr�r�Zreshaper��T�zip�listrLr�r�)rGr�r]r�r`r�r�rkr.)r�r�r�r/r��s$r�)rGr^r+c	Cs�tjdtjftjjtjtjftjdtjftjjtjtjfi|\}}|jj	dk�r,|j}t
|�}|tjjkr�|jd�}tjtj
�j||<d}n|}d}||jd�dd	�}	|r�t|	|<n8|tjjkr�tj|�j�d}
t|
�r�t|	d
|
d�<|�r�t||<t|tj��r|	j|�}	nt|�j|	|d�}	nZ|�rzt|jjtjtjf��rz|j�}t
|�}|||<||dd	�}	||	|<n||dd	�}	|	S)
a
    Cumulative function with skipna support.

    Parameters
    ----------
    values : np.ndarray or ExtensionArray
    accum_func : {np.cumprod, np.maximum.accumulate, np.cumsum, np.minimum.accumulate}
    skipna : bool

    Returns
    -------
    np.ndarray or ExtensionArray
    g�?gr�r�r�TFr)r]N)r4)r�r�)rLZcumprodrA�maximum�
accumulater~ZcumsumZminimumr4r�r%r�Ziinfor�r�rr�Znonzerorarxrrr2Z_from_sequencer=r�Zbool_r�)rGZ
accum_funcr^Zmask_aZmask_br�r`r��changedrkZnz�valsr.r.r/�
na_accum_funcsD


 
r�)T)NN)NNN)N)NTN)NTN)NTrN)NTN)NTN)NTr�N)NTr�N)NTr�N)NTN)NTN)NTN)NTN)NTrN)r�)r�N)Nr�)trSrE�operatorZtypingrrrrrZnumpyrLZpandas._configrZpandas._libsrr	r
rrZpandas._typingr
rrrrZpandas.compat._optionalrZpandas.core.dtypes.castrrZpandas.core.dtypes.commonrrrrrrrrrrrr r!r"r#Zpandas.core.dtypes.dtypesr$Zpandas.core.dtypes.missingr%r&r'Zpandas.core.constructionr(ror,r-rXr0r1rZ�strrfrhr�rrr�r4r�r}r�rsrer�r�r�rtr�r�r�r�r�r�r�ZnanminZnanmaxr�r�r�r�rur�r�r�r�r�r�r�r�r�r��gtZnangt�geZnange�ltZnanlt�leZnanle�eqZnaneq�neZnanner�r�r�r.r.r.r/�<module>sD 4-(O 
%" &9M*(#C*&&NW!- 	$ 






 6