def ensure_bus_index_columns_as_int(net): """ Ensures that all columns with bus indices, e.g. net.line.from_bus, have int as dtype. """ ebts = pp.element_bus_tuples(bus_elements=True, branch_elements=True, res_elements=False) ebts |= {("switch", "element"), ("measurement", "element")} for elm, bus in ebts: net[elm][bus] = net[elm][bus].astype(int)
def _voltlvl_idx(net, element, voltage_level, branch_bus=None, vn_kv_limits=[145, 60, 1]): """ similar to voltlvl_idx, but for only one voltage_level """ vn_kv_limits = [np.inf] + vn_kv_limits + [-np.inf] voltage_level = convert_voltlvl_names(voltage_level, int) lim_max = [0, 0, 1, 1, 2, 2, 3][voltage_level - 1] lim_min = [1, 2, 2, 3, 3, 4, 4][voltage_level - 1] Idx_bus = net.bus.index[(net.bus.vn_kv <= vn_kv_limits[lim_max]) & (net.bus.vn_kv > vn_kv_limits[lim_min])] if element == "bus": return list(Idx_bus) if branch_bus is None and element not in ["trafo", "trafo3w"]: # for all other elements than trafos, take the first possibility for elm, bus_name in element_bus_tuples(): if elm == element: branch_bus = bus_name break if element == "measurement": measurement_buses = Series(index=net.measurement.index) # bus bool_ = net.measurement.element_type == "bus" measurement_buses.loc[bool_] = net.measurement.element.loc[bool_] # line and trafo for branch, side in zip(["line", "line", "trafo", "trafo"], ["from", "to", "hv", "lv"]): bus = side + "_bus" bool1 = net.measurement.element_type == branch bool2 = net.measurement.side == side measurement_buses.loc[bool1 & bool2] = net[branch][bus].loc[ net.measurement.element.loc[bool1 & bool2]].values measurement_buses = measurement_buses.astype(int) isin_Idx_bus = measurement_buses.isin(Idx_bus) elif branch_bus in net[ element].columns: # all other elements than measurement and bus isin_Idx_bus = net[element][branch_bus].isin(Idx_bus) else: raise KeyError( "For net[%s] there is no column '%s'. Please" % (element, str(branch_bus)) + " give 'branch_bus' an valid bus column name, e.g. 'hv_bus' or 'lv_bus'." ) return list(net[element].index[isin_Idx_bus])
def _voltlvl_idx(net, element, voltage_level, branch_bus=None, vn_kv_limits=[145, 60, 1]): """ similar to voltlvl_idx, but for only one voltage_level """ vn_kv_limits = [np.inf] + vn_kv_limits + [-np.inf] voltage_level = convert_voltlvl_names(voltage_level, int) lim_max = [0, 0, 1, 1, 2, 2, 3][voltage_level - 1] lim_min = [1, 2, 2, 3, 3, 4, 4][voltage_level - 1] Idx_bus = net.bus.index[(net.bus.vn_kv <= vn_kv_limits[lim_max]) & (net.bus.vn_kv > vn_kv_limits[lim_min])] if element == "bus": return list(Idx_bus) if branch_bus is None and element not in ["trafo", "trafo3w"]: # for all other elements than trafos, take the first possibility for elm, bus_name in element_bus_tuples(): if elm == element: branch_bus = bus_name break if element == "measurement": if hasattr(branch_bus, "__iter__") and len(branch_bus): branch_bus_Series = Series(branch_bus) trafo_branch_bus = branch_bus_Series[branch_bus_Series.isin( net["trafo"].columns)].iloc[0] line_branch_bus = branch_bus_Series[branch_bus_Series.isin( net["line"].columns)].iloc[0] bus_isin_Idx_bus = net[element]["element"].isin(Idx_bus) trafo_isin_Idx_bus = net["trafo"][trafo_branch_bus].isin(Idx_bus) line_isin_Idx_bus = net["line"][line_branch_bus].isin(Idx_bus) is_bus_type = net[element]["element_type"] == "bus" is_trafo_type = net[element]["element_type"] == "trafo" is_line_type = net[element]["element_type"] == "line" trafos = net[element]["element"][is_trafo_type] trafo_isin_Idx_bus = Series( trafo_isin_Idx_bus.loc[trafos.values].values, index=trafos.index) lines = net[element]["element"][is_line_type] line_isin_Idx_bus = Series( line_isin_Idx_bus.loc[lines.values].values, index=lines.index) isin_Idx_bus = (bus_isin_Idx_bus & is_bus_type ) | trafo_isin_Idx_bus | line_isin_Idx_bus else: raise KeyError( "For element=='measurement', branch_bus must contain the branch_bus " + "for trafo and line, e.g. branch_bus=['hv_bus', 'from_bus'].") elif branch_bus in net[ element].columns: # all other elements than measurement isin_Idx_bus = net[element][branch_bus].isin(Idx_bus) else: raise KeyError( "For net[%s] there is no column '%s'. Please" % (element, str(branch_bus)) + " give 'branch_bus' an valid bus column name, e.g. 'hv_bus' or 'lv_bus'." ) return list(net[element].index[isin_Idx_bus])