Toolbox

The pandapower toolbox is a collection of helper functions that are implemented for the pandapower framework. It is designed for functions of common application that fit nowhere else. Have a look at the available functions to save yourself the effort of maybe implementing something twice. If you develop some functionality which could be interesting to other users as well and do not fit into one of the specialized packages, feel welcome to add your contribution. To improve overview functions are loosely grouped by functionality, please adhere to this notion when adding your own functions and feel free to open new groups as needed.

Note

If you implement a function that might be useful for others, it is mandatory to add a short docstring to make browsing the toolbox practical. Ideally further comments if appropriate and a reference of authorship should be added as well.

General Issues

pandapower.element_bus_tuples(bus_elements=True, branch_elements=True, res_elements=False)

Utility function Provides the tuples of elements and corresponding columns for buses they are connected to :param bus_elements: whether tuples for bus elements e.g. load, sgen, … are included :param branch_elements: whether branch elements e.g. line, trafo, … are included :return: set of tuples with element names and column names

pandapower.pp_elements(bus=True, bus_elements=True, branch_elements=True, other_elements=True, res_elements=False)

Returns a set of pandapower elements.

pandapower.pq_from_cosphi(s, cosphi, qmode, pmode)

Calculates P/Q values from rated apparent power and cosine(phi) values.

  • s: rated apparent power

  • cosphi: cosine phi of the

  • qmode: “ind” for inductive or “cap” for capacitive behaviour

  • pmode: “load” for load or “gen” for generation

As all other pandapower functions this function is based on the consumer viewpoint. For active power, that means that loads are positive and generation is negative. For reactive power, inductive behaviour is modeled with positive values, capacitive behaviour with negative values.

pandapower.cosphi_from_pq(p, q)
pandapower.dataframes_equal(x_df, y_df, tol=1e-14, ignore_index_order=True)

Returns a boolean whether the nets are equal or not.

pandapower.compare_arrays(x, y)

Returns an array of bools whether array x is equal to array y. Strings are allowed in x or y. NaN values are assumed as equal.

pandapower.ensure_iterability(var, len_=None)

Ensures iterability of a variable (and optional length).

Result Information

pandapower.lf_info(net, numv=1, numi=2)

Prints some basic information of the results in a net (max/min voltage, max trafo load, max line load).

OPTIONAL:

numv (integer, 1) - maximal number of printed maximal respectively minimal voltages

numi (integer, 2) - maximal number of printed maximal loading at trafos or lines

pandapower.opf_task(net, delta_pq=0.001, keep=False, log=True)

Collects some basic inforamtion of the optimal powerflow task und prints them.

pandapower.switch_info(net, sidx)

Prints what buses and elements are connected by a certain switch.

pandapower.overloaded_lines(net, max_load=100)

Returns the results for all lines with loading_percent > max_load or None, if there are none.

pandapower.violated_buses(net, min_vm_pu, max_vm_pu)

Returns all bus indices where vm_pu is not within min_vm_pu and max_vm_pu or returns None, if there are none of those buses.

pandapower.nets_equal(net1, net2, check_only_results=False, exclude_elms=None, **kwargs)

Compares the DataFrames of two networks. The networks are considered equal if they share the same keys and values, except of the ‘et’ (elapsed time) entry which differs depending on runtime conditions and entries stating with ‘_’.

Simulation Setup and Preparation

pandapower.add_column_from_node_to_elements(net, column, replace, elements=None, branch_bus=None, verbose=True)

Adds column data to elements, inferring them from the column data of buses they are connected to.

INPUT:

net (pandapowerNet) - the pandapower net that will be changed

column (string) - name of column that should be copied from the bus table to the element table

replace (boolean) - if True, an existing column in the element table will be overwritten

elements (list) - list of elements that should get the column values from the bus table

branch_bus (list) - defines which bus should be considered for branch elements. ‘branch_bus’ must have the length of 2. One entry must be ‘from_bus’ or ‘to_bus’, the other ‘hv_bus’ or ‘lv_bus’

EXAMPLE:

compare to add_zones_to_elements()

pandapower.add_column_from_element_to_elements(net, column, replace, elements=None, continue_on_missing_column=True)

Adds column data to elements, inferring them from the column data of the elements linked by the columns “element” and “element_type” or “et”.

INPUT:

net (pandapowerNet) - the pandapower net that will be changed

