From 7aa0fdf867c452bee445151a35f6dc57d06208d4 Mon Sep 17 00:00:00 2001 From: Swargambharath987 Date: Sat, 28 Feb 2026 23:50:53 -0500 Subject: [PATCH] Fix NA groups being omitted from plotly plots --- www/api/resources/plotly_data.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/www/api/resources/plotly_data.py b/www/api/resources/plotly_data.py index c8fe3af2..fc95786a 100644 --- a/www/api/resources/plotly_data.py +++ b/www/api/resources/plotly_data.py @@ -302,8 +302,15 @@ def post(self, dataset_id): dataframe = pd.concat([dataframe,selected.obs], axis=1) # fill any missing adata.obs values with "NA" - # The below line gives the error - TypeError: Cannot setitem on a Categorical with a new category (NA), set the categories first - #df = df.fillna("NA") + # Categorical columns must have "NA" added as a category before filling + for col in dataframe.select_dtypes(include="category").columns: + if dataframe[col].isna().any(): + if "NA" not in dataframe[col].cat.categories: + dataframe[col] = dataframe[col].cat.add_categories("NA") + dataframe[col] = dataframe[col].fillna("NA") + for col in dataframe.select_dtypes(include="object").columns: + if dataframe[col].isna().any(): + dataframe[col] = dataframe[col].fillna("NA") # Valid analysis column names from api/resources/h5ad.py analysis_tsne_columns = ['X_tsne_1', 'X_tsne_2']