Skip to content

Commit

Permalink
Put in fill_value in pivot_table
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Sep 24, 2024
1 parent d49d634 commit 990f528
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions python/ribasim/ribasim/delwaq/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def _make_boundary(data, boundary_type):
"""
bid = _boundary_name(data.node_id.iloc[0], boundary_type)
piv = (
data.pivot_table(index="time", columns="substance", values="concentration")
data.pivot_table(
index="time", columns="substance", values="concentration", fill_value=-999
)
.reset_index()
.reset_index(drop=True)
)
Expand All @@ -79,9 +81,7 @@ def _make_boundary(data, boundary_type):
boundary = {
"name": bid,
"substances": list(map(_quote, piv.columns[1:])),
"df": piv.to_string(
formatters={"time": _quote}, header=False, index=False, na_rep=-999
),
"df": piv.to_string(formatters={"time": _quote}, header=False, index=False),
}
substances = data.substance.unique()
return boundary, substances
Expand Down Expand Up @@ -182,7 +182,7 @@ def _setup_graph(nodes, edge, use_evaporation=True):
boundary_id -= 1
node_mapping[node_id] = boundary_id
else:
raise Exception("Found unexpected node $node_id in delwaq graph.")
raise Exception(f"Found unexpected node {node_id} in delwaq graph.")

Check warning on line 185 in python/ribasim/ribasim/delwaq/generate.py

View check run for this annotation

Codecov / codecov/patch

python/ribasim/ribasim/delwaq/generate.py#L185

Added line #L185 was not covered by tests

nx.relabel_nodes(G, node_mapping, copy=False)

Expand Down Expand Up @@ -251,25 +251,18 @@ def _setup_boundaries(model):
substances = set()

if model.level_boundary.concentration.df is not None:
model.level_boundary.concentration.df["concentration"].fillna(
np.nan, inplace=True
)
for _, rows in model.level_boundary.concentration.df.groupby(["node_id"]):
boundary, substance = _make_boundary(rows, "LevelBoundary")
boundaries.append(boundary)
substances.update(substance)

if model.flow_boundary.concentration.df is not None:
model.flow_boundary.concentration.df["concentration"].fillna(
np.nan, inplace=True
)
for _, rows in model.flow_boundary.concentration.df.groupby("node_id"):
boundary, substance = _make_boundary(rows, "FlowBoundary")
boundaries.append(boundary)
substances.update(substance)

if model.basin.concentration.df is not None:
model.basin.concentration.df["concentration"].fillna(np.nan, inplace=True)
for _, rows in model.basin.concentration.df.groupby(["node_id"]):
for boundary_type in ("Drainage", "Precipitation"):
nrows = rows.rename(columns={boundary_type.lower(): "concentration"})
Expand Down

0 comments on commit 990f528

Please sign in to comment.