array_utils.py 文件源码

python
阅读 39 收藏 0 点赞 0 评论 0

项目:npstreams 作者: LaurentRDC 项目源码 文件源码
def nan_to_num(array, fill_value = 0.0, copy = True):
    """
    Replace NaNs with another fill value. 

    Parameters
    ----------
    array : array_like
        Input data.
    fill_value : float, optional
        NaNs will be replaced by ``fill_value``. Default is 0.0, in keeping
        with ``numpy.nan_to_num``.
    copy : bool, optional
        Whether to create a copy of `array` (True) or to replace values
        in-place (False). The in-place operation only occurs if
        casting to an array does not require a copy.

    Returns
    -------
    out : ndarray
        Array without NaNs. If ``array`` was not of floating or complearray type,
        ``array`` is returned unchanged.

    Notes
    -----
    Contrary to ``numpy.nan_to_num``, this functions does not handle
    infinite values.

    See Also
    --------
    numpy.nan_to_num : replace NaNs and Infs with zeroes.
    """
    array = np.array(array, subok = True, copy = copy)
    dtype = array.dtype.type

    # Non-inexact types do not have NaNs
    if not np.issubdtype(dtype, np.inexact):
        return array

    iscomplex = np.issubdtype(dtype, np.complexfloating)
    dest = (array.real, array.imag) if iscomplex else (array,)
    for d in dest:
        np.copyto(d, fill_value, where = np.isnan(d))
    return array
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号