column (string) - name of column that should be copied from the tables of the elements.

replace (boolean) - if True, an existing column will be overwritten

elements (list) - list of elements that should get the column values from the linked element tables. If None, all elements with the columns “element” and “element_type” or “et” are considered (these are currently “measurement” and “switch”).

continue_on_missing_column (Boolean, True) - If False, a error will be raised in case of an element table has no column ‘column’ although this element is refered in ‘elements’. E.g. ‘measurement’ is in ‘elements’ and in net.measurement is a trafo measurement but in net.trafo there is no column ‘name’ although column==’name’ - ni this case ‘continue_on_missing_column’ acts.

EXAMPLE:

import pandapower as pp import pandapower.networks as pn net = pn.create_cigre_network_mv() pp.create_measurement(net, “i”, “trafo”, 5, 3, 0, side=”hv”) pp.create_measurement(net, “i”, “line”, 5, 3, 0, side=”to”) pp.create_measurement(net, “p”, “bus”, 5, 3, 2) print(net.measurement.name.values, net.switch.name.values) pp.add_column_from_element_to_elements(net, “name”, True) print(net.measurement.name.values, net.switch.name.values)

pandapower.add_zones_to_elements(net, replace=True, elements=None, **kwargs)

Adds zones to elements, inferring them from the zones of buses they are connected to.

pandapower.reindex_buses(net, bus_lookup)

Changes the index of net.bus and considers the new bus indices in all other pandapower element tables.

INPUT:

net - pandapower network

bus_lookup (dict) - the keys are the old bus indices, the values the new bus indices

pandapower.create_continuous_bus_index(net, start=0, store_old_index=False)

Creates a continuous bus index starting at ‘start’ and replaces all references of old indices by the new ones.

INPUT:

net - pandapower network

OPTIONAL:

start - index begins with “start”

store_old_index - if True, stores the old index in net.bus[“old_index”]

OUTPUT:

bus_lookup - mapping of old to new index

pandapower.reindex_elements(net, element, new_indices, old_indices=None)

Changes the index of net[element].

INPUT:

net - pandapower network

element (str) - name of the element table

new_indices (iterable) - list of new indices

OPTIONAL:

old_indices (iterable) - list of old/previous indices which will be replaced. If None, all indices are considered.

pandapower.create_continuous_elements_index(net, start=0, add_df_to_reindex={})

Creating a continuous index for all the elements, starting at zero and replaces all references of old indices by the new ones.

INPUT:

net - pandapower network with unodered indices

OPTIONAL:

start - index begins with “start”

add_df_to_reindex - by default all useful pandapower elements for power flow will be selected. Customized DataFrames can also be considered here.

OUTPUT:

net - pandapower network with odered and continuous indices

pandapower.set_scaling_by_type(net, scalings, scale_load=True, scale_sgen=True)

Sets scaling of loads and/or sgens according to a dictionary mapping type to a scaling factor. Note that the type-string is case sensitive. E.g. scaling = {“pv”: 0.8, “bhkw”: 0.6}

Parameters
  • net

  • scalings – A dictionary containing a mapping from element type to

  • scale_load

  • scale_sgen

pandapower.convert_format(net)

Converts old nets to new format to ensure consistency. The converted net is returned.

pandapower.set_data_type_of_columns_to_default(net)

Overwrites dtype of DataFrame columns of PandapowerNet elements to default dtypes defined in pandapower. The function “convert_format” does that authomatically for nets saved with pandapower versions below 1.6. If this is required for versions starting with 1.6, it should be done manually with this function.

INPUT:

net - pandapower network with unodered indices

OUTPUT:

No output; the net passed as input has pandapower-default dtypes of columns in element tables.

Topology Modification

pandapower.close_switch_at_line_with_two_open_switches(net)

Finds lines that have opened switches at both ends and closes one of them. Function is usually used when optimizing section points to prevent the algorithm from ignoring isolated lines.

pandapower.fuse_buses(net, b1, b2, drop=True, fuse_bus_measurements=True)

Reroutes any connections to buses in b2 to the given bus b1. Additionally drops the buses b2, if drop=True (default).

pandapower.drop_buses(net, buses, drop_elements=True)

Drops specified buses, their bus_geodata and by default drops all elements connected to them as well.

pandapower.drop_switches_at_buses(net, buses)
pandapower.drop_elements_at_buses(net, buses, bus_elements=True, branch_elements=True, drop_measurements=True)

drop elements connected to given buses

pandapower.drop_trafos(net, trafos, table='trafo')

Deletes all trafos and in the given list of indices and removes any switches connected to it.

pandapower.drop_lines(net, lines)

Deletes all lines and their geodata in the given list of indices and removes any switches connected to it.

pandapower.drop_duplicated_measurements(net, buses=None, keep='first')

Drops duplicated measurements at given set of buses. If buses is None, all buses are considered.

pandapower.set_element_status(net, buses, in_service)

Sets buses and all elements connected to them in or out of service.

pandapower.set_isolated_areas_out_of_service(net, respect_switches=True)

Set all isolated buses and all elements connected to isolated buses out of service.

pandapower.drop_out_of_service_elements(net)

Drop all elements (including corresponding dataframes such as switches, measurements, result tables, geodata) with “in_service” is False. Buses that are connected to in-service branches are not deleted.

pandapower.drop_inactive_elements(net, respect_switches=True)

Drops any elements not in service AND any elements connected to inactive buses.

pandapower.select_subnet(net, buses, include_switch_buses=False, include_results=False, keep_everything_else=False)

Selects a subnet by a list of bus indices and returns a net with all elements connected to them.

pandapower.merge_nets(net1, net2, validate=True, merge_results=True, tol=1e-09, create_continuous_bus_indices=True, **kwargs)

Function to concatenate two nets into one data structure. All element tables get new, continuous indizes in order to avoid duplicates.

pandapower.create_replacement_switch_for_branch(net, element, idx)

Creates a switch parallel to a branch, connecting the same buses as the branch. The switch is closed if the branch is in service and open if the branch is out of service. The in_service status of the original branch is not affected and should be set separately, if needed.

Parameters
  • net – pandapower network

  • element – element table e. g. ‘line’, ‘impedance’

  • idx – index of the branch e. g. 0

Returns

None

pandapower.replace_zero_branches_with_switches(net, elements=('line', 'impedance'), zero_length=True, zero_impedance=True, in_service_only=True, min_length_km=0, min_r_ohm_per_km=0, min_x_ohm_per_km=0, min_c_nf_per_km=0, min_rft_pu=0, min_xft_pu=0, min_rtf_pu=0, min_xtf_pu=0, drop_affected=False)

Creates a replacement switch for branches with zero impedance (line, impedance) and sets them out of service.

Parameters
  • net – pandapower network

  • elements – a tuple of names of element tables e. g. (‘line’, ‘impedance’) or (line)

  • zero_length – whether zero length lines will be affected

  • zero_impedance – whether zero impedance branches will be affected

  • in_service_only – whether the branches that are not in service will be affected

  • drop_affected – wheter the affected branch elements are dropped

  • min_length_km – threshhold for line length for a line to be considered zero line

  • min_r_ohm_per_km – threshhold for line R’ value for a line to be considered zero line

  • min_x_ohm_per_km – threshhold for line X’ value for a line to be considered zero line

  • min_c_nf_per_km – threshhold for line C’ for a line to be considered zero line

  • min_rft_pu – threshhold for R from-to value for impedance to be considered zero impedance

  • min_xft_pu – threshhold for X from-to value for impedance to be considered zero impedance

  • min_rtf_pu – threshhold for R to-from value for impedance to be considered zero impedance

  • min_xtf_pu – threshhold for X to-from value for impedance to be considered zero impedance

Returns

pandapower.replace_impedance_by_line(net, index=None, only_valid_replace=True, max_i_ka=nan)

Creates lines by given impedances data, while the impedances are dropped. INPUT:

net - pandapower net

OPTIONAL:

index (index, None) - Index of all impedances to be replaced. If None, all impedances will be replaced.

only_valid_replace (bool, True) - If True, impedances will only replaced, if a replacement leads to equal power flow results. If False, unsymmetric impedances will be replaced by symmetric lines.

max_i_ka (value(s), False) - Data/Information how to set max_i_ka. If ‘imp.sn_mva’ is given, the sn_mva values of the impedances are considered.

pandapower.replace_line_by_impedance(net, index=None, sn_mva=None, only_valid_replace=True)

Creates impedances by given lines data, while the lines are dropped. INPUT:

