'numpy filter 2D array with 2D pandas mask

Given a 2D numpy array I would like to filter values based on a condition.

data = pd.DataFrame([['A', 'B'], ['C']])
mask = ~pd.isna(data)
filtered_data = data.values[mask.values]
> ['A', 'B', 'C']   // expcted: [['A', 'B'], ['C']

I already explored any solutions on SO where you filter using np.isnan but that doesn't work when all your data types are not numbers. I have a mix of string and NaN.

How can I get a 2D array where all the NaN values are stripped? I would prefer a vectorized a solution rather than looping over each dimension in numpy.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source