Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: make distribute_demand_from_zones_to_buses robust to zero-demand zone #605

Merged
merged 1 commit into from
Mar 16, 2022

Conversation

danielolsen
Copy link
Contributor

Pull Request doc

Purpose

If there is a zone with zero demand (i.e. all "Pd" values are zero for buses in that zone), distribute_demand_from_zones_to_buses will fail, since pandas.pivot_table will by default drop some rows/columns and we will end up with a matrix of a different size. This tweak fixes that.

What the code is doing

By passing dropna=False, we ensure that pivot_table returns a matrix of a constant size, no matter whether there are zero-demand zones or not.

Testing

Tested manually. Before:

>>> from powersimdata import Scenario
>>> from powersimdata.input.input_data import distribute_demand_from_zones_to_buses
>>> scenario = Scenario()
>>> scenario.set_grid("usa_tamu", "Texas")
>>> scenario.set_base_profile("demand", "vJan2021")
>>> demand = scenario.get_demand()
>>> grid = scenario.get_grid()
>>> grid.bus.loc[grid.bus.zone_id == 301, "Pd"] = 0
>>> distribute_demand_from_zones_to_buses(demand, grid.bus)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\DanielOlsen\repos\bes\PowerSimData\powersimdata\input\input_data.py", line 173, in distribute_demand_from_zones_to_buses
    bus_demand = zone_demand.dot(zone_bus_shares.T)
  File "C:\Python39\lib\site-packages\pandas\core\frame.py", line 1446, in dot
    raise ValueError("matrices are not aligned")
ValueError: matrices are not aligned

After:

>>> from powersimdata import Scenario
>>> from powersimdata.input.input_data import distribute_demand_from_zones_to_buses
>>> scenario = Scenario()
>>> scenario.set_grid("usa_tamu", "Texas")
>>> scenario.set_base_profile("demand", "vJan2021")
>>> demand = scenario.get_demand()
>>> grid = scenario.get_grid()
>>> grid.bus.loc[grid.bus.zone_id == 301, "Pd"] = 0
>>> distribute_demand_from_zones_to_buses(demand, grid.bus)
bus_id               3001001  3001002  3001003  ...  3008158  3008159    3008160
UTC Time                                        ...                             
2016-01-01 00:00:00      0.0      0.0      0.0  ...      0.0      0.0  38.204262
2016-01-01 01:00:00      0.0      0.0      0.0  ...      0.0      0.0  41.273131
2016-01-01 02:00:00      0.0      0.0      0.0  ...      0.0      0.0  38.596056
2016-01-01 03:00:00      0.0      0.0      0.0  ...      0.0      0.0  37.426936
2016-01-01 04:00:00      0.0      0.0      0.0  ...      0.0      0.0  37.894470
...                      ...      ...      ...  ...      ...      ...        ...
2016-12-31 19:00:00      0.0      0.0      0.0  ...      0.0      0.0  32.539467
2016-12-31 20:00:00      0.0      0.0      0.0  ...      0.0      0.0  32.040328
2016-12-31 21:00:00      0.0      0.0      0.0  ...      0.0      0.0  32.005590
2016-12-31 22:00:00      0.0      0.0      0.0  ...      0.0      0.0  31.855535
2016-12-31 23:00:00      0.0      0.0      0.0  ...      0.0      0.0  33.504717

[8784 rows x 2000 columns]

Time estimate

5 minutes.

@danielolsen danielolsen added the bug Something isn't working label Mar 16, 2022
@danielolsen danielolsen self-assigned this Mar 16, 2022
@danielolsen danielolsen merged commit b2c1dd6 into develop Mar 16, 2022
@danielolsen danielolsen deleted the daniel/distribute_demand_fix branch March 16, 2022 16:58
FabianHofmann pushed a commit to PyPSA/PowerSimData that referenced this pull request Mar 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants