Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions doc/release/upcoming_changes/30098.deprecation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
`numpy.fix` is deprecated
-------------------------
The `numpy.fix` function is deprecated and will be removed in a future version
of NumPy. Use `numpy.trunc` instead, which provides identical functionality.
Both functions round towards zero (truncate), but `numpy.trunc` is the standard
name used across NumPy and other numerical libraries.
8 changes: 8 additions & 0 deletions numpy/_core/tests/test_deprecations.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,11 @@ class TestTooManyArgsExtremum(_DeprecationTestCase):
@pytest.mark.parametrize("ufunc", [np.minimum, np.maximum])
def test_extremem_3_args(self, ufunc):
self.assert_deprecated(ufunc, args=(np.ones(1), np.zeros(1), np.empty(1)))


class TestFixDeprecation(_DeprecationTestCase):
# Deprecated in Numpy 2.5, 2025-10-30
message = "Calling numpy.fix is deprecated"

def test_assert_warns_deprecated(self):
self.assert_deprecated(lambda: np.fix(1))
23 changes: 12 additions & 11 deletions numpy/lib/_ufunclike_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"""
__all__ = ['fix', 'isneginf', 'isposinf']

import warnings

import numpy._core.numeric as nx
from numpy._core.overrides import array_function_dispatch

Expand All @@ -18,6 +20,9 @@ def fix(x, out=None):
"""
Round to nearest integer towards zero.

.. deprecated:: 2.5.0
`numpy.fix` is deprecated. Use `numpy.trunc` instead.

Round an array of floats element-wise to nearest integer towards zero.
The rounded values have the same data-type as the input.

Expand All @@ -44,18 +49,14 @@ def fix(x, out=None):
--------
rint, trunc, floor, ceil
around : Round to given number of decimals

Examples
--------
>>> import numpy as np
>>> np.fix(3.14)
3.0
>>> np.fix(3)
3
>>> np.fix([2.1, 2.9, -2.1, -2.9])
array([ 2., 2., -2., -2.])

