'How to take a median of a MaskedColumn in Astropy

I am struggling to get a median of a MaskedColumn object in astropy.

Basically, the way my code is set up is that I have a dictionary full of astropy data tables with measurements of different stellar objects.

I'm going into one of these tables and taking out one column. I'm specifically taking data points I want and rejecting (masking out) other data points.

If I print the column data, I get this:

print(dictionary[table_name][column_name])
column_name
[units] 
------
    --
-1.474
    --
-1.498
-1.359
    --
    --
-1.424
 9.990
-1.288
-1.438
-1.540
-1.542
    --
    --
...

etc, where -- is what was put in place of a masked datum, which seems to be standard.

But then when I do np.median OR np.nanmedian of that exact column, I only get nans.

Is there something different I need to be doing with the MaskedColumn classes to take a median?

Here's what I'm trying to print, with names changed for clarity:

print(np.median(dictionary[table_name][column_name]))
nan

print(np.nanmedian(dictionary[table_name][column_name]))
nan

I don't think the way I generate these tables would affect my inability to take the median, but just in case they do, I'll add those below:

When I'm loading them into the dictionary, I do it using:

for j, table_name in enumerate(table_names_list):

    indices_for_object_measurements = (np.where("Specific Object Name" == table["All_Object Names"])[0])

    dictionary["Object Name"] = table[indices_for_object_measurements] 

I also reject some of the unwanted data points in one of these objects using:

reject_column_labels = [(x,i) for i, x in enumerate(table.colnames) if "column to reject" in x]

    for reject_column_label in reject_columns_labels:
        data_column = #(string editing, name of column related to the rejection column)
        
        reject_these_points = np.where("<" == table[reject_column[0]])
        
        table[data_column][reject_these_points] = np.nan


Solution 1:[1]

You should use np.ma.median which handles masked data properly. The astropy MaskedColumn class is a subclass of np.ma.MaskedArray so all the methods in np.ma (https://numpy.org/doc/stable/reference/maskedarray.html) should be applicable.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Tom Aldcroft