🔍 Code Extractor

function isfinite

Maturity: 46

Extended version of numpy.isfinite that handles additional data types including None, strings, datetime objects, masked arrays, and dask arrays.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
853 - 891
Complexity:
complex

Purpose

This function provides a comprehensive finite value check that extends numpy's isfinite functionality to support a wider range of data types commonly encountered in data analysis workflows. It handles scalar values, numpy arrays, pandas objects, dask arrays, masked arrays, datetime/timedelta types, and string types. The function is particularly useful in data validation and preprocessing pipelines where diverse data types need to be checked for finite values.

Source Code

def isfinite(val):
    """
    Helper function to determine if scalar or array value is finite extending
    np.isfinite with support for None, string, datetime types.
    """
    is_dask = is_dask_array(val)
    if not np.isscalar(val) and not is_dask:
        if isinstance(val, np.ma.core.MaskedArray):
            return ~val.mask & isfinite(val.data)
        elif isinstance(val, masked_types):
            return ~val.isna() & isfinite(val._data)
        val = asarray(val, strict=False)

    if val is None:
        return False
    elif is_dask:
        import dask.array as da
        return da.isfinite(val)
    elif isinstance(val, np.ndarray):
        if val.dtype.kind == 'M':
            return ~isnat(val)
        elif val.dtype.kind == 'O':
            return np.array([isfinite(v) for v in val], dtype=bool)
        elif val.dtype.kind in 'US':
            return ~pd.isna(val) if pd else np.ones_like(val, dtype=bool)
        finite = np.isfinite(val)
        if pd and pandas_version >= LooseVersion('1.0.0'):
            finite &= ~pd.isna(val)
        return finite
    elif isinstance(val, datetime_types+timedelta_types):
        return not isnat(val)
    elif isinstance(val, (str, bytes)):
        return True
    finite = np.isfinite(val)
    if pd and pandas_version >= LooseVersion('1.0.0'):
        if finite is pd.NA:
            return False
        return finite & (~pd.isna(val))
    return finite

Parameters

Name Type Default Kind
val - - positional_or_keyword

Parameter Details

val: The value to check for finiteness. Can be a scalar (int, float, None, string, datetime, timedelta), numpy array (including masked arrays), pandas Series/Index, dask array, or any array-like object. The function adapts its behavior based on the input type.

Return Value

Returns a boolean or boolean array indicating whether the input value(s) are finite. For scalar inputs, returns a single boolean. For array-like inputs, returns a boolean array of the same shape. Returns False for None, True for strings/bytes, handles NaT (Not-a-Time) for datetime types, and properly masks invalid values in masked arrays. For pandas NA values (pandas >= 1.0.0), returns False.

Dependencies

  • numpy
  • pandas
  • dask
  • dask.array

Required Imports

import numpy as np
import pandas as pd
from packaging.version import Version as LooseVersion

Conditional/Optional Imports

These imports are only needed under specific conditions:

import dask.array as da

Condition: only when val is a dask array (checked via is_dask_array function)

Optional

Usage Example

import numpy as np
import pandas as pd
from packaging.version import Version as LooseVersion

# Assuming helper functions and types are defined
# Example with scalar
result = isfinite(3.14)
# Returns: True

result = isfinite(None)
# Returns: False

result = isfinite(np.inf)
# Returns: False

# Example with array
arr = np.array([1.0, 2.0, np.inf, np.nan, 3.0])
result = isfinite(arr)
# Returns: array([True, True, False, False, True])

# Example with datetime
import datetime as dt
result = isfinite(dt.datetime.now())
# Returns: True

# Example with string
result = isfinite('hello')
# Returns: True

# Example with masked array
masked_arr = np.ma.array([1, 2, 3], mask=[False, True, False])
result = isfinite(masked_arr)
# Returns: array([True, False, True])

Best Practices

  • This function requires several helper functions (is_dask_array, asarray, isnat) and type definitions (masked_types, datetime_types, timedelta_types) to be available in scope
  • The function handles pandas NA values differently based on pandas version (>= 1.0.0), ensure pandas_version is properly defined
  • For large arrays, consider the performance implications of object dtype arrays which iterate element-by-element
  • The function lazy-imports dask.array only when needed, which is efficient but requires dask to be installed if dask arrays are used
  • When working with masked arrays, the function properly combines mask information with finite checks
  • String and bytes types always return True, which may not be intuitive but is consistent with the extended finite concept
  • For datetime arrays with 'M' dtype kind, the function checks for NaT (Not-a-Time) instead of using np.isfinite

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function isdatetime 63.9% similar

    Determines whether a given value (array or scalar) is a recognized datetime type, checking both NumPy datetime64 arrays and Python datetime objects.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_float 61.0% similar

    A type-checking utility function that determines whether a given object is a floating-point scalar value, supporting both Python's native float type and NumPy floating-point types.

    From: /tf/active/vicechatdev/patches/util.py
  • function isscalar 60.3% similar

    Checks if a value is a scalar type, None, or a datetime-related type.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_nan 59.9% similar

    A type-safe utility function that checks whether a given value is NaN (Not a Number), handling arbitrary types without raising exceptions.

    From: /tf/active/vicechatdev/patches/util.py
  • function isnumeric 58.9% similar

    Determines whether a given value can be converted to a numeric type (int or float), excluding strings and boolean types.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse