From 3e3001455a10dc715b5702a00b08c998d8abb6ab Mon Sep 17 00:00:00 2001 From: Fabian Neumann Date: Thu, 29 Apr 2021 17:11:10 +0200 Subject: [PATCH] address FutureWarning regarding set operations on pd.Index (#103) --- ...ustrial_energy_demand_per_country_today.py | 4 +-- ...build_industrial_production_per_country.py | 2 +- scripts/build_population_layouts.py | 2 +- scripts/build_retro_cost.py | 4 +-- scripts/make_summary.py | 36 +++++++++---------- scripts/plot_network.py | 6 ++-- scripts/plot_summary.py | 8 ++--- scripts/prepare_sector_network.py | 6 ++-- 8 files changed, 34 insertions(+), 34 deletions(-) diff --git a/scripts/build_industrial_energy_demand_per_country_today.py b/scripts/build_industrial_energy_demand_per_country_today.py index 7593477b..4de5d2c1 100644 --- a/scripts/build_industrial_energy_demand_per_country_today.py +++ b/scripts/build_industrial_energy_demand_per_country_today.py @@ -98,7 +98,7 @@ for ct in eu28: for fuel in fuels: summary.at[fuel,sub] = s[fuels[fuel]].sum() - summary.at['other',sub] = summary.at['all',sub] - summary.loc[summary.index^['all','other'],sub].sum() + summary.at['other',sub] = summary.at['all',sub] - summary.loc[summary.index.symmetric_difference(['all','other']),sub].sum() summary['Other Industrial Sectors'] = summary[ois_subs].sum(axis=1) summary.drop(columns=ois_subs,inplace=True) @@ -128,7 +128,7 @@ output = pd.read_csv(snakemake.input.industrial_production_per_country, eu28_averages = final_summary.groupby(level=1,axis=1).sum().divide(output.loc[eu28].sum(),axis=1) -non_eu28 = output.index^eu28 +non_eu28 = output.index.symmetric_difference(eu28) for ct in non_eu28: print(ct) diff --git a/scripts/build_industrial_production_per_country.py b/scripts/build_industrial_production_per_country.py index c8fa6910..eed59d29 100644 --- a/scripts/build_industrial_production_per_country.py +++ b/scripts/build_industrial_production_per_country.py @@ -196,7 +196,7 @@ ammonia = pd.read_csv(snakemake.input.ammonia_production, index_col=0) there = ammonia.index.intersection(countries_demand.index) -missing = countries_demand.index^there +missing = countries_demand.index.symmetric_difference(there) print("Following countries have no ammonia demand:", missing) diff --git a/scripts/build_population_layouts.py b/scripts/build_population_layouts.py index 8e9bb5ab..497e399a 100644 --- a/scripts/build_population_layouts.py +++ b/scripts/build_population_layouts.py @@ -46,7 +46,7 @@ urban_fraction = pd.read_csv(snakemake.input.urban_percent, #fill missing Balkans values missing = ["AL","ME","MK"] reference = ["RS","BA"] -urban_fraction = urban_fraction.reindex(urban_fraction.index|missing) +urban_fraction = urban_fraction.reindex(urban_fraction.index.union(missing)) urban_fraction.loc[missing] = urban_fraction[reference].mean() diff --git a/scripts/build_retro_cost.py b/scripts/build_retro_cost.py index 28610071..9b34507e 100644 --- a/scripts/build_retro_cost.py +++ b/scripts/build_retro_cost.py @@ -194,7 +194,7 @@ def prepare_building_stock_data(): area_per_pop = area_tot.unstack().reindex(index=ct_total.index).apply(lambda x: x / ct_total[x.index]) missing_area_ct = ct_total.index.difference(area_tot.index.levels[0]) - for ct in (missing_area_ct & ct_total.index): + for ct in missing_area_ct.intersection(ct_total.index): averaged_data = pd.DataFrame( area_per_pop.value.reindex(map_for_missings[ct]).mean() * ct_total[ct], @@ -343,7 +343,7 @@ def calculate_cost_energy_curve(u_values, l_strength, l_weight, average_surface_ res = res.reset_index().set_index(["country", "sector"]) # map missing countries - for ct in pd.Index(map_for_missings.keys()) & countries: + for ct in pd.Index(map_for_missings.keys()).intersection(countries): averaged_data = res.reindex(index=map_for_missings[ct], level=0).mean(level=1) index = pd.MultiIndex.from_product([[ct], averaged_data.index.to_list()]) averaged_data.index = index diff --git a/scripts/make_summary.py b/scripts/make_summary.py index 43f64b2c..1b12ee6d 100644 --- a/scripts/make_summary.py +++ b/scripts/make_summary.py @@ -79,7 +79,7 @@ def calculate_nodal_cfs(n,label,nodal_cfs): cf_c = p_c/capacities_c index = pd.MultiIndex.from_tuples([(c.list_name,) + t for t in cf_c.index.to_list()]) - nodal_cfs = nodal_cfs.reindex(index|nodal_cfs.index) + nodal_cfs = nodal_cfs.reindex(index.union(nodal_cfs.index)) nodal_cfs.loc[index,label] = cf_c.values return nodal_cfs @@ -106,7 +106,7 @@ def calculate_cfs(n,label,cfs): cf_c = pd.concat([cf_c], keys=[c.list_name]) - cfs = cfs.reindex(cf_c.index|cfs.index) + cfs = cfs.reindex(cf_c.index.union(cfs.index)) cfs.loc[cf_c.index,label] = cf_c @@ -121,7 +121,7 @@ def calculate_nodal_costs(n,label,nodal_costs): c.df["capital_costs"] = c.df.capital_cost*c.df[opt_name.get(c.name,"p") + "_nom_opt"] capital_costs = c.df.groupby(["location","carrier"])["capital_costs"].sum() index = pd.MultiIndex.from_tuples([(c.list_name,"capital") + t for t in capital_costs.index.to_list()]) - nodal_costs = nodal_costs.reindex(index|nodal_costs.index) + nodal_costs = nodal_costs.reindex(index.union(nodal_costs.index)) nodal_costs.loc[index,label] = capital_costs.values if c.name == "Link": @@ -143,7 +143,7 @@ def calculate_nodal_costs(n,label,nodal_costs): c.df["marginal_costs"] = p*c.df.marginal_cost marginal_costs = c.df.groupby(["location","carrier"])["marginal_costs"].sum() index = pd.MultiIndex.from_tuples([(c.list_name,"marginal") + t for t in marginal_costs.index.to_list()]) - nodal_costs = nodal_costs.reindex(index|nodal_costs.index) + nodal_costs = nodal_costs.reindex(index.union(nodal_costs.index)) nodal_costs.loc[index,label] = marginal_costs.values return nodal_costs @@ -158,7 +158,7 @@ def calculate_costs(n,label,costs): capital_costs_grouped = pd.concat([capital_costs_grouped], keys=["capital"]) capital_costs_grouped = pd.concat([capital_costs_grouped], keys=[c.list_name]) - costs = costs.reindex(capital_costs_grouped.index|costs.index) + costs = costs.reindex(capital_costs_grouped.index.union(costs.index)) costs.loc[capital_costs_grouped.index,label] = capital_costs_grouped @@ -185,7 +185,7 @@ def calculate_costs(n,label,costs): marginal_costs_grouped = pd.concat([marginal_costs_grouped], keys=["marginal"]) marginal_costs_grouped = pd.concat([marginal_costs_grouped], keys=[c.list_name]) - costs = costs.reindex(marginal_costs_grouped.index|costs.index) + costs = costs.reindex(marginal_costs_grouped.index.union(costs.index)) costs.loc[marginal_costs_grouped.index,label] = marginal_costs_grouped @@ -220,7 +220,7 @@ def calculate_nodal_capacities(n,label,nodal_capacities): for c in n.iterate_components(n.branch_components|n.controllable_one_port_components^{"Load"}): nodal_capacities_c = c.df.groupby(["location","carrier"])[opt_name.get(c.name,"p") + "_nom_opt"].sum() index = pd.MultiIndex.from_tuples([(c.list_name,) + t for t in nodal_capacities_c.index.to_list()]) - nodal_capacities = nodal_capacities.reindex(index|nodal_capacities.index) + nodal_capacities = nodal_capacities.reindex(index.union(nodal_capacities.index)) nodal_capacities.loc[index,label] = nodal_capacities_c.values return nodal_capacities @@ -234,7 +234,7 @@ def calculate_capacities(n,label,capacities): capacities_grouped = c.df[opt_name.get(c.name,"p") + "_nom_opt"].groupby(c.df.carrier).sum() capacities_grouped = pd.concat([capacities_grouped], keys=[c.list_name]) - capacities = capacities.reindex(capacities_grouped.index|capacities.index) + capacities = capacities.reindex(capacities_grouped.index.union(capacities.index)) capacities.loc[capacities_grouped.index,label] = capacities_grouped @@ -267,7 +267,7 @@ def calculate_energy(n,label,energy): c_energies = pd.concat([c_energies], keys=[c.list_name]) - energy = energy.reindex(c_energies.index|energy.index) + energy = energy.reindex(c_energies.index.union(energy.index)) energy.loc[c_energies.index,label] = c_energies @@ -294,7 +294,7 @@ def calculate_supply(n,label,supply): s = pd.concat([s], keys=[c.list_name]) s = pd.concat([s], keys=[i]) - supply = supply.reindex(s.index|supply.index) + supply = supply.reindex(s.index.union(supply.index)) supply.loc[s.index,label] = s @@ -313,7 +313,7 @@ def calculate_supply(n,label,supply): s = pd.concat([s], keys=[c.list_name]) s = pd.concat([s], keys=[i]) - supply = supply.reindex(s.index|supply.index) + supply = supply.reindex(s.index.union(supply.index)) supply.loc[s.index,label] = s return supply @@ -339,7 +339,7 @@ def calculate_supply_energy(n,label,supply_energy): s = pd.concat([s], keys=[c.list_name]) s = pd.concat([s], keys=[i]) - supply_energy = supply_energy.reindex(s.index|supply_energy.index) + supply_energy = supply_energy.reindex(s.index.union(supply_energy.index)) supply_energy.loc[s.index,label] = s @@ -357,7 +357,7 @@ def calculate_supply_energy(n,label,supply_energy): s = pd.concat([s], keys=[c.list_name]) s = pd.concat([s], keys=[i]) - supply_energy = supply_energy.reindex(s.index|supply_energy.index) + supply_energy = supply_energy.reindex(s.index.union(supply_energy.index)) supply_energy.loc[s.index,label] = s @@ -366,7 +366,7 @@ def calculate_supply_energy(n,label,supply_energy): def calculate_metrics(n,label,metrics): - metrics = metrics.reindex(pd.Index(["line_volume","line_volume_limit","line_volume_AC","line_volume_DC","line_volume_shadow","co2_shadow"])|metrics.index) + metrics = metrics.reindex(pd.Index(["line_volume","line_volume_limit","line_volume_AC","line_volume_DC","line_volume_shadow","co2_shadow"]).union(metrics.index)) metrics.at["line_volume_DC",label] = (n.links.length*n.links.p_nom_opt)[n.links.carrier == "DC"].sum() metrics.at["line_volume_AC",label] = (n.lines.length*n.lines.s_nom_opt).sum() @@ -384,7 +384,7 @@ def calculate_metrics(n,label,metrics): def calculate_prices(n,label,prices): - prices = prices.reindex(prices.index|n.buses.carrier.unique()) + prices = prices.reindex(prices.index.union(n.buses.carrier.unique())) #WARNING: this is time-averaged, see weighted_prices for load-weighted average prices[label] = n.buses_t.marginal_price.mean().groupby(n.buses.carrier).mean() @@ -467,7 +467,7 @@ def calculate_market_values(n, label, market_values): techs = n.generators.loc[generators,"carrier"].value_counts().index - market_values = market_values.reindex(market_values.index | techs) + market_values = market_values.reindex(market_values.index.union(techs)) for tech in techs: @@ -488,7 +488,7 @@ def calculate_market_values(n, label, market_values): techs = n.links.loc[all_links,"carrier"].value_counts().index - market_values = market_values.reindex(market_values.index | techs) + market_values = market_values.reindex(market_values.index.union(techs)) for tech in techs: links = all_links[n.links.loc[all_links,"carrier"] == tech] @@ -505,7 +505,7 @@ def calculate_market_values(n, label, market_values): def calculate_price_statistics(n, label, price_statistics): - price_statistics = price_statistics.reindex(price_statistics.index|pd.Index(["zero_hours","mean","standard_deviation"])) + price_statistics = price_statistics.reindex(price_statistics.index.union(pd.Index(["zero_hours","mean","standard_deviation"]))) buses = n.buses.index[n.buses.carrier == "AC"] diff --git a/scripts/plot_network.py b/scripts/plot_network.py index cd419808..2aaef6bc 100644 --- a/scripts/plot_network.py +++ b/scripts/plot_network.py @@ -130,7 +130,7 @@ def plot_map(network, components=["links", "stores", "storage_units", "generator costs.drop(list(costs.columns[(costs == 0.).all()]), axis=1, inplace=True) - new_columns = ((preferred_order & costs.columns) + new_columns = (preferred_order.intersection(costs.columns) .append(costs.columns.difference(preferred_order))) costs = costs[new_columns] @@ -147,7 +147,7 @@ def plot_map(network, components=["links", "stores", "storage_units", "generator n.links.carrier != "B2B")], inplace=True) # drop non-bus - to_drop = costs.index.levels[0] ^ n.buses.index + to_drop = costs.index.levels[0].symmetric_difference(n.buses.index) if len(to_drop) != 0: print("dropping non-buses", to_drop) costs.drop(to_drop, level=0, inplace=True, axis=0) @@ -463,7 +463,7 @@ def plot_series(network, carrier="AC", name="test"): "battery storage", "hot water storage"]) - new_columns = ((preferred_order & supply.columns) + new_columns = (preferred_order.intersection(supply.columns) .append(supply.columns.difference(preferred_order))) supply = supply.groupby(supply.columns, axis=1).sum() diff --git a/scripts/plot_summary.py b/scripts/plot_summary.py index fe28cfed..bc852e70 100644 --- a/scripts/plot_summary.py +++ b/scripts/plot_summary.py @@ -82,7 +82,7 @@ def plot_costs(): print(df.sum()) - new_index = (preferred_order&df.index).append(df.index.difference(preferred_order)) + new_index = preferred_order.intersection(df.index).append(df.index.difference(preferred_order)) new_columns = df.sum().sort_values().index @@ -136,7 +136,7 @@ def plot_energy(): print(df) - new_index = (preferred_order&df.index).append(df.index.difference(preferred_order)) + new_index = preferred_order.intersection(df.index).append(df.index.difference(preferred_order)) new_columns = df.columns.sort_values() #new_columns = df.sum().sort_values().index @@ -177,7 +177,7 @@ def plot_balances(): balances_df = pd.read_csv(snakemake.input.balances,index_col=list(range(3)),header=list(range(n_header))) balances = {i.replace(" ","_") : [i] for i in balances_df.index.levels[0]} - balances["energy"] = balances_df.index.levels[0]^co2_carriers + balances["energy"] = balances_df.index.levels[0].symmetric_difference(co2_carriers) for k,v in balances.items(): @@ -205,7 +205,7 @@ def plot_balances(): if df.empty: continue - new_index = (preferred_order&df.index).append(df.index.difference(preferred_order)) + new_index = preferred_order.intersection(df.index).append(df.index.difference(preferred_order)) new_columns = df.columns.sort_values() diff --git a/scripts/prepare_sector_network.py b/scripts/prepare_sector_network.py index abf3ce37..185ca06e 100644 --- a/scripts/prepare_sector_network.py +++ b/scripts/prepare_sector_network.py @@ -240,7 +240,7 @@ def remove_elec_base_techs(n): for c in n.iterate_components(snakemake.config["pypsa_eur"]): to_keep = snakemake.config["pypsa_eur"][c.name] - to_remove = pd.Index(c.df.carrier.unique())^to_keep + to_remove = pd.Index(c.df.carrier.unique()).symmetric_difference(to_keep) print("Removing",c.list_name,"with carrier",to_remove) names = c.df.index[c.df.carrier.isin(to_remove)] print(names) @@ -921,7 +921,7 @@ def add_storage(network): # hydrogen stored overground h2_capital_cost = costs.at["hydrogen storage tank", "fixed"] - nodes_overground = nodes ^ cavern_nodes.index + nodes_overground = nodes.symmetric_difference(cavern_nodes.index) network.madd("Store", nodes_overground + " H2 Store", @@ -1484,7 +1484,7 @@ def create_nodes_for_heat_sector(): else: nodes[sector + " urban decentral"] = pop_layout.index # for central nodes, residential and services are aggregated - nodes["urban central"] = pop_layout.index ^ nodes["residential urban decentral"] + nodes["urban central"] = pop_layout.index.symmetric_difference(nodes["residential urban decentral"]) return nodes