Skip to content

Commit

Permalink
Merge pull request #916 from koen-vg/pandas-dep-fixes
Browse files Browse the repository at this point in the history
Pandas deprecation fixes
  • Loading branch information
fneum authored Feb 7, 2024
2 parents bf07300 + c3d3b5f commit 0ded96c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
36 changes: 15 additions & 21 deletions scripts/make_summary_perfect.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ def calculate_energy(n, label, energy):
.groupby(level=0)
.sum()
.multiply(c.df.sign)
.groupby(c.df.carrier, axis=1)
.T.groupby(c.df.carrier)
.sum()
.T
)
else:
c_energies = pd.DataFrame(
Expand Down Expand Up @@ -394,16 +395,9 @@ def calculate_supply_energy(n, label, supply_energy):
if len(items) == 0:
continue

s = (
(-1)
* c.pnl["p" + end]
.reindex(items, axis=1)
.multiply(n.snapshot_weightings.objective, axis=0)
.groupby(level=0)
.sum()
.groupby(c.df.loc[items, "carrier"], axis=1)
.sum()
).T
s = (-1) * c.pnl["p" + end].reindex(items, axis=1).multiply(
n.snapshot_weightings.objective, axis=0
).groupby(level=0).sum().T.groupby(c.df.loc[items, "carrier"]).sum()
s.index = s.index + end
s = pd.concat([s], keys=[c.list_name])
s = pd.concat([s], keys=[i])
Expand Down Expand Up @@ -498,7 +492,7 @@ def calculate_weighted_prices(n, label, weighted_prices):
else:
suffix = " " + carrier

buses = n.buses.index[n.buses.index.str[2:] == suffix]
buses = n.buses.index[n.buses.index.str[5:] == suffix]

if buses.empty:
continue
Expand All @@ -509,14 +503,14 @@ def calculate_weighted_prices(n, label, weighted_prices):
else n.loads_t.p_set.reindex(buses, axis=1)
)
for tech in value:
names = n.links.index[n.links.index.to_series().str[-len(tech) :] == tech]
names = n.links.index[
n.links.index.to_series().str[-len(tech) - 5 : -5] == tech
]

if names.empty:
continue

load += (
n.links_t.p0[names].groupby(n.links.loc[names, "bus0"], axis=1).sum()
)
load += n.links_t.p0[names].T.groupby(n.links.loc[names, "bus0"]).sum().T

# Add H2 Store when charging
# if carrier == "H2":
Expand Down Expand Up @@ -557,9 +551,9 @@ def calculate_market_values(n, label, market_values):

dispatch = (
n.generators_t.p[gens]
.groupby(n.generators.loc[gens, "bus"], axis=1)
.T.groupby(n.generators.loc[gens, "bus"])
.sum()
.reindex(columns=buses, fill_value=0.0)
.T.reindex(columns=buses, fill_value=0.0)
)

revenue = dispatch * n.buses_t.marginal_price[buses]
Expand All @@ -583,9 +577,9 @@ def calculate_market_values(n, label, market_values):

dispatch = (
n.links_t["p" + i][links]
.groupby(n.links.loc[links, "bus" + i], axis=1)
.T.groupby(n.links.loc[links, "bus" + i])
.sum()
.reindex(columns=buses, fill_value=0.0)
.T.reindex(columns=buses, fill_value=0.0)
)

revenue = dispatch * n.buses_t.marginal_price[buses]
Expand Down Expand Up @@ -652,7 +646,7 @@ def calculate_co2_emissions(n, label, df):
emitted = n.generators_t.p[gens.index].mul(em_pu)

emitted_grouped = (
emitted.groupby(level=0).sum().groupby(n.generators.carrier, axis=1).sum().T
emitted.groupby(level=0).sum().T.groupby(n.generators.carrier).sum()
)

df = df.reindex(emitted_grouped.index.union(df.index))
Expand Down
6 changes: 4 additions & 2 deletions scripts/prepare_perfect_foresight.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ def get_investment_weighting(time_weighting, r=0.01):
end = time_weighting.cumsum()
start = time_weighting.cumsum().shift().fillna(0)
return pd.concat([start, end], axis=1).apply(
lambda x: sum(get_social_discount(t, r) for t in range(int(x[0]), int(x[1]))),
lambda x: sum(
get_social_discount(t, r) for t in range(int(x.iloc[0]), int(x.iloc[1]))
),
axis=1,
)

Expand Down Expand Up @@ -188,7 +190,7 @@ def concat_networks(years):
pnl = getattr(n, component.list_name + "_t")
for k in iterkeys(component.pnl):
pnl_year = component.pnl[k].copy().reindex(snapshots, level=1)
if pnl_year.empty and ~(component.name == "Load" and k == "p_set"):
if pnl_year.empty and (not (component.name == "Load" and k == "p_set")):
continue
if component.name == "Load":
static_load = network.loads.loc[network.loads.p_set != 0]
Expand Down

0 comments on commit 0ded96c

Please sign in to comment.