function is_float
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.
/tf/active/vicechatdev/patches/util.py
1273 - 1277
simple
Purpose
This function provides a unified way to check if an object is a floating-point scalar, handling both standard Python floats and NumPy floating-point types (float16, float32, float64, etc.). It's commonly used in data validation, type checking, and preprocessing pipelines where distinguishing floating-point scalars from other numeric types (integers, complex numbers) or collections is necessary.
Source Code
def is_float(obj):
"""
Checks if the argument is a floating-point scalar.
"""
return isinstance(obj, (float, np.floating))
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
obj |
- | - | positional_or_keyword |
Parameter Details
obj: The object to be checked. Can be any Python object. The function will return True only if this object is an instance of Python's built-in float type or any NumPy floating-point type (np.float16, np.float32, np.float64, np.floating, etc.).
Return Value
Returns a boolean value: True if the input object is a floating-point scalar (either Python float or NumPy floating type), False otherwise. This includes all NumPy floating-point subtypes like float16, float32, float64, longdouble, etc.
Dependencies
numpy
Required Imports
import numpy as np
Usage Example
import numpy as np
def is_float(obj):
return isinstance(obj, (float, np.floating))
# Test with Python float
result1 = is_float(3.14)
print(result1) # True
# Test with NumPy float
result2 = is_float(np.float64(2.718))
print(result2) # True
# Test with integer
result3 = is_float(42)
print(result3) # False
# Test with string
result4 = is_float("3.14")
print(result4) # False
# Test with NumPy array
result5 = is_float(np.array([1.0, 2.0]))
print(result5) # False (array, not scalar)
# Test with different NumPy float types
result6 = is_float(np.float32(1.5))
print(result6) # True
Best Practices
- This function only checks for scalar floating-point values; it will return False for arrays or collections of floats
- The function uses isinstance() which is the Pythonic way to check types and properly handles inheritance (all NumPy floating types inherit from np.floating)
- This function does not perform type coercion; it only checks the actual type of the object
- For checking if a value can be converted to float (like the string '3.14'), use a different approach with try/except around float() conversion
- When working with pandas Series or DataFrames, this function checks the container type, not the dtype; use dtype checking methods for those cases
- NumPy's np.floating base class covers all NumPy float types (float16, float32, float64, float128/longdouble), making this check comprehensive for NumPy floats
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function isnumeric 71.5% similar
-
function is_number 71.1% similar
-
function is_nan 70.0% similar
-
function isscalar 68.8% similar
-
function isdatetime 65.2% similar