def test_add_column_from_element_to_elements(): net = nw.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) assert net.measurement.name.isnull().all() assert ~net.switch.name.isnull().all() orig_switch_names = copy.deepcopy(net.switch.name.values) expected_measurement_names = np.array( [net.trafo.name.loc[0], net.line.name.loc[0], net.bus.name.loc[2]]) expected_switch_names = np.append( net.line.name.loc[net.switch.element.loc[net.switch.et == "l"]].values, net.trafo.name.loc[net.switch.element.loc[net.switch.et == "t"]].values) pp.add_column_from_element_to_elements(net, "name", False) assert all( pp.compare_arrays(net.measurement.name.values, expected_measurement_names)) assert all(pp.compare_arrays(net.switch.name.values, orig_switch_names)) del net.measurement["name"] pp.add_column_from_element_to_elements(net, "name", True) assert all( pp.compare_arrays(net.measurement.name.values, expected_measurement_names)) assert all(pp.compare_arrays(net.switch.name.values, expected_switch_names))
def check_subnet_correctness(net, elements, branch_bus): for elm in elements: if "bus" in net[elm].columns: assert all(pp.compare_arrays(net[elm]["subnet"].values, np.array(["subnet_%i" % bus for bus in net[elm].bus]))) elif branch_bus[0] in net[elm].columns: assert all(pp.compare_arrays(net[elm]["subnet"].values, np.array([ "subnet_%i" % bus for bus in net[elm][branch_bus[0]]]))) elif branch_bus[1] in net[elm].columns: assert all(pp.compare_arrays(net[elm]["subnet"].values, np.array([ "subnet_%i" % bus for bus in net[elm][branch_bus[1]]])))
def test_column_indices(): df = pd.DataFrame([[0, 3, 2, 4, 5, 6]], columns=["b", "q", "a", "g", "f", "c"]) query_cols = ["b", "g", "f", "c", "q", "a", "q", "a", "f"] col_idx = sb.column_indices(df, query_cols) assert all( pp.compare_arrays(col_idx, np.array([0, 3, 4, 5, 1, 2, 1, 2, 4])))
def _get_unique_duplicated_dict(df, subset=None): """ Returns a dict which keys are the indices of unique row of the dataframe 'df'. The values of the dict are the indices which are duplicated to each key index. """ subset = subset or df.columns dupl = df.index[df.duplicated(subset=subset)] uniq = df.index[~df.duplicated(subset=subset)] uniq_dupl_dict = {} # nan_str only needed since compare_arrays() using old numpy versions connected to python 3.4 # don't detect reliably nans as equal nan_str = "nan" while nan_str in df.values: nan_str += "n" for uni in uniq: do_dupl_fit = compare_arrays( np.repeat(df.loc[uni, subset].fillna(nan_str).values.reshape(1, -1), len(dupl), axis=0), df.loc[dupl, subset].fillna(nan_str).values).all(axis=1) uniq_dupl_dict[uni] = list(dupl[do_dupl_fit]) return uniq_dupl_dict