Reputation: 4936
I have two MaskedArray objects, with the same length:
>> grades_passed
[90 -- 88 62 -- 100]
>> grades_not_passed
[-- 54 -- -- 34 --]
And I'm trying to combine them to get:
[90 54 88 62 34 100]
I tried some manipulations like:
total_final = grades_passed + grades_not_passed
or
total_final = ma.mask_or(grades_passed, grades_not_passed)
total_final = ma.concatenate(grades_passed, grades_not_passed)
But none of them do the trick
Upvotes: 1
Views: 123
Reputation: 1905
The following works, as long as there is no overlap of the two masks:
grades = grades_passed.filled(1) * grades_not_passed.filled(1)
The parameter 1
temporarily sets the fill_value
of the two variables, so masked values take on 1
as value to be filled.
Upvotes: 2
Reputation: 2348
Convert the arrays to lists and create dictionaries with index values (this assumes that the two arrays won't have a value in the same index position). Then you can combine those and output the result as a masked array or list:
def combine_grades(passed, failed):
# Put index:value for each list into a dictionary
pass_dict = {list(passed).index(i):i for i in list(passed) if i != '--'}
fail_dict = {list(failed).index(i):i for i in list(failed) if i != '--'}
# Combine those dictionaries
full_dict = {**pass_dict, **fail_dict}
# Return a masked array (if that's what you want. Otherwise, just return the list)
return ma.array([full_dict[i] for i in sorted(full_dict)])
Then call:
combine_grades(grades_passed, grades_not_passed)
Output:
masked_array(data=[ 90, 54, 88, 62, 34, 100],
mask=False,
fill_value=999999)
Upvotes: 0