Skip to content

c108.numeric

Standardize numeric types from Python stdlib and third-party libraries.

This module provides robust numeric type conversion suitable for display formatting, data processing, and cross-library interoperability.

std_numeric(value, *, on_error='raise', allow_bool=False)

Convert numeric types to standardPython int or float.

Normalizes numeric values from Python stdlib and third-party libraries (NumPy, Pandas, Decimal, etc.) into standardPython types for display, serialization, and processing.

Design Philosophy

This function provides transparent type normalization without heuristics: - Types with index() → int (exact integer types) - Types with float() → float (all other numeric types) - No "smart" conversions based on numeric values

The source type's float() method handles overflow/underflow: - Overflow: values too large become inf/-inf - Underflow: values too small become 0.0/-0.0 - Special values (nan, inf) pass through unchanged

Parameters

value : various Numeric value to convert. Supports Python int/float/None, Decimal, Fraction, and third-party types via index, float, .item(), or .value protocols.

{"raise", "nan", "none"}, default "raise"

How to handle TYPE ERRORS (unsupported types like str, list, dict): - "raise": Raise TypeError (default) - "nan": Return float('nan') - "none": Return None

Note: Numeric edge cases (inf, nan) are valid values, not errors.

bool, default False

If True, convert bool to int (True→1, False→0). If False, treat bool as type error. Default False helps catch bugs since bool is a subclass of int in Python.

Returns

int For Python int (arbitrary precision) or types implementing index (NumPy integers, etc.).

float For all other numeric types via float(), including special IEEE 754 values (inf, -inf, nan, 0.0, -0.0).

None For None input, pandas.NA, numpy.ma.masked, or type errors when on_error="none".

Raises

TypeError When on_error="raise" and value is unsupported type or bool when allow_bool=False.

Type Conversion Rules

Integer types (via index): Python int, NumPy int8-64/uint8-64 → int (arbitrary precision)

Float types (via float): Python float, Decimal, Fraction, NumPy float16-128 → float

Special values (pass through as float): inf, -inf, nan → preserved unchanged

