function numpy_scalar_to_python
Converts NumPy scalar types to their equivalent native Python types (float or int), returning the original value if it's not a NumPy numeric scalar.
/tf/active/vicechatdev/patches/util.py
2218 - 2227
simple
Purpose
This utility function provides type conversion from NumPy scalar types to Python built-in types. It's useful when interfacing between NumPy operations and code that expects native Python types, such as JSON serialization, database operations, or APIs that don't handle NumPy types. The function checks if the input is a NumPy float or integer subclass and converts accordingly, otherwise returns the input unchanged.
Source Code
def numpy_scalar_to_python(scalar):
"""
Converts a NumPy scalar to a regular python type.
"""
scalar_type = type(scalar)
if np.issubclass_(scalar_type, np.float_):
return float(scalar)
elif np.issubclass_(scalar_type, np.int_):
return int(scalar)
return scalar
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
scalar |
- | - | positional_or_keyword |
Parameter Details
scalar: A value to be converted, typically a NumPy scalar (np.float64, np.int32, etc.). Can be any type, but conversion only occurs for NumPy float and integer types. Non-NumPy types are returned unchanged.
Return Value
Returns a native Python type: float if the input is a NumPy floating-point scalar, int if the input is a NumPy integer scalar, or the original scalar unchanged if it's neither. The return type matches the semantic type of the input (numeric types remain numeric).
Dependencies
numpy
Required Imports
import numpy as np
Usage Example
import numpy as np
def numpy_scalar_to_python(scalar):
scalar_type = type(scalar)
if np.issubclass_(scalar_type, np.float_):
return float(scalar)
elif np.issubclass_(scalar_type, np.int_):
return int(scalar)
return scalar
# Example usage
np_float = np.float64(3.14)
py_float = numpy_scalar_to_python(np_float)
print(type(py_float), py_float) # <class 'float'> 3.14
np_int = np.int32(42)
py_int = numpy_scalar_to_python(np_int)
print(type(py_int), py_int) # <class 'int'> 42
# Non-NumPy types pass through unchanged
regular_str = "hello"
result = numpy_scalar_to_python(regular_str)
print(type(result), result) # <class 'str'> hello
Best Practices
- This function is designed for scalar values only, not arrays or sequences
- The function returns the original value unchanged if it's not a NumPy numeric type, making it safe to use on mixed-type data
- Useful for preparing NumPy data for JSON serialization or other contexts requiring native Python types
- Does not handle NumPy complex numbers, boolean types, or other specialized NumPy dtypes - these will be returned unchanged
- Consider using this function when interfacing with APIs or libraries that don't natively support NumPy types
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function is_float 64.7% similar
-
function isnumeric 58.5% similar
-
function asarray 58.1% similar
-
function isscalar 57.6% similar
-
function clean_for_json_v3 55.0% similar