Skip to content
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

Possible Solutions to Issues 26216, 25677, and 25589 #26276

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions issue_25589_tests.py
@@ -0,0 +1,66 @@
import numpy as np
from numpy.ma.testutils import assert_equal

array1 = np.ma.array([3, 4, 5, 6], mask = [False, False, False, False])
result1_array1 = array1 ** 3.5
assert_equal(result1_array1.data, np.array([46.76537180435969, 128.0, 279.5084971874737, 529.0897844411664]))
assert_equal(len(result1_array1.mask), 4)

result2_array1 = array1 ** 0.9
assert_equal(result2_array1.data, np.array([2.6878753795222865, 3.4822022531844965, 4.256699612603923, 5.015752812467621]))
assert_equal(len(result2_array1.mask), 4)

result3_array1 = array1 ** 1.75
assert_equal(result3_array1.data, np.array([6.8385211708643325, 11.313708498984761, 16.71850762441055, 23.00195175286581]))
assert_equal(len(result3_array1.mask), 4)

array2 = np.ma.array([10, 10.7, 1.5, 19.3412], mask = [False])
result1_array2 = array2 ** 1.2391
assert_equal(result1_array2.data, np.array([17.342032668438673, 18.858599562101034, 1.6527024811874533, 39.271871725802804]))
assert_equal(len(result1_array2.mask), 4)

result2_array2 = array2 ** -0.6783
assert_equal(result2_array2.data, np.array([0.20974904879785425, 0.20034060622191774, 0.7595516280961806, 0.1340844142280547]))
assert_equal(len(result2_array2.mask), 4)

result3_array2 = array2 ** 5.1948
assert_equal(result3_array2.data, np.array([156602.9720686047, 222557.8146985335, 8.217862539806553, 4819744.856544941]))
assert_equal(len(result3_array2.mask), 4)

array3 = np.ma.array([3.4, 1.2], mask = [False, False])
result1_array3 = array3 ** 7.8

assert_equal(result1_array3.data, np.array([13980.91363494161, 4.145851281415363]))
assert_equal(len(result1_array3.mask), 2)

result2_array3 = array3 ** 1.2
assert_equal(result2_array3.data, np.array([4.342848711597634, 1.2445647472039776]))
assert_equal(len(result2_array3.mask), 2)

result3_array3 = np.ma.power(array3, 6.7)
assert_equal(result3_array3.data, np.array([3638.3857677420656, 3.3924569758842456]))
assert_equal(len(result3_array3.mask), 2)

array4 = np.ma.array([7.1205], mask = False)
result1_array4 = np.ma.power(array4, 4.57)
assert_equal(result1_array4, np.array([7869.967571527348]))
assert_equal(len(result1_array4.mask), 1)

result2_array4 = np.ma.power(array4, 0)
assert_equal(result2_array4, np.array([1]))
assert_equal(len(result2_array4.mask), 1)

array5 = np.ma.array([6.7], mask = [False])
result1_array5 = array5 ** 2.1
assert_equal(result1_array5, np.array([54.294655975192626]))
assert_equal(len(result1_array5.mask), 1)

result2_array5 = array5 ** -0.345
assert_equal(result2_array5, np.array([0.518805047913752]))
assert_equal(len(result2_array5.mask), 1)

result3_array5 = np.ma.power(array5, 1.24)
assert_equal(result3_array5, np.array([10.576275504399648]))
assert_equal(len(result3_array5.mask), 1)

print("All tests passed!")
64 changes: 64 additions & 0 deletions issue_25677_tests.py
@@ -0,0 +1,64 @@
import numpy as np
from numpy.ma.testutils import assert_equal

float16_1 = np.ma.array([230.12, 5601, 1027.8, 12389.4219], mask = [0, 0, 1, 0], dtype = "float16")
assert_equal(float16_1.filled(), np.array([230.12, 5601, 65504, 12389.4219], dtype = np.float16))

float16_2 = np.ma.array([7.8, 1.204, -193], mask = [0, 0, 0], dtype = np.float16)
assert_equal(float16_2.filled(), np.array([7.8, 1.204, -193], dtype = np.float16))

float16_3 = np.ma.array([2951], mask = [1], dtype = np.float16)
assert_equal(float16_3.filled(), np.array([65504], dtype = np.float16))

float16_4 = np.ma.array([65504, 65504, 221.4612, 43391, -14583], mask = [1, 0, 1, 0, 1], dtype = np.float16)
assert_equal(float16_4.filled(), np.array([65504, 65504, 65504, 43391, 65504], dtype = np.float16))

int8_1 = np.ma.array([1, 2, 6, 7], mask = [1, 1, 0, 1], dtype = np.int8)
assert_equal(int8_1.filled(), np.array([127, 127, 6, 127], dtype = np.int8))

int8_2 = np.ma.array([127, 127], mask = [1, 0], dtype = np.int8)
assert_equal(int8_2.filled(), np.array([127, 127], dtype = np.int8))

int8_3 = np.ma.array([100], mask = [1], dtype = np.int8)
assert_equal(int8_3.filled(), np.array([127], dtype = np.int8))

int8_4 = np.ma.array([-14, 123, 104, -98], mask = [0, 0, 0, 1], dtype = np.int8)
assert_equal(int8_4.filled(), np.array([-14, 123, 104, 127], dtype = np.int8))

uint8_1 = np.ma.array([1, 5], mask = [0, 1], dtype = np.uint8)
assert_equal(uint8_1.filled(), np.array([1, 255], dtype = np.uint8))

uint8_2 = np.ma.array([19, 156], mask = [1, 1], dtype = np.uint8)
assert_equal(uint8_2.filled(), np.array([255, 255], dtype = np.uint8))

uint8_3 = np.ma.array([92, 57, 194, 1, 0, 12], mask = [1, 1, 1, 0, 1, 0], dtype = np.uint8)
assert_equal(uint8_3.filled(), np.array([255, 255, 255, 1, 255, 12], dtype = np.uint8))

uint8_4 = np.ma.array([1, 4, 12, 96, 45], mask = [0, 0, 0, 0, 1], dtype = np.uint8)
assert_equal(uint8_4.filled(), np.array([1, 4, 12, 96, 255], dtype = np.uint8))

int16_1 = np.ma.array([0, 120], mask = [1, 1], dtype = "int16")
assert_equal(int16_1.filled(), np.array([32767, 32767], dtype = np.int16))

int16_2 = np.ma.array([240], mask = [1], dtype = np.int16)
assert_equal(int16_2.filled(), np.array([32767], dtype = np.int16))

int16_3 = np.ma.array([-1, 42, 123, 50, 81, 217], mask = [0, 1, 0, 0, 0, 0], dtype = "int16")
assert_equal(int16_3.filled(), np.array([-1, 32767, 123, 50, 81, 217], dtype = np.int16))

int16_4 = np.ma.array([10, 128, -19, 1], mask = [0, 0, 1, 0], dtype = np.int16)
assert_equal(int16_4.filled(), np.array([10, 128, 32767, 1], dtype = np.int16))

uint16_1 = np.ma.array([1, 5], mask = [0, 1], dtype = "uint16")
assert_equal(uint16_1.filled(), np.array([1, 65535], dtype = np.uint16))

uint16_2 = np.ma.array([9, 79, 31, 111], mask = [1, 1, 1, 1], dtype = "uint16")
assert_equal(uint16_2.filled(), np.array([65535, 65535, 65535, 65535], dtype = np.uint16))

uint16_3 = np.ma.array([57, 12, 100], mask = [0, 0, 0], dtype = np.uint16)
assert_equal(uint16_3.filled(), np.array([57, 12, 100], dtype = np.uint16))

uint16_4 = np.ma.array([2, 56, 6, 1, 123, 61, 1], mask = [0, 0, 0, 1, 0, 1, 1], dtype = "uint16")
assert_equal(uint16_4.filled(), np.array([2, 56, 6, 65535, 123, 65535, 65535], dtype = np.uint16))

print("All tests passed!")
30 changes: 28 additions & 2 deletions numpy/lib/_function_base_impl.py
Expand Up @@ -3867,8 +3867,34 @@ def median(a, axis=None, out=None, overwrite_input=False, keepdims=False):
>>> assert not np.all(a==b)

"""
return _ureduce(a, func=_median, keepdims=keepdims, axis=axis, out=out,
overwrite_input=overwrite_input)

# handle edge case where a is a Python list
if isinstance(a, list):
a = np.array(a)

if a.size > 0 and len(a.shape) > 0 and all(isinstance(i, np.datetime64) for i in a) and axis is None:
a = sorted(a)
if len(a) % 2 != 0:
return a[len(a)//2]
else:
second = a[len(a)//2]
first = a[len(a)//2 - 1]
days_between = (second - first)//2
median_date = np.datetime64(first + days_between)

if out is not None:
np.append(out, [median_date])
return out

else:
return median_date
elif a.size > 0 and len(a.shape) > 0 and all(isinstance(i, np.datetime64) for i in a) and axis is not None:
if isinstance(axis, int):
dates = a[:,axis]

else:
return _ureduce(a, func=_median, keepdims=keepdims, axis=axis, out=out,
overwrite_input=overwrite_input)


def _median(a, axis=None, out=None, overwrite_input=False):
Expand Down
19 changes: 14 additions & 5 deletions numpy/ma/core.py
Expand Up @@ -167,11 +167,16 @@ class MaskError(MAError):
# b: boolean - c: complex - f: floats - i: integer - O: object - S: string
default_filler = {'b': True,
'c': 1.e20 + 0.0j,
'f': 1.e20,
'i': 999999,
"int8": 127,
"uint8": 255,
"int16": 32767,
"uint16": 65535,
"float16": 65504,
"float32": 1.e20,
"float64": 1.e20,
"i": 999999,
'O': '?',
'S': b'N/A',
'u': 999999,
'V': b'???',
'U': 'N/A'
}
Expand Down Expand Up @@ -298,7 +303,10 @@ def _scalar_fill_value(dtype):
if dtype.kind in 'Mm':
return default_filler.get(dtype.str[1:], '?')
else:
return default_filler.get(dtype.kind, '?')
if default_filler.get(str(dtype)) is not None:
return default_filler.get(str(dtype), '?')
else:
return default_filler.get(dtype.kind, '?')

dtype = _get_dtype_of(obj)
return _recursive_fill_value(dtype, _scalar_fill_value)
Expand Down Expand Up @@ -7085,7 +7093,7 @@ def power(a, b, third=None):
# Get the masks
ma = getmask(a)
mb = getmask(b)
m = mask_or(ma, mb)
m = mask_or(ma, mb, shrink=False)
# Get the rawdata
fa = getdata(a)
fb = getdata(b)
Expand All @@ -7112,6 +7120,7 @@ def power(a, b, third=None):
elif result._mask is nomask:
result._mask = invalid
result._data[invalid] = result.fill_value
# result.mask = a.mask
return result

argmin = _frommethod('argmin')
Expand Down
68 changes: 68 additions & 0 deletions numpy/ma/tests/test_core.py
Expand Up @@ -4455,6 +4455,74 @@ def test_power(self):
assert_almost_equal(x, y)
assert_almost_equal(x._data, y._data)

# Adding custom test cases for issue 25589
array1 = np.ma.array([3, 4, 5, 6], mask = [False, False, False, False])
result1_array1 = array1 ** 3.5
assert_equal(result1_array1.data, np.array([46.76537180435969, 128.0, 279.5084971874737, 529.0897844411664]))
assert_equal(len(result1_array1.mask), 4)

result2_array1 = array1 ** 0.9
assert_equal(result2_array1.data, np.array([2.6878753795222865, 3.4822022531844965,
4.256699612603923, 5.015752812467621]))
assert_equal(len(result2_array1.mask), 4)

result3_array1 = array1 ** 1.75
assert_equal(result3_array1.data, np.array([6.8385211708643325, 11.313708498984761,
16.71850762441055, 23.00195175286581]))
assert_equal(len(result3_array1.mask), 4)

array2 = np.ma.array([10, 10.7, 1.5, 19.3412], mask = [False])
result1_array2 = array2 ** 1.2391
assert_equal(result1_array2.data, np.array([17.342032668438673, 18.858599562101034,
1.6527024811874533, 39.271871725802804]))
assert_equal(len(result1_array2.mask), 4)

result2_array2 = array2 ** -0.6783
assert_equal(result2_array2.data, np.array([0.20974904879785425, 0.20034060622191774,
0.7595516280961806, 0.1340844142280547]))
assert_equal(len(result2_array2.mask), 4)

result3_array2 = array2 ** 5.1948
assert_equal(result3_array2.data, np.array([156602.9720686047, 222557.8146985335,
8.217862539806553, 4819744.856544941]))
assert_equal(len(result3_array2.mask), 4)

array3 = np.ma.array([3.4, 1.2], mask = [False, False])
result1_array3 = array3 ** 7.8

assert_equal(result1_array3.data, np.array([13980.91363494161, 4.145851281415363]))
assert_equal(len(result1_array3.mask), 2)

result2_array3 = array3 ** 1.2
assert_equal(result2_array3.data, np.array([4.342848711597634, 1.2445647472039776]))
assert_equal(len(result2_array3.mask), 2)

result3_array3 = np.ma.power(array3, 6.7)
assert_equal(result3_array3.data, np.array([3638.3857677420656, 3.3924569758842456]))
assert_equal(len(result3_array3.mask), 2)

array4 = np.ma.array([7.1205], mask = False)
result1_array4 = np.ma.power(array4, 4.57)
assert_equal(result1_array4, np.array([7869.967571527348]))
assert_equal(len(result1_array4.mask), 1)

result2_array4 = np.ma.power(array4, 0)
assert_equal(result2_array4, np.array([1]))
assert_equal(len(result2_array4.mask), 1)

array5 = np.ma.array([6.7], mask = [False])
result1_array5 = array5 ** 2.1
assert_equal(result1_array5, np.array([54.294655975192626]))
assert_equal(len(result1_array5.mask), 1)

result2_array5 = array5 ** -0.345
assert_equal(result2_array5, np.array([0.518805047913752]))
assert_equal(len(result2_array5.mask), 1)

result3_array5 = np.ma.power(array5, 1.24)
assert_equal(result3_array5, np.array([10.576275504399648]))
assert_equal(len(result3_array5.mask), 1)

def test_power_with_broadcasting(self):
# Test power w/ broadcasting
a2 = np.array([[1., 2., 3.], [4., 5., 6.]])
Expand Down
94 changes: 94 additions & 0 deletions test_cases.py
@@ -0,0 +1,94 @@
import numpy as np

# bug 25677
arr = np.ma.array([1, 2, 3], mask=[1, 0, 1], dtype=np.int8)
arr.filled()

arr = np.ma.array([1, 2, 3], mask=[1, 0, 1], dtype=np.float16)
arr.filled()

arr = np.ma.array([1.e4, 1.e4, 1.e4], mask = [1,1,1], dtype = np.float16)
arr.filled()

f16 = np.ma.array([230.12, 5601, 1027.8, 12389.4219], mask = [0, 0, 1, 0], dtype = "float16")
f16.filled()

f32 = np.ma.array([-230.12, 5601, 1027.8, 12389.4219, -9999.5321, 567.1034827], mask = [0, 0, 1, 0, 1, 1], dtype = "float32")
f32.filled()

f64 = np.ma.array([3.141592, -230.12, 5601, 1027.8, 12389.4219, -9999.5321, 567.1034827], mask = [0, 0, 1, 0, 1, 1, 1], dtype = "float64")
f64.filled()

i8 = np.ma.array([1, 2, 6, 7], mask = [1, 1, 0, 1], dtype = np.int8)
i8.filled()

ui8 = np.ma.array([1, 5], mask = [0, 1], dtype = np.uint8)
ui8.filled()

i16 = np.ma.array([5000, 120], mask = [1, 1], dtype = "int16")
i16.filled()

ui16 = np.ma.array([1, 5], mask = [0, 1], dtype = "uint16")
ui16.filled()

i32 = np.ma.array([11111, 5500, -9182], mask = [0, 1, 1], dtype = np.int32)
i32.filled()

ui32 = np.ma.array([11111, 5500, 9182, 4040], mask = [1, 0, 1, 1], dtype = np.uint32)
ui32.filled()

i64 = np.ma.array([pow(2, 34), pow(-2, 40), pow(2, 9)], mask = [0, 0, 0], dtype = np.int64)
i64.filled()

ui64 = np.ma.array([57, 120, pow(2, 34), pow(2, 40), pow(2, 41)], mask = [1, 1, 0, 0, 0], dtype = np.uint64)
ui64.filled()

i64 = np.ma.array([19675483920], mask=True, dtype = np.int64)
i64.filled()

a = np.ma.array([([0, 1],)], mask = np.ma.array([([False, False],)], dtype=[('A', '?', (2,))]), dtype=[('A', '>i2', (2,))])

ilist = [1, 2, 3, 4, 5]
flist = [1.1, 2.2, 3.3, 4.4, 5.5]
slist = [b'one', b'two', b'three', b'four', b'five']
ddtype = [('a', int), ('b', float), ('c', '|S8')]
mask = [0, 1, 0, 0, 1]
base = np.ma.array(list(zip(ilist, flist, slist)), mask=mask, dtype=ddtype)

# bug 26216
a = np.array([np.datetime64('2005-02-25'), np.datetime64(1, 'Y')])
print(np.median(a))

# bug 25589
a = np.array((1, 2, 3)) # arbitrary data
b = np.zeros(3) # all-False mask
m = np.ma.masked_array(a, b)
m * 4
m ** 2
m ** 3
m ** 1.2
m ** -0.4

array1 = np.ma.array([3, 4, 5, 6], mask = [False, False, False, False])
array1 ** 3.5
array1 ** 0.9
array1 ** 1.75

array2 = np.ma.array([10, 10.7, 1.5, 19.3412], mask = [False])
array2 ** 1.2391
array2 ** -0.6783
array2 ** 5.1948

array3 = np.ma.array([3.4, 1.2], mask = [False, False])
array3 ** 7.8
array3 ** 1.2
np.ma.power(array3, 6.7)

array4 = np.ma.array([7.1205], mask = False)
np.ma.power(array4, 4.57)
np.ma.power(array4, 0)

array5 = np.ma.array([6.7], mask = [False])
array5 ** 2.1
array5 ** -0.345
np.ma.power(array5, 1.24)