Skip to content

Commit

Permalink
Use .loc[:, c] to assign to DataFrame columns
Browse files Browse the repository at this point in the history
Instead of `[c]`. This hopefully squelches a few instances of

  ```
  SettingWithCopyWarning:
  A value is trying to be set on a copy of a slice from a DataFrame.
  Try using .loc[row_indexer,col_indexer] = value instead

  See the caveats in the documentation: https://pandas.pydata.org/pandas-doc#
  ```

It should also make sure that the assignment always happens and isn't
lost in an ephemeral copy.
  • Loading branch information
gnn committed Feb 16, 2023
1 parent 87d679a commit 70298a1
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/egon/data/datasets/pypsaeursec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def neighbor_reduction():
network.buses.country = network.buses.index.str[:2]
neighbors = network.buses[network.buses.country != "DE"]

neighbors["new_index"] = (
neighbors.loc[:, "new_index"] = (
db.next_etrago_id("bus") + neighbors.reset_index().index
)

Expand Down Expand Up @@ -554,7 +554,7 @@ def neighbor_reduction():
# Connect to local database
engine = db.engine()

neighbors["scn_name"] = "eGon100RE"
neighbors.loc[:, "scn_name"] = "eGon100RE"
neighbors.index = neighbors["new_index"]

# Correct geometry for non AC buses
Expand Down Expand Up @@ -609,11 +609,11 @@ def neighbor_reduction():

# prepare and write neighboring crossborder lines to etrago tables
def lines_to_etrago(neighbor_lines=neighbor_lines, scn="eGon100RE"):
neighbor_lines["scn_name"] = scn
neighbor_lines["cables"] = 3 * neighbor_lines["num_parallel"].astype(
int
)
neighbor_lines["s_nom"] = neighbor_lines["s_nom_min"]
neighbor_lines.loc[:, "scn_name"] = scn
neighbor_lines.loc[:, "cables"] = 3 * neighbor_lines[
"num_parallel"
].astype(int)
neighbor_lines.loc[:, "s_nom"] = neighbor_lines["s_nom_min"]

for i in [
"name",
Expand Down Expand Up @@ -689,7 +689,7 @@ def links_to_etrago(neighbor_links, scn="eGon100RE", extendable=True):
None
"""
neighbor_links["scn_name"] = scn
neighbor_links.loc[:, "scn_name"] = scn

if extendable is True:
neighbor_links = neighbor_links.drop(
Expand Down Expand Up @@ -808,9 +808,9 @@ def links_to_etrago(neighbor_links, scn="eGon100RE", extendable=True):
links_to_etrago(neighbor_links[neighbor_links.carrier == "DC"], "eGon2035")

# prepare neighboring generators for etrago tables
neighbor_gens["scn_name"] = "eGon100RE"
neighbor_gens["p_nom"] = neighbor_gens["p_nom_opt"]
neighbor_gens["p_nom_extendable"] = False
neighbor_gens.loc[:, "scn_name"] = "eGon100RE"
neighbor_gens.loc[:, "p_nom"] = neighbor_gens["p_nom_opt"]
neighbor_gens.loc[:, "p_nom_extendable"] = False

# Unify carrier names
neighbor_gens.carrier = neighbor_gens.carrier.str.replace(" ", "_")
Expand Down Expand Up @@ -847,7 +847,7 @@ def links_to_etrago(neighbor_links, scn="eGon100RE", extendable=True):
)

# prepare neighboring loads for etrago tables
neighbor_loads["scn_name"] = "eGon100RE"
neighbor_loads.loc[:, "scn_name"] = "eGon100RE"

# Unify carrier names
neighbor_loads.carrier = neighbor_loads.carrier.str.replace(" ", "_")
Expand Down Expand Up @@ -879,7 +879,7 @@ def links_to_etrago(neighbor_links, scn="eGon100RE", extendable=True):
)

# prepare neighboring stores for etrago tables
neighbor_stores["scn_name"] = "eGon100RE"
neighbor_stores.loc[:, "scn_name"] = "eGon100RE"

# Unify carrier names
neighbor_stores.carrier = neighbor_stores.carrier.str.replace(" ", "_")
Expand Down Expand Up @@ -919,7 +919,7 @@ def links_to_etrago(neighbor_links, scn="eGon100RE", extendable=True):
)

# prepare neighboring storage_units for etrago tables
neighbor_storage["scn_name"] = "eGon100RE"
neighbor_storage.loc[:, "scn_name"] = "eGon100RE"

# Unify carrier names
neighbor_storage.carrier = neighbor_storage.carrier.str.replace(" ", "_")
Expand Down

0 comments on commit 70298a1

Please sign in to comment.