Skip to content
Snippets Groups Projects

Several fixes

Closed David Schäfer requested to merge multifix into develop
3 files
+ 37
19
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 30
16
@@ -78,7 +78,7 @@ class History:
hist = hist.copy()
mask = mask.copy()
self.hist = hist
self.hist = hist.astype("category", copy=copy)
self.mask = mask
@property
@@ -166,7 +166,7 @@ class History:
touched = s.notna()
self.mask.iloc[touched, :pos] = False
self.hist[pos] = s
self.hist[pos] = s.astype("category")
return self
@@ -263,7 +263,7 @@ class History:
# clear the current mask
self.mask.loc[(~value_mask & value_hist.notna()).any(axis="columns")] = False
self.hist.loc[:, columns] = value_hist.copy()
self.hist.loc[:, columns] = value_hist.astype("category")
self.mask.loc[:, columns] = value_mask.copy()
return self
@@ -309,7 +309,7 @@ class History:
# this may leave us in an unstable state, because
# the last column maybe is not entirely True, but
# the following append, will fix this
self.hist = self.hist.iloc[:, :-n]
self.hist = self.hist.iloc[:, :-n].astype("category")
self.mask = self.mask.iloc[:, :-n]
self.append(s)
@@ -323,7 +323,7 @@ class History:
-------
pd.Series: maximum values
"""
return self.hist[self.mask].idxmax(axis=1)
return self.hist[self.mask].astype(float).idxmax(axis=1)
def max(self) -> pd.Series:
"""
@@ -371,11 +371,18 @@ class History:
-------
History
"""
self.hist = self.hist.reindex(index=index, copy=False, fill_value=np.nan)
self.mask = self.mask.reindex(index=index, copy=False, fill_value=False)
hist = self.hist.astype(float).reindex(
index=index, copy=False, fill_value=np.nan
)
mask = self.mask.astype(bool).reindex(index=index, copy=False, fill_value=False)
# Note: all following code must handle empty frames
self.hist.iloc[:, -1:] = self.hist.iloc[:, -1:].fillna(fill_value_last)
self.mask.iloc[:, -1:] = True
hist.iloc[:, -1:] = hist.iloc[:, -1:].fillna(fill_value_last)
mask.iloc[:, -1:] = True
self.mask = mask.astype(bool)
self.hist.hist = hist.astype("category")
return self
def __copy__(self, deep: bool = True):
@@ -455,8 +462,10 @@ class History:
f"'hist' must be of type pd.DataFrame, but {type(obj).__name__} was given"
)
if (obj.dtypes != float).any():
raise ValueError("dtype of all columns in hist must be float")
if obj.dtypes.isin([float, pd.Categorical]).any() is False:
raise ValueError(
"dtype of all columns in hist must be float or categorical"
)
if not obj.empty and (
not obj.columns.equals(pd.Index(range(len(obj.columns))))
@@ -478,8 +487,8 @@ class History:
f"value must be of type pd.Series, but {type(obj).__name__} was given"
)
if not obj.dtype == float:
raise ValueError("dtype must be float")
if not ((obj.dtype == float) or isinstance(obj.dtype, pd.CategoricalDtype)):
raise ValueError("dtype must be float or categorical")
return obj
@@ -533,12 +542,17 @@ def applyFunctionOnHistory(
new_history = History()
if func_handle_df:
history.hist = hist_func(history.hist, **hist_kws)
# we need to pass the data as floats as functions may fail with Categorical
history.hist = hist_func(history.hist.astype(float), **hist_kws).astype(
"category"
)
history.mask = hist_func(history.mask, **mask_kws)
else:
for pos in history.columns:
new_history.hist[pos] = hist_func(history.hist[pos], **hist_kws)
new_history.hist[pos] = hist_func(
history.hist[pos].astype(float), **hist_kws
).astype("category")
new_history.mask[pos] = mask_func(history.mask[pos], **mask_kws)
# handle unstable state
@@ -548,7 +562,7 @@ def applyFunctionOnHistory(
if isinstance(last_column, str) and last_column == "dummy":
last_column = pd.Series(UNTOUCHED, index=new_history.index, dtype=float)
new_history.append(last_column, force=True)
new_history.append(last_column.astype("category"), force=True)
# assure a boolean mask and UNFLAGGED column
new_history.mask = new_history.mask.fillna(True).astype(bool)
Loading