Skip to content

Commit

Permalink
Prevent np.asarray from destroying series indices
Browse files Browse the repository at this point in the history
  • Loading branch information
has2k1 committed Oct 17, 2018
1 parent 6bd20f6 commit 48a3224
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
10 changes: 10 additions & 0 deletions doc/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Changelog
=========

v0.5.2
------
*(2018-17-10)*

Bug Fixes
*********

- Fixed issue where some functions that took pandas series
would return output where the index did not match that of the input.

v0.5.1
------
*(2018-15-10)*
Expand Down
16 changes: 12 additions & 4 deletions mizani/bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ def rescale_mid(x, to=(0, 1), _from=None, mid=0):
array_like = False
x = [x]

x = np.asarray(x)
if not hasattr(x, 'dtype'):
x = np.asarray(x)

if _from is None:
_from = np.array([np.min(x), np.max(x)])
else:
Expand Down Expand Up @@ -174,7 +176,9 @@ def rescale_max(x, to=(0, 1), _from=None):
array_like = False
x = [x]

x = np.asarray(x)
if not hasattr(x, 'dtype'):
x = np.asarray(x)

if _from is None:
_from = np.array([np.min(x), np.max(x)])

Expand Down Expand Up @@ -210,7 +214,9 @@ def squish_infinite(x, range=(0, 1)):
[0.0, -10.0, 0.5, 0.25, 9.0]
"""
xtype = type(x)
x = np.asarray(x)

if not hasattr(x, 'dtype'):
x = np.asarray(x)

x[x == -np.inf] = range[0]
x[x == np.inf] = range[1]
Expand Down Expand Up @@ -247,7 +253,9 @@ def squish(x, range=(0, 1), only_finite=True):
[0.0, 0.0, 0.2, 0.5, 0.8, 1.0, 1.0]
"""
xtype = type(x)
x = np.asarray(x)

if not hasattr(x, 'dtype'):
x = np.asarray(x)

finite = np.isfinite(x) if only_finite else True

Expand Down
20 changes: 20 additions & 0 deletions mizani/tests/test_bounds.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ def test_rescale_max():
# branches #
assert rescale_max(2, _from=(0, 10)) == 0.2

# Maintains the same index
s = pd.Series([1, 2, 3], index=[3, 2, 1])
result = rescale_max(s)
assert s.index.equals(result.index)


def test_rescale_mid():
a = [1, 2, 3]
Expand All @@ -272,6 +277,11 @@ def test_rescale_mid():
rescale_mid([2], _from=(2, 2), to=(2, 2), mid=2),
[2])

# Maintains the same index
s = pd.Series([1, 2, 3], index=[3, 2, 1])
result = rescale_mid(s, mid=1)
assert s.index.equals(result.index)


def test_squish_infinite():
a = [-np.inf, np.inf, -np.inf, np.inf]
Expand All @@ -283,6 +293,11 @@ def test_squish_infinite():
npt.assert_allclose(squish_infinite(b, (1, 10)),
[5, 1, 2, 3, 6])

# Maintains the same index
s = pd.Series([1, 2, 3, 4], index=[4, 3, 2, 1])
result = squish_infinite(s)
assert s.index.equals(result.index)


def test_squish():
a = [-np.inf, np.inf, -np.inf, np.inf]
Expand All @@ -300,6 +315,11 @@ def test_squish():
[5, 1, 2, 3, 6])
npt.assert_allclose(squish(c, (1, 10)), c)

# Maintains the same index
s = pd.Series([.1, .2, .3, 9], index=[4, 3, 2, 1])
result = squish(s)
assert s.index.equals(result.index)


def test_zero_range():
c = np.array
Expand Down
5 changes: 5 additions & 0 deletions mizani/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ def test_round_any():
assert round_any(x, 5) == 5
assert round_any(x, 1.5) == 4.5

# Maintains the same index
s = pd.Series([1.1, 2.2, 3.3], index=[3, 2, 1])
result = round_any(s, 2)
assert s.index.equals(result.index)


def test_min_max():
x = [1, 2, 3, 4, 5]
Expand Down
8 changes: 6 additions & 2 deletions mizani/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ def round_any(x, accuracy, f=np.round):
"""
Round to multiple of any number.
"""
x = np.asarray(x)
if not hasattr(x, 'dtype'):
x = np.asarray(x)

return f(x / accuracy) * accuracy


Expand All @@ -66,7 +68,9 @@ def min_max(x, na_rm=False, finite=True):
out : tuple
(minimum, maximum) of x
"""
x = np.asarray(x)
if not hasattr(x, 'dtype'):
x = np.asarray(x)

if na_rm and finite:
x = x[np.isfinite(x)]
elif not na_rm and np.any(np.isnan(x)):
Expand Down

0 comments on commit 48a3224

Please sign in to comment.