Skip to content

Commit

Permalink
Do not modify arrays after np.asarray
Browse files Browse the repository at this point in the history
This change ensures that input arrays are not modified and also
prevents us from trying write to numpy arrays that have been
write protected by pandas copy-on-write mechanism. Use np.array with
copy=True.
  • Loading branch information
has2k1 committed May 17, 2024
1 parent c1e014f commit 9d8fe28
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
15 changes: 15 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
Changelog
=========

v0.11.4
-------
*not-yet-released*

Bug Fixes
---------

- Fixed :class:`~mizani.bounds.squish` and
:class:`~mizani.bounds.squish_infinite` so that they do not reuse
numpy arrays. The users object is not modified.

This also prevents exceptions where the numpy array backs a pandas
object and it is protected by copy-on-write.


v0.11.3
-------
*2024-05-09*
Expand Down
4 changes: 2 additions & 2 deletions mizani/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def squish_infinite(
>>> list(squish_infinite(arr2, (-10, 9)))
[0.0, -10.0, 0.5, 0.25, 9.0]
"""
_x = np.asarray(x)
_x = np.array(x, copy=True)
_x[np.isneginf(_x)] = range[0]
_x[np.isposinf(_x)] = range[1]
return _x
Expand Down Expand Up @@ -273,7 +273,7 @@ def squish(
>>> list(squish([-np.inf, -1.5, 0.2, 0.8, 1.0, np.inf], only_finite=False))
[0.0, 0.0, 0.2, 0.8, 1.0, 1.0]
"""
_x = np.asarray(x)
_x = np.array(x, copy=True)
finite = np.isfinite(_x) if only_finite else True
_x[np.logical_and(_x < range[0], finite)] = range[0]
_x[np.logical_and(_x > range[1], finite)] = range[1]
Expand Down
1 change: 1 addition & 0 deletions mizani/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
DurationUnit,
FloatArrayLike,
FloatSeries,
NDArrayAny,
NDArrayFloat,
NullType,
NumericUFunction,
Expand Down

0 comments on commit 9d8fe28

Please sign in to comment.