Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit390d797

Browse files
authored
Merge pull request#10479 from eric-wieser/masked-array-out-fix
BUG: Calling ufuncs with a positional output argument causes the result to inherit the output's mask
2 parents9901910 +56848bb commit390d797

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

‎numpy/ma/core.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,18 +3029,20 @@ def __array_wrap__(self, obj, context=None):
30293029

30303030
ifcontextisnotNone:
30313031
result._mask=result._mask.copy()
3032-
(func,args,_)=context
3033-
m=reduce(mask_or, [getmaskarray(arg)forarginargs])
3032+
func,args,out_i=context
3033+
# args sometimes contains outputs (gh-10459), which we don't want
3034+
input_args=args[:func.nin]
3035+
m=reduce(mask_or, [getmaskarray(arg)forargininput_args])
30343036
# Get the domain mask
30353037
domain=ufunc_domain.get(func,None)
30363038
ifdomainisnotNone:
30373039
# Take the domain, and make sure it's a ndarray
3038-
iflen(args)>2:
3040+
iflen(input_args)>2:
30393041
withnp.errstate(divide='ignore',invalid='ignore'):
3040-
d=filled(reduce(domain,args),True)
3042+
d=filled(reduce(domain,input_args),True)
30413043
else:
30423044
withnp.errstate(divide='ignore',invalid='ignore'):
3043-
d=filled(domain(*args),True)
3045+
d=filled(domain(*input_args),True)
30443046

30453047
ifd.any():
30463048
# Fill the result where the domain is wrong

‎numpy/ma/tests/test_core.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5057,6 +5057,31 @@ def test_ufunc_with_output():
50575057
assert_(yisx)
50585058

50595059

5060+
deftest_ufunc_with_out_varied():
5061+
""" Test that masked arrays are immune to gh-10459 """
5062+
# the mask of the output should not affect the result, however it is passed
5063+
a=array([1,2,3],mask=[1,0,0])
5064+
b=array([10,20,30],mask=[1,0,0])
5065+
out=array([0,0,0],mask=[0,0,1])
5066+
expected=array([11,22,33],mask=[1,0,0])
5067+
5068+
out_pos=out.copy()
5069+
res_pos=np.add(a,b,out_pos)
5070+
5071+
out_kw=out.copy()
5072+
res_kw=np.add(a,b,out=out_kw)
5073+
5074+
out_tup=out.copy()
5075+
res_tup=np.add(a,b,out=(out_tup,))
5076+
5077+
assert_equal(res_kw.mask,expected.mask)
5078+
assert_equal(res_kw.data,expected.data)
5079+
assert_equal(res_tup.mask,expected.mask)
5080+
assert_equal(res_tup.data,expected.data)
5081+
assert_equal(res_pos.mask,expected.mask)
5082+
assert_equal(res_pos.data,expected.data)
5083+
5084+
50605085
deftest_astype():
50615086
descr= [('v',int,3), ('x', [('y',float)])]
50625087
x=array([

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp