🔍 Code Extractor

function isnumeric

Maturity: 27

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

File:
/tf/active/vicechatdev/patches/util.py
Lines:
805 - 812
Complexity:
simple

Purpose

This function provides a robust check to determine if a value is numeric or can be safely converted to a numeric type. It explicitly excludes strings and booleans (including numpy booleans) from being considered numeric, even if they could technically be converted. This is useful for data validation, type checking in data processing pipelines, and filtering numeric values from mixed-type collections.

Source Code

def isnumeric(val):
    if isinstance(val, (str, bool, np.bool_)):
        return False
    try:
        float(val)
        return True
    except:
        return False

Parameters

Name Type Default Kind
val - - positional_or_keyword

Parameter Details

val: The value to check for numeric convertibility. Can be of any type including int, float, str, bool, numpy types, or any other Python object. The function will attempt to convert it to float to determine if it's numeric.

Return Value

Returns a boolean value: True if the value is numeric (can be converted to float) and is not a string or boolean type; False otherwise. Returns False for strings, booleans (both Python bool and numpy.bool_), and any value that raises an exception when converted to float.

Dependencies

  • numpy

Required Imports

import numpy as np

Usage Example

import numpy as np

def isnumeric(val):
    if isinstance(val, (str, bool, np.bool_)):
        return False
    try:
        float(val)
        return True
    except:
        return False

# Test with various types
print(isnumeric(42))           # True
print(isnumeric(3.14))         # True
print(isnumeric('123'))        # False (string excluded)
print(isnumeric(True))         # False (boolean excluded)
print(isnumeric(np.bool_(True))) # False (numpy boolean excluded)
print(isnumeric(np.int64(5)))  # True
print(isnumeric(None))         # False
print(isnumeric([1, 2, 3]))    # False

Best Practices

  • This function uses a bare except clause which catches all exceptions. While this makes it robust, it may hide unexpected errors during debugging.
  • The function explicitly excludes strings and booleans from being considered numeric, even though Python's float() can convert string representations of numbers and booleans to numeric values.
  • Consider using this function when you need strict numeric type checking that excludes string representations of numbers.
  • Be aware that this function will return True for numpy numeric types (int64, float64, etc.) but False for numpy boolean types.
  • The function does not distinguish between integers and floats - both are considered numeric.
  • For production code, consider replacing the bare except with specific exception handling (e.g., except (ValueError, TypeError)) for better error visibility.

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function is_number 77.5% similar

    Determines whether an object is a number or behaves like a number, with special handling for numpy types and numeric-like classes.

    From: /tf/active/vicechatdev/patches/util.py
  • function is_float 71.5% 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 is_nan 67.4% 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 isdatetime 61.8% 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 isscalar 61.0% similar

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

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