Skip to content

Commit

Permalink
Merge pull request #25756 from charris/backport-24711
Browse files Browse the repository at this point in the history
BUG: Fix np.quantile([Fraction(2,1)], 0.5) (#24711)
  • Loading branch information
charris committed Feb 3, 2024
2 parents f62dfc6 + fee88ab commit ce89a0a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion numpy/lib/function_base.py
Expand Up @@ -4655,7 +4655,8 @@ def _lerp(a, b, t, out=None):
diff_b_a = subtract(b, a)
# asanyarray is a stop-gap until gh-13105
lerp_interpolation = asanyarray(add(a, diff_b_a * t, out=out))
subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5)
subtract(b, diff_b_a * (1 - t), out=lerp_interpolation, where=t >= 0.5,
casting='unsafe', dtype=type(lerp_interpolation.dtype))
if lerp_interpolation.ndim == 0 and out is None:
lerp_interpolation = lerp_interpolation[()] # unpack 0d arrays
return lerp_interpolation
Expand Down
4 changes: 4 additions & 0 deletions numpy/lib/tests/test_function_base.py
Expand Up @@ -3606,6 +3606,10 @@ def test_fraction(self):
assert_equal(q, Fraction(7, 2))
assert_equal(type(q), Fraction)

q = np.quantile(x, .5)
assert_equal(q, 1.75)
assert_equal(type(q), np.float64)

q = np.quantile(x, Fraction(1, 2))
assert_equal(q, Fraction(7, 4))
assert_equal(type(q), Fraction)
Expand Down

0 comments on commit ce89a0a

Please sign in to comment.