Skip to content

Commit

Permalink
Merge pull request #148 from PyPSA/refactor-inplace-operation
Browse files Browse the repository at this point in the history
replace pandas inplace operation by assignment
  • Loading branch information
FabianHofmann authored Jan 29, 2024
2 parents 141da8b + 518bfbe commit 15a6159
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions powerplantmatching/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def to_year(ds):
ppl = clean_name(ppl)

res = units.join(ppl.set_index("projectID"), "projectID", rsuffix="_ppl")
res.DateIn.fillna(res.DateIn_ppl, inplace=True)
res["DateIn"] = res.DateIn.fillna(res.DateIn_ppl)
not_included_ppl = ppl.query("projectID not in @res.projectID")
res = pd.concat([res, not_included_ppl]).pipe(set_column_name, "GEO")
res = scale_to_net_capacities(res)
Expand Down Expand Up @@ -1232,7 +1232,7 @@ def UBA(
"\xd6lr\xfcckstand": "Oil",
}
)
uba.Name.replace([r"(?i)oe", r"(?i)ue"], ["ö", "ü"], regex=True, inplace=True)
uba["Name"] = uba.Name.replace([r"(?i)oe", r"(?i)ue"], ["ö", "ü"], regex=True)
if prune_wind:
uba = uba.loc[lambda x: x.Fueltype != "Wind"]
if prune_solar:
Expand Down Expand Up @@ -1574,7 +1574,7 @@ def IRENASTAT(raw=False, update=False, config=None):
}

df["Fueltype"] = df.Technology.map(fueltype_dict)
df.Technology.replace(technology_dict, inplace=True)
df["Technology"] = df.Technology.replace(technology_dict)

l = list(set(df.columns).difference(set(["Capacity"])))
df = df.groupby(l, as_index=False, dropna=True).sum()
Expand Down
16 changes: 9 additions & 7 deletions powerplantmatching/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ def fill_missing_commissioning_years(df):
df = get_obj_if_Acc(df)
df = df.copy()
# 1st try: Fill with both country- and fueltypespecific averages
df.DateIn.fillna(
df.groupby(["Country", "Fueltype"]).DateIn.transform("mean"), inplace=True
df["DateIn"] = df.DateIn.fillna(
df.groupby(["Country", "Fueltype"]).DateIn.transform("mean")
)
# 2nd try: Fill remaining with only fueltype-specific average
df.DateIn.fillna(df.groupby(["Fueltype"]).DateIn.transform("mean"), inplace=True)
df["DateIn"] = df.DateIn.fillna(df.groupby(["Fueltype"]).DateIn.transform("mean"))
# 3rd try: Fill remaining with only country-specific average
df.DateIn.fillna(df.groupby(["Country"]).DateIn.transform("mean"), inplace=True)
df["DateIn"] = df.DateIn.fillna(df.groupby(["Country"]).DateIn.transform("mean"))
if df.DateIn.isnull().any():
count = len(df[df.DateIn.isnull()])
logger.warn(
Expand All @@ -251,7 +251,7 @@ def fill_missing_commissioning_years(df):
)
)
df["DateIn"] = df.DateIn.astype(float)
df.DateRetrofit.fillna(df.DateIn, inplace=True)
df["DateRetrofit"] = df.DateRetrofit.fillna(df.DateIn)
return df


Expand Down Expand Up @@ -319,7 +319,7 @@ def wm(x):
target_fueltypes = ["Wind", "Solar", "Bioenergy"]
df = df[df.Fueltype.isin(target_fueltypes)]
df = fill_missing_commissioning_years(df)
df.Technology.fillna("-", inplace=True)
df["Technology"] = df.Technology.fillna("-")
df = (
df.groupby(["Country", "DateIn", "Fueltype", "Technology"])
.agg(f)
Expand Down Expand Up @@ -539,7 +539,9 @@ def gross_to_net_factors(reference="opsd", aggfunc="median", return_entire_data=
if return_entire_data:
return df
else:
df.energy_source_level_2.fillna(value=df.energy_source, inplace=True)
df["energy_source_level_2"] = df.energy_source_level_2.fillna(
value=df.energy_source
)
df.replace(
dict(
energy_source_level_2={
Expand Down

0 comments on commit 15a6159

Please sign in to comment.