Skip to content

Commit

Permalink
Using loc.
Browse files Browse the repository at this point in the history
  • Loading branch information
quant12345 committed Jul 13, 2024
1 parent a043eae commit f9499f5
Show file tree
Hide file tree
Showing 23 changed files with 101 additions and 101 deletions.
2 changes: 1 addition & 1 deletion pandapower/converter/cim/cim2pp/convert_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def _copy_to_measurement(self, input_df: pd.DataFrame):
ignore_index=True, sort=False)
for one_attr in self.net[pp_type].columns:
if one_attr in input_df.columns:
self.net[pp_type][one_attr][start_index_pp_net:] = input_df[one_attr][:]
self.net[pp_type].loc[start_index_pp_net:, one_attr] = input_df[one_attr][:]

def create_measurements_from_analog(self):
self.logger.info("------------------------- Creating measurements from Analog -------------------------")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def add_coordinates_from_dl_cim16(self, diagram_name: str = None):
one_ele_df['coords'] = one_ele_df[['xPosition', 'yPosition']].values.tolist()
one_ele_df['coords'] = one_ele_df[['coords']].values.tolist()
for _, df_group in one_ele_df.groupby(by=sc['o_id']):
one_ele_df['coords'][df_group.index.values[0]] = df_group[
one_ele_df.at[df_group.index.values[0], 'coords'] = df_group[
['xPosition', 'yPosition']].values.tolist()
one_ele_df = one_ele_df.drop_duplicates([sc['o_id']], keep='first')
one_ele_df['coords'] = one_ele_df['coords'].astype(str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def add_geo_coordinates_from_gl_cim16(self):
line_geo['coords'] = line_geo[['xPosition', 'yPosition']].values.tolist()
line_geo['coords'] = line_geo[['coords']].values.tolist()
for _, df_group in line_geo.groupby(by=sc['o_id']):
line_geo['coords'][df_group.index.values[0]] = df_group[['xPosition', 'yPosition']].values.tolist()
line_geo.at[df_group.index.values[0], 'coords'] = df_group[['xPosition', 'yPosition']].values.tolist()
line_geo = line_geo.drop_duplicates([sc['o_id']], keep='first')
line_geo = line_geo.sort_values(by='index')
line_geo['geo'] = '{"coordinates": ' + line_geo['coords'].astype(str) + ', "type": "LineString"}'
Expand All @@ -83,7 +83,7 @@ def add_geo_coordinates_from_gl_cim16(self):
one_ele_df['coords'] = one_ele_df[['xPosition', 'yPosition']].values.tolist()
one_ele_df['coords'] = one_ele_df[['coords']].values.tolist()
for _, df_group in one_ele_df.groupby(by=sc['o_id']):
one_ele_df['coords'][df_group.index.values[0]] = df_group[
one_ele_df.at[df_group.index.values[0], 'coords'] = df_group[
['xPosition', 'yPosition']].values.tolist()
one_ele_df = one_ele_df.drop_duplicates([sc['o_id']], keep='first')
one_ele_df['coords'] = one_ele_df['coords'].astype(str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def _prepare_external_network_injections_cim16(self) -> pd.DataFrame:
how='left', left_on='ConnectivityNode', right_on='b_id')

# convert pu generators with prio = 0 to pq generators (PowerFactory does it same)
eni['referencePriority'].loc[eni['referencePriority'] == 0] = -1
eni.loc[eni['referencePriority'] == 0, 'referencePriority'] = -1
eni['referencePriority'] = eni['referencePriority'].astype(float)
eni['controlEnabled'].loc[eni['referencePriority'] == -1] = False
eni.loc[eni['referencePriority'] == -1, 'controlEnabled'] = False
eni['p'] = -eni['p']
eni['q'] = -eni['q']
eni['x0x_max'] = ((eni['maxR1ToX1Ratio'] + 1j) /
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ def _prepare_switches_cim16(self) -> pd.DataFrame:
pd.concat(
[eqssh_switches, self.cimConverter.merge_eq_ssh_profile('Disconnector', add_cim_type_column=True)],
ignore_index=True, sort=False)
eqssh_switches.type[start_index_cim_net:] = 'DS'
eqssh_switches.loc[start_index_cim_net:, 'type'] = 'DS'
start_index_cim_net = eqssh_switches.index.size
eqssh_switches = \
pd.concat(
[eqssh_switches, self.cimConverter.merge_eq_ssh_profile('LoadBreakSwitch', add_cim_type_column=True)],
ignore_index=True, sort=False)
eqssh_switches.type[start_index_cim_net:] = 'LBS'
eqssh_switches.loc[start_index_cim_net:, 'type'] = 'LBS'
start_index_cim_net = eqssh_switches.index.size
# switches needs to be the last which getting appended because of class inherit problem in jpa
eqssh_switches = pd.concat(
[eqssh_switches, self.cimConverter.merge_eq_ssh_profile('Switch', add_cim_type_column=True)],
ignore_index=True, sort=False)
eqssh_switches.type[start_index_cim_net:] = 'LS'
eqssh_switches.loc[start_index_cim_net:, 'type'] = 'LS'
# drop all duplicates to fix class inherit problem in jpa
eqssh_switches = eqssh_switches.drop_duplicates(subset=['rdfId'], keep='first')
switch_length_before_merge = eqssh_switches.index.size
Expand Down
16 changes: 8 additions & 8 deletions pandapower/grid_equivalents/auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,16 @@ def calc_zpbn_parameters(net, boundary_buses, all_external_buses, slack_as="gen"
if i in net[ele].bus.values and net[ele].in_service[net[ele].bus == i].values.any():
ind = list(net[ele].index[net[ele].bus == i].values)
# act. values --> ref. values:
S[power][k] += sum(net[res_ele].p_mw[ind].values * sign) / net.sn_mva + \
S.loc[k, power] += sum(net[res_ele].p_mw[ind].values * sign) / net.sn_mva + \
1j * sum(net[res_ele].q_mvar[ind].values *
sign) / net.sn_mva
S[sn][k] = sum(net[ele].sn_mva[ind].values) + \
S.loc[k, sn] = sum(net[ele].sn_mva[ind].values) + \
1j * 0 if ele != "ext_grid" else 1e6 + 1j * 0
S[power.replace('_separate', '_integrated')] += S[power][k]
S[sn.replace('_separate', '_integrated')] += S[sn][k]
S.ext_bus[k] = all_external_buses[k]
S.v_m[k] = net.res_bus.vm_pu[i]
S.v_cpx[k] = S.v_m[k] * \
S.loc[k, 'ext_bus'] = all_external_buses[k]
S.loc[k, 'v_m'] = net.res_bus.vm_pu[i]
S.loc[k, 'v_cpx'] = S.v_m[k] * \
np.exp(1j * net.res_bus.va_degree[i] * np.pi / 180)
k = k + 1

Expand Down Expand Up @@ -445,7 +445,7 @@ def match_cost_functions_and_eq_net(net, boundary_buses, eq_type):
for pc in net[cost_elm].itertuples():
new_idx = net[pc.et].index[
net[pc.et].origin_id == pc.et_origin_id].values
net[cost_elm].element[pc.Index] = new_idx[0]
net[cost_elm].loc[pc.Index, 'element'] = new_idx[0]
net[cost_elm] = net[cost_elm].drop(columns=["bus", "et_origin_id", "origin_idx", "origin_seq"])


Expand Down Expand Up @@ -511,7 +511,7 @@ def adaptation_phase_shifter(net, v_boundary, p_boundary):
net[e].to_bus[i] = hb
elif e == "trafo":
if net[e].hv_bus[i] == lb:
net[e].hv_bus[i] = hb
net[e].loc[i, 'hv_bus'] = hb
else:
net[e].lv_bus[i] = hb
elif e == "trafo3w":
Expand All @@ -524,7 +524,7 @@ def adaptation_phase_shifter(net, v_boundary, p_boundary):
elif e in ["bus", "load", "sgen", "gen", "shunt", "ward", "xward"]:
pass
else:
net[e].bus[i] = hb
net[e].loc[i, 'bus'] = hb
pp.create_transformer_from_parameters(net, hb, lb, 1e5,
net.bus.vn_kv[hb]*(1-vm_errors[idx]),
net.bus.vn_kv[lb],
Expand Down
18 changes: 9 additions & 9 deletions pandapower/grid_equivalents/rei_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def _create_net_zpbn(net, boundary_buses, all_internal_buses, all_external_buses
else:
if elm == "gen" and bus in net.ext_grid.bus.values and \
net.ext_grid.in_service[net.ext_grid.bus == bus].values[0]:
net_zpbn[elm].name[elm_idx] = str(net.ext_grid.name[
net_zpbn[elm].loc[elm_idx, 'name'] = str(net.ext_grid.name[
net.ext_grid.bus == bus].values[0]) + "-" + net_zpbn[elm].name[elm_idx]
ext_grid_cols = list(set(elm_org.columns) & set(net.ext_grid.columns) - \
{"name", "bus", "p_mw", "sn_mva", "in_service", "scaling"})
Expand All @@ -323,7 +323,7 @@ def _create_net_zpbn(net, boundary_buses, all_internal_buses, all_external_buses
else:
names = elm_org.name[elm_org.bus == bus].values
names = [str(n) for n in names]
net_zpbn[elm].name[elm_idx] = "//".join(names) + "-" + net_zpbn[elm].name[elm_idx]
net_zpbn[elm].loc[elm_idx, 'name'] = "//".join(names) + "-" + net_zpbn[elm].name[elm_idx]
if len(names) > 1:
net_zpbn[elm].loc[elm_idx, list(other_cols_number)] = \
elm_org[list(other_cols_number)][elm_org.bus == bus].sum(axis=0)
Expand Down Expand Up @@ -358,10 +358,10 @@ def _create_net_zpbn(net, boundary_buses, all_internal_buses, all_external_buses
for cost_elm in ["poly_cost", "pwl_cost"]:
if len(net[cost_elm]):
df = net_zpbn[cost_elm].copy()
df.et[(df.et == "ext_grid") &
(~df.bus.isin(boundary_buses))] = "gen"
df.et[(df.et.isin(["storage", "dcline"]) &
(~df.bus.isin(boundary_buses)))] = "load"
df.loc[(df.et == "ext_grid") &
(~df.bus.isin(boundary_buses)), 'et'] = "gen"
df.loc[(df.et.isin(["storage", "dcline"]) &
(~df.bus.isin(boundary_buses))), 'et'] = "load"

logger.debug("During the equivalencing, also in polt_cost, " +
"storages and dclines are treated as loads, and" +
Expand Down Expand Up @@ -397,7 +397,7 @@ def _create_net_zpbn(net, boundary_buses, all_internal_buses, all_external_buses
df.element[pc_idx[0]] = idx
df = df.drop(pc_idx[1:])
elif len(pc_idx) == 1:
df.element[pc_idx[0]] = idx
df.loc[pc_idx[0], 'element'] = idx
net_zpbn[cost_elm] = df

drop_and_edit_cost_functions(net_zpbn, [], False, True, False)
Expand Down Expand Up @@ -701,5 +701,5 @@ def _integrate_power_elements_connected_with_switch_buses(net, net_external, all
else: # There ars some "external" elements connected with bus-bus switches.
# They will be aggregated.
elm1 = connected_elms[0]
net[elm].bus[connected_elms] = net[elm].bus[elm1]
net_external[elm].bus[connected_elms] = net_external[elm].bus[elm1]
net[elm].loc[connected_elms, 'bus'] = net[elm].bus[elm1]
net_external[elm].loc[connected_elms, 'bus'] = net_external[elm].bus[elm1]
20 changes: 10 additions & 10 deletions pandapower/grid_equivalents/ward_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def _calculate_ward_and_impedance_parameters(Ybus_eq, bus_lookups, show_computin
for j in range(nb_b_buses_ppc):
if j > i:
if np.abs(params[i, j]) > 1e-10:
impedance_parameter.from_bus[k] = b_buses_pd[i]
impedance_parameter.to_bus[k] = b_buses_pd[j]
impedance_parameter.rft_pu[k] = (-1 / params[i, j]).real
impedance_parameter.xft_pu[k] = (-1 / params[i, j]).imag
impedance_parameter.rtf_pu[k] = (-1 / params[j, i]).real
impedance_parameter.xtf_pu[k] = (-1 / params[j, i]).imag
impedance_parameter.loc[k, 'from_bus'] = b_buses_pd[i]
impedance_parameter.loc[k, 'to_bus'] = b_buses_pd[j]
impedance_parameter.loc[k, 'rft_pu'] = (-1 / params[i, j]).real
impedance_parameter.loc[k, 'xft_pu'] = (-1 / params[i, j]).imag
impedance_parameter.loc[k, 'rtf_pu'] = (-1 / params[j, i]).real
impedance_parameter.loc[k, 'xtf_pu'] = (-1 / params[j, i]).imag
k += 1
else:
impedance_parameter = impedance_parameter[:-1]
Expand Down Expand Up @@ -173,9 +173,9 @@ def _replace_external_area_by_wards(net_external, bus_lookups, ward_parameter_no
eq_power.q_mvar -= \
pd.concat([net_external.res_ext_grid.q_mvar, net_external.res_gen.q_mvar[slack_gen]])
for bus in eq_power.bus:
net_external.ward.ps_mw[net_external.ward.bus==bus] = \
net_external.ward.loc[net_external.ward.bus==bus, 'ps_mw'] = \
eq_power.p_mw[eq_power.bus==bus].values
net_external.ward.qs_mvar[net_external.ward.bus==bus] = \
net_external.ward.loc[net_external.ward.bus==bus, 'qs_mvar'] = \
eq_power.q_mvar[eq_power.bus==bus].values

net_external.poly_cost = net_external.poly_cost[0:0]
Expand Down Expand Up @@ -257,9 +257,9 @@ def _replace_external_area_by_xwards(net_external, bus_lookups, xward_parameter_
eq_power.q_mvar -= \
pd.concat([net_external.res_ext_grid.q_mvar, net_external.res_gen.q_mvar[slack_gen]])
for bus in eq_power.bus:
net_external.xward.ps_mw[net_external.xward.bus==bus] = \
net_external.xward.loc[net_external.xward.bus==bus, 'ps_mw'] = \
eq_power.p_mw[eq_power.bus==bus].values
net_external.xward.qs_mvar[net_external.xward.bus==bus] = \
net_external.xward.loc[net_external.xward.bus==bus, 'qs_mvar'] = \
eq_power.q_mvar[eq_power.bus==bus].values

net_external.poly_cost=net_external.poly_cost[0:0]
Expand Down
2 changes: 1 addition & 1 deletion pandapower/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ def set_group_reference_column(net, index, reference_column, element_type=None):
net[et][reference_column] = pd.Series([None]*net[et].shape[0], dtype=object)
if pd.api.types.is_object_dtype(net[et][reference_column]):
idxs = net[et].index[net[et][reference_column].isnull()]
net[et][reference_column].loc[idxs] = ["%s_%i_%s" % (et, idx, str(
net[et].loc[idxs, reference_column] = ["%s_%i_%s" % (et, idx, str(
uuid.uuid4())) for idx in idxs]

# determine duplicated values which would corrupt Groups functionality
Expand Down
4 changes: 2 additions & 2 deletions pandapower/protection/utility_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ def create_sc_bus(net_copy, sc_line_id, sc_fraction):
# check if switches are connected to the line and set the switches to new lines
for switch_id in net.switch.index:
if (aux_line.from_bus == net.switch.bus[switch_id]) & (net.switch.element[switch_id] == sc_line_id):
net.switch.element[switch_id] = sc_line1
net.switch.loc[switch_id, 'element'] = sc_line1
elif (aux_line.to_bus == net.switch.bus[switch_id]) & (net.switch.element[switch_id] == sc_line_id):
net.switch.element[switch_id] = sc_line2

# set geodata for new bus
net.bus.geo.loc[max_idx_bus + 1] = None
net.bus.loc[max_idx_bus + 1, 'geo'] = None

x1, y1 = _get_coords_from_bus_idx(net, aux_line.from_bus)[0]
x2, y2 = _get_coords_from_bus_idx(net, aux_line.to_bus)[0]
Expand Down
8 changes: 4 additions & 4 deletions pandapower/shortcircuit/results.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ def _get_bus_results(net, ppc, ppc_0, bus):
# in trafo3w, we add very high numbers (1e10) as impedances to block current
# here, we need to replace such high values by np.inf
baseZ = ppc_0["bus"][ppc_index, BASE_KV] ** 2 / ppc_0["baseMVA"]
net.res_bus_sc["xk0_ohm"].loc[net.res_bus_sc["xk0_ohm"]/baseZ > 1e9] = np.inf
net.res_bus_sc["rk0_ohm"].loc[net.res_bus_sc["rk0_ohm"]/baseZ > 1e9] = np.inf
net.res_bus_sc.loc[net.res_bus_sc["xk0_ohm"]/baseZ > 1e9, "xk0_ohm"] = np.inf
net.res_bus_sc.loc[net.res_bus_sc["rk0_ohm"]/baseZ > 1e9, "rk0_ohm"] = np.inf
else:
net.res_bus_sc["ikss_ka"] = ppc["bus"][ppc_index, IKSS1] + ppc["bus"][ppc_index, IKSS2]
net.res_bus_sc["skss_mw"] = ppc["bus"][ppc_index, SKSS]
Expand All @@ -106,8 +106,8 @@ def _get_bus_results(net, ppc, ppc_0, bus):
net.res_bus_sc["xk_ohm"] = ppc["bus"][ppc_index, X_EQUIV_OHM]
# if for some reason (e.g. contribution of ext_grid set close to 0) we used very high values for rk, xk, we replace them by np.inf
baseZ = ppc["bus"][ppc_index, BASE_KV] ** 2 / ppc["baseMVA"]
net.res_bus_sc["rk_ohm"].loc[net.res_bus_sc["rk_ohm"] / baseZ > 1e9] = np.inf
net.res_bus_sc["xk_ohm"].loc[net.res_bus_sc["xk_ohm"] / baseZ > 1e9] = np.inf
net.res_bus_sc.loc[net.res_bus_sc["rk_ohm"] / baseZ > 1e9, "rk_ohm"] = np.inf
net.res_bus_sc.loc[net.res_bus_sc["xk_ohm"] / baseZ > 1e9, "xk_ohm"] = np.inf

net.res_bus_sc = net.res_bus_sc.loc[bus, :]

Expand Down
2 changes: 1 addition & 1 deletion pandapower/test/api/test_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def test_elements_connected_to_group():
pp.create_switches(net, [0]*2, [10, 11], "b", closed=[True, False])
net.load.at[0, "in_service"] = False
net.line.at[4, "in_service"] = False
net.bus.in_service.loc[[3, 9]] = False
net.bus.loc[[3, 9], 'in_service'] = False

# create group
index = pp.create_group(net, ["bus", "line", "switch"], [[0], [net.line.index[-1]], [6, 7]])
Expand Down
4 changes: 2 additions & 2 deletions pandapower/test/control/test_continuous_tap_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def test_continuous_tap_control_vectorized_lv():
pp.create_transformer(net, hv, lv, "63 MVA 110/20 kV")
pp.create_load(net, lv, 25*(lv-8), 25*(lv-8) * 0.4)
pp.set_user_pf_options(net, init='dc', calculate_voltage_angles=True)
net.trafo.tap_side.iloc[3:] = "lv"
net.trafo.loc[3:, 'tap_side'] = "lv"
tol = 1e-4
# --- run loadflow
pp.runpp(net)
Expand Down Expand Up @@ -200,7 +200,7 @@ def test_continuous_tap_control_vectorized_hv():
pp.create_transformer(net, hv, lv, "63 MVA 110/20 kV")
pp.create_load(net, hv, 2.5*(hv-8), 2.5*(hv-8) * 0.4)
pp.set_user_pf_options(net, init='dc', calculate_voltage_angles=True)
net.trafo.tap_side.iloc[3:] = "lv"
net.trafo.loc[3:, 'tap_side'] = "lv"
tol = 1e-4
# --- run loadflow
pp.runpp(net)
Expand Down
4 changes: 2 additions & 2 deletions pandapower/test/control/test_discrete_tap_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def test_discrete_tap_control_vectorized_lv():
pp.create_transformer(net, hv, lv, "63 MVA 110/20 kV")
pp.create_load(net, lv, 25*(lv-8), 25*(lv-8) * 0.4)
pp.set_user_pf_options(net, init='dc', calculate_voltage_angles=True)
net.trafo.tap_side.iloc[3:] = "lv"
net.trafo.loc[3:, 'tap_side'] = "lv"
# --- run loadflow
pp.runpp(net)
assert not all(_vm_in_desired_area(net, 1.01, 1.03, "lv")) # there should be something
Expand Down Expand Up @@ -332,7 +332,7 @@ def test_discrete_tap_control_vectorized_hv():
pp.create_transformer(net, hv, lv, "63 MVA 110/20 kV")
pp.create_load(net, hv, 2.5*(hv-8), 2.5*(hv-8) * 0.4)
pp.set_user_pf_options(net, init='dc', calculate_voltage_angles=True)
net.trafo.tap_side.iloc[3:] = "lv"
net.trafo.loc[3:, 'tap_side'] = "lv"
# --- run loadflow
pp.runpp(net)
assert not all(_vm_in_desired_area(net, 1.01, 1.03, "hv")) # there should be something
Expand Down
2 changes: 1 addition & 1 deletion pandapower/test/grid_equivalents/test_get_equivalent.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_basic_usecases():
def test_case9_with_slack_generator_in_external_net():
net = pp.networks.case9()
idx = pp.replace_ext_grid_by_gen(net)
net.gen.slack.loc[idx] = True
net.gen.loc[idx, 'slack'] = True
pp.runpp(net)

# since the only slack is in the external_buses, we expect get_equivalent() to move the slack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_drop_internal_branch_elements():

def test_trafo_phase_shifter():
net = pp.networks.create_cigre_network_mv(with_der="pv_wind")
net.trafo.shift_degree[0] = 150
net.trafo.loc[0, 'shift_degree'] = 150
pp.runpp(net)
net_eq = pp.grid_equivalents.get_equivalent(net, "rei", [4, 8], [0],
retain_original_internal_indices=True)
Expand Down Expand Up @@ -102,11 +102,11 @@ def test_drop_measurements_and_controllers():

def test_check_network():
net = pp.networks.case9()
net.bus.in_service[5] = False
net.bus.loc[5, 'in_service'] = False
pp.runpp(net)
_check_network(net)

net.bus.in_service[5] = True
net.bus.loc[5, 'in_service'] = True
pp.runpp(net)
pp.create_bus(net, net.bus.vn_kv.values[0])
pp.create_bus(net, net.bus.vn_kv.values[0])
Expand Down
Loading

0 comments on commit f9499f5

Please sign in to comment.