net - pandapower net

OPTIONAL:

index (index, None) - Index of all lines to be replaced. If None, all lines will be replaced.

sn_kva (list or array, None) - Values of sn_kva for creating the impedances. If None, the net.sn_kva is assumed

only_valid_replace (bool, True) - If True, lines will only replaced, if a replacement leads to equal power flow results. If False, capacitance and dielectric conductance will be neglected.

pandapower.replace_ext_grid_by_gen(net, ext_grids=None, gen_indices=None, slack=False, cols_to_keep=None, add_cols_to_keep=None)

Replaces external grids by generators.

INPUT:

net - pandapower net

OPTIONAL:

ext_grids (iterable) - indices of external grids which should be replaced

gen_indices (iterable) - required indices of new generators

slack (bool, False) - indicates which value is set to net.gen.slack for the new generators

cols_to_keep (list, None) - list of column names which should be kept while replacing ext_grids. If None these columns are kept if values exist: “max_p_mw”, “min_p_mw”, “max_q_mvar”, “min_q_mvar”. However cols_to_keep is given, these columns are alway set: “bus”, “vm_pu”, “p_mw”, “name”, “in_service”, “controllable”

add_cols_to_keep (list, None) - list of column names which should be added to ‘cols_to_keep’ to be kept while replacing ext_grids.

pandapower.replace_gen_by_ext_grid(net, gens=None, ext_grid_indices=None, cols_to_keep=None, add_cols_to_keep=None)

Replaces generators by external grids.

INPUT:

net - pandapower net

OPTIONAL:

gens (iterable) - indices of generators which should be replaced

ext_grid_indices (iterable) - required indices of new external grids

cols_to_keep (list, None) - list of column names which should be kept while replacing gens. If None these columns are kept if values exist: “max_p_mw”, “min_p_mw”, “max_q_mvar”, “min_q_mvar”. However cols_to_keep is given, these columns are alway set: “bus”, “vm_pu”, “va_degree”, “name”, “in_service”

add_cols_to_keep (list, None) - list of column names which should be added to ‘cols_to_keep’ to be kept while replacing gens.

pandapower.replace_gen_by_sgen(net, gens=None, sgen_indices=None, cols_to_keep=None, add_cols_to_keep=None)

Replaces generators by static generators.

INPUT:

net - pandapower net

OPTIONAL:

gens (iterable) - indices of generators which should be replaced

sgen_indices (iterable) - required indices of new static generators

cols_to_keep (list, None) - list of column names which should be kept while replacing gens. If None these columns are kept if values exist: “max_p_mw”, “min_p_mw”, “max_q_mvar”, “min_q_mvar”. However cols_to_keep is given, these columns are always set: “bus”, “p_mw”, “q_mvar”, “name”, “in_service”, “controllable”

add_cols_to_keep (list, None) - list of column names which should be added to ‘cols_to_keep’ to be kept while replacing gens.

pandapower.replace_sgen_by_gen(net, sgens=None, gen_indices=None, cols_to_keep=None, add_cols_to_keep=None)

Replaces static generators by generators.

INPUT:

net - pandapower net

OPTIONAL:

sgens (iterable) - indices of static generators which should be replaced

gen_indices (iterable) - required indices of new generators

cols_to_keep (list, None) - list of column names which should be kept while replacing sgens. If None these columns are kept if values exist: “max_p_mw”, “min_p_mw”, “max_q_mvar”, “min_q_mvar”. However cols_to_keep is given, these columns are always set: “bus”, “vm_pu”, “p_mw”, “name”, “in_service”, “controllable”

add_cols_to_keep (list, None) - list of column names which should be added to ‘cols_to_keep’ to be kept while replacing sgens.

pandapower.replace_ward_by_internal_elements(net, wards=None)

Replaces wards by loads and shunts INPUT:

net - pandapower net

OPTIONAL:

wards (iterable) - indices of xwards which should be replaced

OUTPUT:

No output - the given wards in pandapower net are replaced by loads and shunts

pandapower.replace_xward_by_internal_elements(net, xwards=None)

Replaces xward by loads, shunts, impedance and generators INPUT:

net - pandapower net

OPTIONAL:

xwards (iterable) - indices of xwards which should be replaced

OUTPUT:

No output - the given xwards in pandapower are replaced by buses, loads, shunts, impadance and generators

