Skip to content

How to check if an array is masked? #1077

Answered by agoose77
jrueb asked this question in Q&A
Discussion options

You must be logged in to vote

We don't as-yet have a routine to do this. A "hacky" way is to check the type string, but if you want to specify an axis, we should write a proper visitor to do this. Your proposed function might look something like this:

def is_masked(array, axis=1):
    # To support Py 2, we can't use nonlocal
    state = {
        "posaxis": axis, 
        "is_mask": False
    }

    def apply(layout, depth):
        posaxis = state['posaxis'] = layout.axis_wrap_if_negative(state['posaxis'])
        if (depth == posaxis + 1) and isinstance(layout, ak._util.optiontypes):
            state['is_mask'] = True

    layout = ak.to_layout(array)
    ak._util.recursive_walk(layout, apply)

    return state['is…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jpivarski
Comment options

Answer selected by jrueb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants