🔍 Code Extractor

function numpy_scalar_to_python

Maturity: 40

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.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
2218 - 2227
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function is_float 64.7% 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 isnumeric 58.5% 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
  • function asarray 58.1% similar

    Converts array-like objects (lists, pandas Series, objects with __array__ method) to NumPy ndarray format with optional strict validation.

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

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

    From: /tf/active/vicechatdev/patches/util.py
  • function clean_for_json_v3 55.0% similar

    Recursively converts Python objects (including NumPy and Pandas types) into JSON-serializable formats by handling special numeric types, NaN/Inf values, and nested data structures.

    From: /tf/active/vicechatdev/vice_ai/smartstat_scripts/f0a78968-1d2b-4fbe-a0c6-a372da2ce2a4/project_1/analysis.py
← Back to Browse