From e193897ca29aca21790b316f92f95985767ec297 Mon Sep 17 00:00:00 2001 From: Andrecho <37243793+andrewendlinger@users.noreply.github.com> Date: Fri, 20 Feb 2026 19:39:10 +0000 Subject: [PATCH 1/2] fix: handle NaN expressions in PriorKnowledge to prevent TypeError --- pyAMARES/kernel/PriorKnowledge.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pyAMARES/kernel/PriorKnowledge.py b/pyAMARES/kernel/PriorKnowledge.py index dc4a4ba..1a0e99a 100644 --- a/pyAMARES/kernel/PriorKnowledge.py +++ b/pyAMARES/kernel/PriorKnowledge.py @@ -369,6 +369,11 @@ def generateparameter( lval = df_lb2[peak].iloc[i] uval = df_ub2[peak].iloc[i] expr = df_expr[peak].iloc[i] + + # --- FIX: Ensure expr is None if it evaluates to NaN --- + if pd.isna(expr): + expr = None + # Handle NaN values for bounds if np.isnan(lval): lval = -np.inf From 816ae0cc245bb987d83b7e429c98cffc2e7d23ae Mon Sep 17 00:00:00 2001 From: Andrecho <37243793+andrewendlinger@users.noreply.github.com> Date: Fri, 20 Feb 2026 19:52:44 +0000 Subject: [PATCH 2/2] fix: prevent pandas LossySetitemError during unit conversion Removes float32 downcasting in `safe_convert_to_numeric` and ensures numeric columns are cast to float64 in `unitconverter`. This resolves a `LossySetitemError` in pandas 2.2+ caused by attempting to insert float64 constants (e.g., `np.pi`) into float32 columns. --- pyAMARES/kernel/PriorKnowledge.py | 35 ++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/pyAMARES/kernel/PriorKnowledge.py b/pyAMARES/kernel/PriorKnowledge.py index 1a0e99a..a182aae 100644 --- a/pyAMARES/kernel/PriorKnowledge.py +++ b/pyAMARES/kernel/PriorKnowledge.py @@ -15,9 +15,14 @@ def safe_convert_to_numeric(x): try: - return pd.to_numeric( - x, downcast="float", errors="raise" - ) # Use float as default output type + res = pd.to_numeric(x, errors="raise") + # Ensure float64 to prevent LossySetitemError in pandas 2.2+ + if isinstance(res, pd.Series): + if np.issubdtype(res.dtype, np.number): + return res.astype(float) + elif isinstance(res, (int, float, np.number)) and not isinstance(res, bool): + return float(res) + return res except (ValueError, TypeError): return x @@ -181,16 +186,30 @@ def unitconverter(df_ini, MHz=120.0): pandas.DataFrame: A DataFrame with converted unit values in specified rows. """ df = deepcopy(df_ini) + + # Pre-cast numeric columns to float64 to prevent pandas 2.2+ LossySetitemError + for col in df.columns: + if np.issubdtype(df[col].dtype, np.number): + df[col] = df[col].astype(float) + if "chemicalshift" in df.index: - df.loc["chemicalshift", df.notna().loc["chemicalshift"]] *= MHz + mask = df.notna().loc["chemicalshift"] + if mask.any(): + df.loc["chemicalshift", mask] = df.loc["chemicalshift", mask].astype( + float + ) * float(MHz) if "linewidth" in df.index: - df.loc["linewidth", df.notna().loc["linewidth"]] *= np.pi + mask = df.notna().loc["linewidth"] + if mask.any(): + df.loc["linewidth", mask] = df.loc["linewidth", mask].astype(float) * float( + np.pi + ) if "phase" in df.index: - df.loc["phase", df.notna().loc["phase"]] = np.deg2rad( - df.loc["phase"][df.notna().loc["phase"]].astype(float) - ) + mask = df.notna().loc["phase"] + if mask.any(): + df.loc["phase", mask] = np.deg2rad(df.loc["phase"][mask].astype(float)) return df