Overflow/underflow (handled by source type's float): Decimal('1e400').float() → inf Decimal('1e-400').float() → 0.0

Array scalars (via .item()): NumPy/PyTorch/TensorFlow/JAX tensor scalars extracted then converted

Physical quantities (via .value): Astropy Quantity → extract .value, discard units, convert

Missing data: None → None pandas.NA → nan numpy.ma.masked → nan

Common Types Supported

  • Python: int, float, None, Decimal, Fraction
  • NumPy: int/uint/float scalars, nan, inf, arrays via .item()
  • Pandas: numeric scalars, pd.NA
  • ML: PyTorch/TensorFlow/JAX scalars via .item()/.numpy()
  • Scientific: Astropy Quantity (via .value)
  • Any type with float() or index()

Examples

Basic types:

std_numeric(42) 42 std_numeric(3.14) 3.14 std_numeric(None) # Returns None

Decimal and Fraction (always become float):

from decimal import Decimal std_numeric(Decimal('42')) 42.0 std_numeric(Decimal('3.14')) 3.14

Overflow and underflow (handled by float):

std_numeric(Decimal('1e400')) inf std_numeric(Decimal('1e-400')) 0.0

Special values:

std_numeric(float('inf')) inf std_numeric(float('nan')) nan

Error handling:

std_numeric("invalid") Traceback (most recent call last): ... TypeError: unsupported numeric type: str std_numeric("invalid", on_error="nan") nan

Boolean handling:

std_numeric(True) Traceback (most recent call last): ... TypeError: boolean values not supported, got True. Set allow_bool=True to convert booleans to int std_numeric(True, allow_bool=True) 1

NumPy scalars and arrays:

import numpy as np # doctest: +SKIP std_numeric(np.int64(42)) # doctest: +SKIP 42 std_numeric(np.float32(123)) # doctest: +SKIP 123.0 std_numeric(np.array([42]).item()) # doctest: +SKIP 42

Pandas types:

import pandas as pd # doctest: +SKIP std_numeric(pd.Series([42]).iloc[0]) # doctest: +SKIP 42 std_numeric(pd.NA, on_error="nan") # doctest: +SKIP nan

PyTorch tensors:

import torch # doctest: +SKIP std_numeric(torch.tensor(42).item()) # doctest: +SKIP 42 std_numeric(torch.tensor(3.14, dtype=torch.float32).item()) # doctest: +SKIP 3.140000104904175

TensorFlow tensors:

import tensorflow as tf # doctest: +SKIP std_numeric(tf.constant(42).numpy().item()) # doctest: +SKIP 42 std_numeric(tf.constant(123).numpy()) # doctest: +SKIP 123

JAX arrays:

import jax.numpy as jnp # doctest: +SKIP std_numeric(jnp.array(42).item()) # doctest: +SKIP 42 std_numeric(jnp.array(3.14)) # doctest: +SKIP 3.140000104904175

Astropy quantities:

from astropy import units as u # doctest: +SKIP std_numeric((123 * u.second).value) # doctest: +SKIP 123.0

See Also

float() : Python built-in for float conversion int() : Python built-in for integer conversion

Source code in c108/numeric.py
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
def std_numeric(
    value: Any,
    *,
    on_error: Literal["raise", "nan", "none"] = "raise",
    allow_bool: bool = False,
) -> int | float | None:
    """
    Convert numeric types to standardPython int or float.

    Normalizes numeric values from Python stdlib and third-party libraries
    (NumPy, Pandas, Decimal, etc.) into standardPython types for display,
    serialization, and processing.

    Design Philosophy
    -----------------
    This function provides **transparent type normalization** without heuristics:
    - Types with __index__() → int (exact integer types)
    - Types with __float__() → float (all other numeric types)
    - No "smart" conversions based on numeric values

    The source type's __float__() method handles overflow/underflow:
    - Overflow: values too large become inf/-inf
    - Underflow: values too small become 0.0/-0.0
    - Special values (nan, inf) pass through unchanged

    Parameters
    ----------
    value : various
        Numeric value to convert. Supports Python int/float/None, Decimal,
        Fraction, and third-party types via __index__, __float__, .item(),
        or .value protocols.

    on_error : {"raise", "nan", "none"}, default "raise"
        How to handle TYPE ERRORS (unsupported types like str, list, dict):
        - "raise": Raise TypeError (default)
        - "nan": Return float('nan')
        - "none": Return None

        Note: Numeric edge cases (inf, nan) are valid values, not errors.

    allow_bool : bool, default False
        If True, convert bool to int (True→1, False→0). If False, treat
        bool as type error. Default False helps catch bugs since bool is
        a subclass of int in Python.

    Returns
    -------
    int
        For Python int (arbitrary precision) or types implementing __index__
        (NumPy integers, etc.).

    float
        For all other numeric types via __float__(), including special IEEE 754
        values (inf, -inf, nan, 0.0, -0.0).

    None
        For None input, pandas.NA, numpy.ma.masked, or type errors when
        on_error="none".

    Raises
    ------
    TypeError
        When on_error="raise" and value is unsupported type or bool when
        allow_bool=False.

    Type Conversion Rules
    ---------------------
    **Integer types** (via __index__):
        Python int, NumPy int8-64/uint8-64 → int (arbitrary precision)

    **Float types** (via __float__):
        Python float, Decimal, Fraction, NumPy float16-128 → float

    **Special values** (pass through as float):
        inf, -inf, nan → preserved unchanged

    **Overflow/underflow** (handled by source type's __float__):
        Decimal('1e400').__float__() → inf
        Decimal('1e-400').__float__() → 0.0

    **Array scalars** (via .item()):
        NumPy/PyTorch/TensorFlow/JAX tensor scalars extracted then converted

    **Physical quantities** (via .value):
        Astropy Quantity → extract .value, discard units, convert

    **Missing data**:
        None → None
        pandas.NA → nan
        numpy.ma.masked → nan

    Common Types Supported
    ----------------------
    - Python: int, float, None, Decimal, Fraction
    - NumPy: int/uint/float scalars, nan, inf, arrays via .item()
    - Pandas: numeric scalars, pd.NA
    - ML: PyTorch/TensorFlow/JAX scalars via .item()/.numpy()
    - Scientific: Astropy Quantity (via .value)
    - Any type with __float__() or __index__()

    Examples
    --------
    Basic types:
    >>> std_numeric(42)
    42
    >>> std_numeric(3.14)
    3.14
    >>> std_numeric(None) # Returns None

    Decimal and Fraction (always become float):
    >>> from decimal import Decimal
    >>> std_numeric(Decimal('42'))
    42.0
    >>> std_numeric(Decimal('3.14'))
    3.14

    Overflow and underflow (handled by __float__):
    >>> std_numeric(Decimal('1e400'))
    inf
    >>> std_numeric(Decimal('1e-400'))
    0.0

    Special values:
    >>> std_numeric(float('inf'))
    inf
    >>> std_numeric(float('nan'))
    nan

    Error handling:
    >>> std_numeric("invalid")
    Traceback (most recent call last):
        ...
    TypeError: unsupported numeric type: str
    >>> std_numeric("invalid", on_error="nan")
    nan

    Boolean handling:
    >>> std_numeric(True)
    Traceback (most recent call last):
        ...
    TypeError: boolean values not supported, got True. Set allow_bool=True to convert booleans to int
    >>> std_numeric(True, allow_bool=True)
    1

    NumPy scalars and arrays:
    >>> import numpy as np                          # doctest: +SKIP
    >>> std_numeric(np.int64(42))                   # doctest: +SKIP
    42
    >>> std_numeric(np.float32(123))                # doctest: +SKIP
    123.0
    >>> std_numeric(np.array([42]).item())          # doctest: +SKIP
    42

    Pandas types:
    >>> import pandas as pd                         # doctest: +SKIP
    >>> std_numeric(pd.Series([42]).iloc[0])        # doctest: +SKIP
    42
    >>> std_numeric(pd.NA, on_error="nan")          # doctest: +SKIP
    nan

    PyTorch tensors:
    >>> import torch                                # doctest: +SKIP
    >>> std_numeric(torch.tensor(42).item())        # doctest: +SKIP
    42
    >>> std_numeric(torch.tensor(3.14, dtype=torch.float32).item())     # doctest: +SKIP
    3.140000104904175

    TensorFlow tensors:
    >>> import tensorflow as tf                         # doctest: +SKIP
    >>> std_numeric(tf.constant(42).numpy().item())     # doctest: +SKIP
    42
    >>> std_numeric(tf.constant(123).numpy())           # doctest: +SKIP
    123

    JAX arrays:
    >>> import jax.numpy as jnp                         # doctest: +SKIP
    >>> std_numeric(jnp.array(42).item())               # doctest: +SKIP
    42
    >>> std_numeric(jnp.array(3.14))                    # doctest: +SKIP
    3.140000104904175

    Astropy quantities:
    >>> from astropy import units as u                  # doctest: +SKIP
    >>> std_numeric((123 * u.second).value)             # doctest: +SKIP
    123.0

    See Also
    --------
    float() : Python built-in for float conversion
    int() : Python built-in for integer conversion
    """

    def __is_bool_type(val: Any) -> bool:
        """Check if value is any kind of boolean."""
        val_type = type(val)
        val_type_name = val_type.__name__.lower()

        if val_type is bool:
            return True
        if "bool" in val_type_name:
            return True
        if isinstance(val, (bool, int)) and val_type_name in ("bool_", "bool8", "bool"):
            return True

        # Check for mock/custom __name__ attribute on the class
        if hasattr(val_type, "__name__"):
            custom_name = getattr(val_type, "__name__", "")
            if isinstance(custom_name, str) and "bool" in custom_name.lower():
                return True

        return False

    def __handle_error(error_type: str = "unsupported") -> int | float | None:
        """Handle errors based on on_error parameter."""
        if on_error == "raise":
            if error_type == "bool":
                raise TypeError("boolean values not supported")
            elif error_type == "complex":
                raise TypeError("complex numbers not supported")
            elif error_type == "collection":
                raise TypeError(
                    f"sizable collection not supported, got {type(value).__name__} "
                    f"with length {len(value)}. Extract scalar first"
                )
            else:  # unsupported
                raise TypeError(
                    f"unsupported numeric type: {type(value).__name__}. "
                    f"Expected int, float, or types with __index__, __float__, or .item()"
                )
        elif on_error == "nan":
            return float("nan")
        else:  # "none"
            return None

    def __handle_special_cases() -> int | float | None | bool:
        """Handle pandas.NA, numpy.ma.masked, and other special cases. Returns False if not handled."""
        # pandas.NA special case
        if hasattr(value, "__class__"):
            cls = value.__class__
            cls_name = getattr(cls, "__name__", "")
            cls_module = getattr(cls, "__module__", "")
            if cls_name == "NAType" and "pandas" in cls_module:
                return float("nan")

        # numpy.ma.masked special case
        if hasattr(value, "__class__"):
            cls = value.__class__
            if getattr(cls, "__name__", "") == "MaskedConstant" and getattr(
                cls, "__module__", ""
            ).startswith("numpy.ma"):
                return float("nan")

        return False

    def __extract_from_dtype() -> int | float | None | bool:
        """Extract and convert values from objects with dtype attribute. Returns False if not handled."""
        if not hasattr(value, "dtype"):
            return False

        try:
            dtype_str = str(value.dtype)

            # Reject complex types
            if "complex" in dtype_str:
                return __handle_error("complex")

            # Check for boolean dtype
            dtype_is_bool = False
            if hasattr(value.dtype, "is_bool"):
                try:
                    dtype_is_bool = value.dtype.is_bool
                except (AttributeError, TypeError):
                    pass
            if not dtype_is_bool and "bool" in dtype_str.lower():
                dtype_is_bool = True

            if dtype_is_bool and not allow_bool:
                return __handle_error("bool")
        except AttributeError:
            pass

        # Extract scalar from tensor
        result = None
        if hasattr(value, "item") and callable(value.item):
            try:
                result = value.item()
            except (TypeError, ValueError, AttributeError):
                pass

        if result is None and hasattr(value, "numpy") and callable(value.numpy):
            try:
                result = value.numpy()
            except (TypeError, ValueError, AttributeError):
                pass

        if result is not None:
            if __is_bool_type(result):
                if allow_bool:
                    return 1 if result else 0
                else:
                    return __handle_error("bool")

            if type(result) in (int, float) or result is None:
                return result
            elif isinstance(result, (int, float)):
                return int(result) if isinstance(result, int) else float(result)

        return False

    def __try_protocol_methods() -> int | float | None | bool:
        """Try various protocol methods (__index__, __float__, __int__). Returns False if not handled."""
        # __index__() for exact integer types
        if hasattr(value, "__index__"):
            try:
                return operator.index(value)
            except (TypeError, ValueError, AttributeError):
                pass

        # Astropy Quantity (has .value and .unit)
        if hasattr(value, "value") and hasattr(value, "unit"):
            try:
                magnitude = value.value
                return std_numeric(magnitude, on_error=on_error, allow_bool=allow_bool)
            except (TypeError, ValueError, AttributeError):
                pass

        # __float__() for all other numeric types
        if hasattr(value, "__float__"):
            try:
                return float(value)
            except OverflowError:
                try:
                    if value < 0:
                        return float("-inf")
                    else:
                        return float("inf")
                except (TypeError, AttributeError):
                    return float("inf")
            except (TypeError, ValueError) as e:
                if on_error == "raise":
                    raise TypeError(f"cannot convert {type(value).__name__} to float: {e}") from e
                elif on_error == "nan":
                    return float("nan")
                else:
                    return None

        # __int__() as last resort
        if hasattr(value, "__int__"):
            try:
                return int(value)
            except (TypeError, ValueError, OverflowError) as e:
                if on_error == "raise":
                    raise TypeError(f"cannot convert {type(value).__name__} to int: {e}") from e
                elif on_error == "nan":
                    return float("nan")
                else:
                    return None

        return False

    # ===== Main conversion logic =====

    # None passthrough
    if value is None:
        return None

    # Boolean handling
    if isinstance(value, bool):
        if allow_bool:
            return int(value)
        else:
            if on_error == "raise":
                raise TypeError(
                    f"boolean values not supported, got {value}. "
                    f"Set allow_bool=True to convert booleans to int"
                )
            elif on_error == "nan":
                return float("nan")
            else:
                return None

    # Standard Python types - fast path
    if type(value) in (int, float):
        return value

    # Handle special cases (pandas.NA, numpy.ma.masked)
    special_result = __handle_special_cases()
    if special_result is not False:
        return special_result

    # Reject array-like collections
    if hasattr(value, "__len__"):
        try:
            length = len(value)
        except TypeError:
            pass
        else:
            if on_error == "raise":
                if isinstance(value, str):
                    raise TypeError(f"unsupported numeric type: {type(value).__name__}")
                return __handle_error("collection")
            elif on_error == "nan":
                return float("nan")
            else:
                return None

    # Handle array/tensor scalars with dtype
    dtype_result = __extract_from_dtype()
    if dtype_result is not False:
        return dtype_result

    # Try .item() for non-tensor objects (e.g., pandas scalars)
    if hasattr(value, "item") and callable(value.item):
        try:
            result = value.item()
            if __is_bool_type(result):
                if allow_bool:
                    return 1 if result else 0
                else:
                    return __handle_error("bool")
            elif type(result) in (int, float) or result is None:
                return result
            elif isinstance(result, (int, float)):
                return int(result) if isinstance(result, int) else float(result)
        except (TypeError, ValueError, AttributeError):
            pass

    # SymPy Boolean handling (only when allow_bool=True)
    if allow_bool and hasattr(value, "__class__"):
        cls = value.__class__
        cls_name = cls.__dict__.get("__name__", cls.__name__ if hasattr(cls, "__name__") else "")
        cls_module = cls.__dict__.get(
            "__module__", cls.__module__ if hasattr(cls, "__module__") else ""
        )

        if "sympy" in cls_module and cls_name in ("BooleanTrue", "BooleanFalse"):
            try:
                return int(bool(value))
            except (TypeError, ValueError):
                return 1 if cls_name == "BooleanTrue" else 0

    # Try protocol methods (__index__, __float__, __int__)
    protocol_result = __try_protocol_methods()
    if protocol_result is not False:
        return protocol_result

    # Type not supported
    return __handle_error("unsupported")