-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
median raises assertion error for an empty array #9498
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @adityav1810, Thank you for your contribution to Numba. 😄
I think the tests you're looking for is in numba/tests/test_array_reductions.py::TestArrayReductions::check_median_basic, the actual test itself (on line 343 of same file) is quite confusing, so you'd want to simply add a empty array case in this particular function and run the check
function on it.
Also do remember to run flake8
on the file that you change.
Feel free to ask questions if any.
Hi @kc611 . Thanks for the help. I added the test and ran |
numba/np/arraymath.py
Outdated
if n != 0: | ||
return _median_inner(temp_arry, n) | ||
else: | ||
return np.nan |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You cannot call np.median
when the array dtype is datetime*
and it is empty:
>>> import numpy as np
>>> a = np.array([]).astype(np.datetime64)
>>> np.median(a)
/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/fromnumeric.py:3474: RuntimeWarning: Mean of empty slice.
return _methods._mean(a, axis=axis, dtype=dtype,
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<__array_function__ internals>", line 180, in median
File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3793, in median
r, k = _ureduce(a, func=_median, axis=axis, out=out,
File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3702, in _ureduce
r = func(a, **kwargs)
File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/lib/function_base.py", line 3847, in _median
rout = mean(part[indexer], axis=axis, out=out)
File "<__array_function__ internals>", line 180, in mean
File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/fromnumeric.py", line 3474, in mean
return _methods._mean(a, axis=axis, dtype=dtype,
File "/Users/guilhermeleobas/micromamba/envs/numba/lib/python3.9/site-packages/numpy/core/_methods.py", line 179, in _mean
ret = umr_sum(arr, axis, dtype, out, keepdims, where=where)
numpy.core._exceptions._UFuncBinaryResolutionError: ufunc 'add' cannot use operands with types dtype('<M8') and dtype('<M8')
Changing your code to the one below should fix the issue:
@overload(np.median)
def np_median(a):
if not isinstance(a, types.Array):
return
is_datetime = as_dtype(a.dtype).char in 'mM'
def median_impl(a):
# np.median() works on the flattened array, and we need a temporary
# workspace anyway
temp_arry = a.flatten()
n = temp_arry.shape[0]
if not is_datetime and n == 0:
return np.nan
return _median_inner(temp_arry, n)
return median_impl
Co-authored-by: Guilherme Leobas <[email protected]>
Regarding the datetime64 failure, I think this may be a bug on NumPy: |
@adityav1810 Can you add a release notes file to your PR? |
This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks! |
This pull request is marked as stale as it has had no activity in the past 3 months. Please respond to this comment if you're still interested in working on this. Many thanks! |
This is my first time contributing to an opensource project.
Solving issue #9433
It would be great if someone could help me figure out where can i add some test cases to support this solution.