function isnumeric
Determines whether a given value can be converted to a numeric type (int or float), excluding strings and boolean types.
/tf/active/vicechatdev/patches/util.py
805 - 812
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.
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function is_number 77.5% similar
-
function is_float 71.5% similar
-
function is_nan 67.4% similar
-
function isdatetime 61.8% similar
-
function isscalar 61.0% similar