Skip to content

Commit 7e4354d

Browse files
committed
Close #229
Problem was created from creating np.nan values in all columns and then converting with pandas.
1 parent 35f1908 commit 7e4354d

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/petab_gui/commands.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,38 @@ def redo(self):
155155
if np.any(dtypes != df.dtypes):
156156
for col, dtype in dtypes.items():
157157
if dtype != df.dtypes[col]:
158-
df[col] = df[col].astype(dtype)
158+
is_pandas_nullable_int = isinstance(
159+
dtype,
160+
(
161+
pd.Int64Dtype,
162+
pd.Int32Dtype,
163+
pd.Int16Dtype,
164+
pd.Int8Dtype,
165+
),
166+
)
167+
168+
if is_pandas_nullable_int:
169+
# Keep pandas nullable integer types as is
170+
df[col] = df[col].astype(dtype)
171+
# If column has NaN and dtype is integer, use nullable Int type
172+
elif (
173+
np.issubdtype(dtype, np.integer)
174+
and df[col].isna().any()
175+
):
176+
# Convert numpy int types to pandas nullable Int types
177+
if dtype == np.int64:
178+
df[col] = df[col].astype("Int64")
179+
elif dtype == np.int32:
180+
df[col] = df[col].astype("Int32")
181+
elif dtype == np.int16:
182+
df[col] = df[col].astype("Int16")
183+
elif dtype == np.int8:
184+
df[col] = df[col].astype("Int8")
185+
else:
186+
# Fallback for other integer types
187+
df[col] = df[col].astype("Int64")
188+
else:
189+
df[col] = df[col].astype(dtype)
159190
self.model.endInsertRows()
160191
else:
161192
self.model.beginRemoveRows(
@@ -261,7 +292,17 @@ def _apply_changes(self, use_new: bool):
261292
for col, dtype in original_dtypes.items():
262293
if col not in update_df.columns:
263294
continue
264-
if np.issubdtype(dtype, np.number):
295+
# Check if it's a pandas extension dtype (like Int64)
296+
is_pandas_nullable_int = isinstance(
297+
dtype,
298+
(pd.Int64Dtype, pd.Int32Dtype, pd.Int16Dtype, pd.Int8Dtype),
299+
)
300+
301+
if is_pandas_nullable_int:
302+
# Keep pandas nullable integer types as is
303+
df[col] = pd.to_numeric(df[col], errors="coerce")
304+
df[col] = df[col].astype(dtype)
305+
elif np.issubdtype(dtype, np.number):
265306
df[col] = pd.to_numeric(df[col], errors="coerce")
266307
else:
267308
df[col] = df[col].astype(dtype)

0 commit comments

Comments
 (0)