Skip to main content

GitHub Workflow CI Status Supported Versions PyPI Ruff

intnan

Integer data types lack special values for -inf, inf and NaN. Especially NaN as an indication for missing data would be useful in many scientific contexts.

Of course there is numpy.ma.MaskedArray around for the very same reason. Nevertheless, it might sometimes be annoying to carry a separate mask array around — and masked arrays are notoriously slow. In those cases, using a set of numpy-compatible functions for the same job will do just fine.

This package provides such an implementation for a set of standard numpy functions, treating integer arrays in such a way, that a designated sentinel value resembles NaN:

  • For signed integer types, the lowest negative value (np.iinfo(dtype).min) is used as the missing value, e.g. -2147483648 for int32. Large negative values are chosen deliberately, so that python indexing (from the end) is unlikely to run into them accidentally.
  • For unsigned integer types, the value 0 is used.
  • For float and string types, the corresponding NaN, empty bytes or empty string values are used.
  • For object arrays, None is used.

Installation

pip install intnan

or with uv:

uv pip install intnan

The package requires Python 3.12+ and works with numpy 2.4+. All public functions are fully type annotated using numpy.typing.

Usage

Simply import the package and use the provided functions like their numpy counterparts:

import numpy as np
import intnan

a = np.array([1, -(2**31), 3], dtype=np.int32)  # -(2**31) marks a missing value

intnan.isnan(a)  # array([False,  True, False])
intnan.nansum(a)  # 4
intnan.nanmean(a)  # 2.0
intnan.fix_invalid(a)  # array([1, 0, 3], dtype=int32)

Functions

The following functions are provided by intnan. Where applicable, their semantics mirror the corresponding numpy function, with missing values ignored instead of propagated.

Missing value handling:

  • nanval(x) — return the missing value for a given array or data type
  • isnan(x) — boolean mask of missing values, works on arrays and scalars
  • fix_invalid(x, copy=True, fill_value=0) — replace missing values
  • asfloat(x) — convert to a float array, missing values become NaN
  • asint(x) — convert to an integer array, missing values become the integer missing value
  • anynan(x), allnan(x) — test for the presence of missing values

Reductions:

  • nanmax(x), nanmin(x) and their index counterparts nanargmax(x), nanargmin(x)
  • nansum(x), nanprod(x), nancumsum(x), nancumprod(x)
  • nanmean(x), nanmedian(x), nanvar(x, ddof=0), nanstd(x, ddof=0)

Element-wise binary operations:

  • nanmaximum(x, y), nanminimum(x, y) — as np.maximum/np.minimum, but picking the valid value wherever one input is missing

Comparison:

  • nanequal(x, y) — element-wise equality, treating missing values as an ordinary value
  • nanclose(x, y, delta=sys.float_info.epsilon) — element-wise closeness with tolerance delta

Performance

The library ships two interchangeable implementations:

  • intnan_np — based purely on vectorized numpy operations
  • intnan_numba — JIT-compiled with numba for functions that allow major speed gains

Both provide the identical API. On import, the numba implementation is automatically selected whenever numba is installed and importable; otherwise the numpy implementation is used. This makes numba an optional runtime dependency. Compiled numba kernels are cached on disk, so no recompilation overhead occurs after the first use.

To get the accelerated implementation, simply install numba alongside:

pip install intnan numba

Development

The project uses uv for dependency management:

git clone https://github.com/ml31415/intnan
cd intnan
uv sync          # create virtualenv and install all dependencies
uv run pre-commit install  # optional: run lint, format and type checks on every commit
uv run pytest    # run the test suite
uv run ruff check . && uv run ruff format --check .  # lint and format check
uv run mypy      # type check

Tests are run against both implementations and a range of dtypes (int32, int64, float32, float64) in the CI on Python 3.12 through 3.14.

License

BSD 3-Clause, see LICENSE.txt.

Metadata

Release files for intnan 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for intnan 0.3.1
File Size Uploaded
intnan-0.3.1.tar.gz 56.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for intnan 0.3.1
File Interpreter ABI Platform
intnan-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 68.2 kB

Release files / intnan-0.3.1.tar.gz

Download URL intnan-0.3.1.tar.gz
Size 56.0 kB
Tags Source
SHA-256 checksum
How to use checksums
b920e37f6a7f5eabed816e1822e6b730ca19dfc4278ed719976a06a4401a444d
BLAKE2b-256 checksum
How to use checksums
88512354bdd5c3d4a9bdd37e6070649a7af03b8a36040d59a8b565b905b00647
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / intnan-0.3.1-py3-none-any.whl

Download URL intnan-0.3.1-py3-none-any.whl
Size 12.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74c19d7da7548ce81e20cf8ade20b910c14df1e1c623114eb6235c8ec617a20f
BLAKE2b-256 checksum
How to use checksums
9ddeadeecc67f226e755fd1e811923b5fa65015741d81ddad7db64dd28cb0589
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.2.1

2 release files

0.1.4

1 release file

0.1.3

1 release file

0.1.2

2 release files

0.1.1

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page