diff --git a/include/evalhyd/detail/masks.hpp b/include/evalhyd/detail/masks.hpp
index a0706c557561c377a7c80aa0e6fc4feda1a72ca9..d1d503e257a826f6448f199c42265daa874fcd65 100644
--- a/include/evalhyd/detail/masks.hpp
+++ b/include/evalhyd/detail/masks.hpp
@@ -232,17 +232,29 @@ namespace evalhyd
                         {
                             return std::stod(num);
                         }
-                        else if (str == "median")
-                        {
-                            return xt::median(q);
-                        }
-                        else if (str == "mean")
-                        {
-                            return xt::mean(q)();
-                        }
-                        else  // (str == "qtl")
+                        else
                         {
-                            return xt::quantile(q, {std::stod(num)})();
+                            auto q_filtered = xt::filter(q, !xt::isnan(q));
+
+                            if (q_filtered.size() > 0)
+                            {
+                                if (str == "median")
+                                {
+                                    return xt::median(q_filtered);
+                                }
+                                else if (str == "mean")
+                                {
+                                    return xt::mean(q_filtered)();
+                                }
+                                else  // (str == "qtl")
+                                {
+                                    return xt::quantile(q_filtered, {std::stod(num)})();
+                                }
+                            }
+                            else
+                            {
+                                return double(NAN);
+                            }
                         }
                     };