"""
# NumPy 2.5.0, 2025-10-30
warnings.warn(
'Calling numpy.fix is deprecated since NumPy 2.5.0. '
'Use numpy.trunc instead.',
DeprecationWarning,
stacklevel=2
)
return nx.trunc(x, out=out)


Expand Down
20 changes: 16 additions & 4 deletions numpy/lib/_ufunclike_impl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,28 @@ _ArrayT = TypeVar("_ArrayT", bound=NDArray[Any])

@overload
@deprecated("np.fix will be deprecated in NumPy 2.5 in favor of np.trunc", category=PendingDeprecationWarning)
def fix(x: _FloatLike_co, out: None = None) -> floating: ...
def fix( # type: ignore[misc]
x: _FloatLike_co,
out: None = None,
) -> floating: ...
@overload
@deprecated("np.fix will be deprecated in NumPy 2.5 in favor of np.trunc", category=PendingDeprecationWarning)
def fix(x: _ArrayLikeFloat_co, out: None = None) -> NDArray[floating]: ...
def fix(
x: _ArrayLikeFloat_co,
out: None = None,
) -> NDArray[floating]: ...
@overload
@deprecated("np.fix will be deprecated in NumPy 2.5 in favor of np.trunc", category=PendingDeprecationWarning)
def fix(x: _ArrayLikeObject_co, out: None = None) -> NDArray[object_]: ...
def fix(
x: _ArrayLikeObject_co,
out: None = None,
) -> NDArray[object_]: ...
@overload
@deprecated("np.fix will be deprecated in NumPy 2.5 in favor of np.trunc", category=PendingDeprecationWarning)
def fix(x: _ArrayLikeFloat_co | _ArrayLikeObject_co, out: _ArrayT) -> _ArrayT: ...
def fix(
x: _ArrayLikeFloat_co | _ArrayLikeObject_co,
out: _ArrayT,
) -> _ArrayT: ...

@overload
def isposinf( # type: ignore[misc]
Expand Down
64 changes: 39 additions & 25 deletions numpy/lib/tests/test_ufunclike.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np
from numpy import fix, isneginf, isposinf
from numpy.testing import assert_, assert_array_equal, assert_equal, assert_raises
Expand Down Expand Up @@ -40,12 +42,16 @@ def test_fix(self):
out = np.zeros(a.shape, float)
tgt = np.array([[1., 1., 1., 1.], [-1., -1., -1., -1.]])

res = fix(a)
assert_equal(res, tgt)
res = fix(a, out)
assert_equal(res, tgt)
assert_equal(out, tgt)
assert_equal(fix(3.14), 3)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "Calling numpy.fix is deprecated", DeprecationWarning
)
res = fix(a)
assert_equal(res, tgt)
res = fix(a, out)
assert_equal(res, tgt)
assert_equal(out, tgt)
assert_equal(fix(3.14), 3)

def test_fix_with_subclass(self):
class MyArray(np.ndarray):
Expand All @@ -67,17 +73,21 @@ def __array_finalize__(self, obj):

a = np.array([1.1, -1.1])
m = MyArray(a, metadata='foo')
f = fix(m)
assert_array_equal(f, np.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')

# check 0d arrays don't decay to scalars
m0d = m[0, ...]
m0d.metadata = 'bar'
f0d = fix(m0d)
assert_(isinstance(f0d, MyArray))
assert_equal(f0d.metadata, 'bar')
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "Calling numpy.fix is deprecated", DeprecationWarning
)
f = fix(m)
assert_array_equal(f, np.array([1, -1]))
assert_(isinstance(f, MyArray))
assert_equal(f.metadata, 'foo')

# check 0d arrays don't decay to scalars
m0d = m[0, ...]
m0d.metadata = 'bar'
f0d = fix(m0d)
assert_(isinstance(f0d, MyArray))
assert_equal(f0d.metadata, 'bar')

def test_scalar(self):
x = np.inf
Expand All @@ -87,11 +97,15 @@ def test_scalar(self):
assert_equal(type(actual), type(expected))

x = -3.4
actual = np.fix(x)
expected = np.float64(-3.0)
assert_equal(actual, expected)
assert_equal(type(actual), type(expected))

out = np.array(0.0)
actual = np.fix(x, out=out)
assert_(actual is out)
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", "Calling numpy.fix is deprecated", DeprecationWarning
)
actual = np.fix(x)
expected = np.float64(-3.0)
assert_equal(actual, expected)
assert_equal(type(actual), type(expected))

out = np.array(0.0)
actual = np.fix(x, out=out)
assert_(actual is out)
2 changes: 2 additions & 0 deletions numpy/tests/test_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def test_warning_calls():
for path in base.rglob("*.py"):
if base / "testing" in path.parents:
continue
if base / "typing" / "tests" / "data" in path.parents:
continue
if path == base / "__init__.py":
continue
if path == base / "random" / "__init__.py":
Expand Down
15 changes: 9 additions & 6 deletions numpy/typing/tests/data/pass/ufunclike.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import warnings
from typing import Any

import numpy as np
Expand Down Expand Up @@ -32,12 +33,14 @@ def __array__(self, dtype: np.typing.DTypeLike | None = None,
AR_LIKE_O = [Object(), Object(), Object()]
AR_U: np.ndarray[Any, np.dtype[np.str_]] = np.zeros(3, dtype="U5")

np.fix(AR_LIKE_b) # type: ignore[deprecated]
np.fix(AR_LIKE_u) # type: ignore[deprecated]
np.fix(AR_LIKE_i) # type: ignore[deprecated]
np.fix(AR_LIKE_f) # type: ignore[deprecated]
np.fix(AR_LIKE_O) # type: ignore[deprecated]
np.fix(AR_LIKE_f, out=AR_U) # type: ignore[deprecated]
with warnings.catch_warnings():
warnings.filterwarnings("ignore", "Calling numpy.fix is deprecated", DeprecationWarning)
np.fix(AR_LIKE_b) # type: ignore[deprecated]
np.fix(AR_LIKE_u) # type: ignore[deprecated]
np.fix(AR_LIKE_i) # type: ignore[deprecated]
np.fix(AR_LIKE_f) # type: ignore[deprecated]
np.fix(AR_LIKE_O) # type: ignore[deprecated]
np.fix(AR_LIKE_f, out=AR_U) # type: ignore[deprecated]

np.isposinf(AR_LIKE_b)
np.isposinf(AR_LIKE_u)
Expand Down
Loading