Item/Element Selection

pandapower.get_element_index(net, element, name, exact_match=True)

Returns the element(s) identified by a name or regex and its element-table.

INPUT:

net - pandapower network

element - Table to get indices from (“line”, “bus”, “trafo” etc.)

name - Name of the element to match.

OPTIONAL:
exact_match (boolean, True) - True: Expects exactly one match, raises

UserWarning otherwise.

False: returns all indices containing the name

OUTPUT:

index - The indices of matching element(s).

pandapower.get_element_indices(net, element, name, exact_match=True)

Returns a list of element(s) identified by a name or regex and its element-table -> Wrapper function of get_element_index()

INPUT:

net - pandapower network

element (str or iterable of strings) - Element table to get indices from (“line”, “bus”,

“trafo” etc.)

name (str or iterable of strings) - Name of the element to match.

OPTIONAL:
exact_match (boolean, True) - True: Expects exactly one match, raises

UserWarning otherwise.

False: returns all indices containing the name

OUTPUT:

index (list) - List of the indices of matching element(s).

EXAMPLE:

import pandapower.networks as pn import pandapower as pp net = pn.example_multivoltage() idx1 = pp.get_element_indices(net, “bus”, [“Bus HV%i” % i for i in range(1, 4)]) idx2 = pp.get_element_indices(net, [“bus”, “line”], “HV”, exact_match=False) idx3 = pp.get_element_indices(net, [“bus”, “line”], [“Bus HV3”, “MV Line6”])

pandapower.next_bus(net, bus, element_id, et='line', **kwargs)

Returns the index of the second bus an element is connected to, given a first one. E.g. the from_bus given the to_bus of a line.

pandapower.get_connected_elements(net, element, buses, respect_switches=True, respect_in_service=False)

Returns elements connected to a given bus.

INPUT:

net (pandapowerNet)

element (string, name of the element table)

buses (single integer or iterable of ints)

OPTIONAL:
respect_switches (boolean, True) - True: open switches will be respected

False: open switches will be ignored

respect_in_service (boolean, False) - True: in_service status of connected lines will be

respected

False: in_service status will be ignored

OUTPUT:

connected_elements (set) - Returns connected elements.

pandapower.get_connected_buses(net, buses, consider=('l', 's', 't', 't3', 'i'), respect_switches=True, respect_in_service=False)

Returns buses connected to given buses. The source buses will NOT be returned.

INPUT:

net (pandapowerNet)

buses (single integer or iterable of ints)

OPTIONAL:
respect_switches (boolean, True) - True: open switches will be respected

False: open switches will be ignored

respect_in_service (boolean, False) - True: in_service status of connected buses

will be respected False: in_service status will be ignored

consider (iterable, (“l”, “s”, “t”, “t3”, “i”)) - Determines, which types of connections will be considered.

l: lines s: switches t: trafos t3: trafo3ws i: impedances

OUTPUT:

cl (set) - Returns connected buses.

pandapower.get_connected_buses_at_element(net, element, et, respect_in_service=False)

Returns buses connected to a given line, switch or trafo. In case of a bus switch, two buses will be returned, else one.

INPUT:

net (pandapowerNet)

element (integer)

et (string) - Type of the source element:

l, line: line s, switch: switch t, trafo: trafo t3, trafo3w: trafo3w i, impedance: impedance

OPTIONAL:
respect_in_service (boolean, False) - True: in_service status of connected buses

will be respected

False: in_service status will be ignored

OUTPUT:

cl (set) - Returns connected switches.

pandapower.get_connected_switches(net, buses, consider=('b', 'l', 't', 't3'), status='all')

Returns switches connected to given buses.

INPUT:

net (pandapowerNet)

buses (single integer or iterable of ints)

OPTIONAL:
consider (iterable, (“l”, “s”, “t”, “t3)) - Determines, which types of connections

will be considered. l: lines b: bus-bus-switches t: transformers t3: 3W transformers

status (string, (“all”, “closed”, “open”)) - Determines, which switches will

be considered

OUTPUT:

cl (set) - Returns connected switches.

pandapower.get_connected_elements_dict(net, buses, respect_switches=True, respect_in_service=False, include_empty_lists=False, connected_buses=True, connected_bus_elements=True, connected_branch_elements=True, connected_other_elements=True)

Returns a dict of lists of connected elements.