Skip to content

Commit

Permalink
refactor: avoid repeated index lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
danielolsen committed Oct 8, 2020
1 parent ad8bd84 commit 2c07448
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions powersimdata/scenario/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ def calculate_bus_demand(bus, demand):
"""
zone_demand = bus.groupby("zone_id").sum().Pd
bus_zone_share = bus.apply(lambda x: x.Pd / zone_demand.loc[x.zone_id], axis=1)
bus_to_zone = pd.DataFrame(index=zone_demand.index, columns=bus.index)
bus_to_zone = np.zeros((len(zone_demand), len(bus)))
bus_idx_lookup = {b: i for i, b in enumerate(bus.index)}
zone_idx_lookup = {z: i for i, z in enumerate(zone_demand.index)}
for b in bus.index:
bus_to_zone.loc[bus.loc[b, "zone_id"], b] = bus_zone_share.loc[b]
bus_idx = bus_idx_lookup[b]
zone_idx = zone_idx_lookup[bus.loc[b, "zone_id"]]
bus_to_zone[zone_idx, bus_idx] = bus_zone_share.loc[b]
bus_demand = pd.DataFrame(
np.dot(demand.to_numpy(), bus_to_zone), index=demand.index, columns=bus.index
)
Expand Down

0 comments on commit 2c07448

Please sign in to comment.