Skip to content
Merged
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
3 changes: 2 additions & 1 deletion chainladder/utils/sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def cumprod(a, axis=None, dtype=None, out=None):
return array(np.cumprod(a.todense(), axis=axis, dtype=dtype, out=out))


def floor(x):
def floor(x: COO) -> COO:
x = x.copy()
x.data = np.floor(x.data)
return x

Expand Down
9 changes: 6 additions & 3 deletions chainladder/utils/tests/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,17 @@ def test_floor_rounds_down() -> None:
np.testing.assert_array_equal(result.todense(), [1.0, 2.0, -1.0])


def test_floor_mutates_in_place() -> None:
def test_floor_returns_copy() -> None:
"""
Checks in-place mutation of floor function.
Checks that floor returns a copy and does not mutate its input,
mirroring np.floor (see issue #740).

Returns
-------
None
"""
a = array([1.2, 2.7, -0.3])
result: COO = floor(a)
assert result is a
assert result is not a
np.testing.assert_array_equal(result.todense(), [1.0, 2.0, -1.0])
np.testing.assert_array_equal(a.todense(), [1.2, 2.7, -0.3])
Loading