Skip to content

Commit 6893e65

Browse files
committed
code: replace NoneType with empty DataFrame
1 parent b3fc382 commit 6893e65

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

scripts/compile_cost_assumptions.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,12 @@ def get_sheet_location(
325325
return key_list[0]
326326
elif len(key_list) > 1:
327327
logger.info(f"{tech_name} appears in more than one sheet name")
328-
return None
328+
return "Multiple sheets found"
329329
else:
330330
logger.info(
331331
f"tech {tech_name} with sheet name {sheet_names_dict[tech_name]} not found in excel sheets. "
332332
)
333-
return None
333+
return "Sheet not found"
334334

335335

336336
def get_dea_maritime_data(
@@ -582,6 +582,7 @@ def get_dea_vehicle_data(
582582
def get_data_DEA(
583583
years: list,
584584
tech_name: str,
585+
sheet_names_dict: dict,
585586
input_data_dict: dict,
586587
offwind_no_grid_costs_flag: bool = True,
587588
expectation: str = None,
@@ -596,6 +597,8 @@ def get_data_DEA(
596597
years for which a cost assumption is provided
597598
tech_name : str
598599
technology name
600+
sheet_names_dict : dict
601+
dictionary having the technology name as keys and Excel sheet names as values
599602
input_data_dict : dict
600603
dictionary where the keys are the path to the DEA inputs and the values are the sheet names
601604
offwind_no_grid_costs_flag : bool
@@ -609,10 +612,10 @@ def get_data_DEA(
609612
technology data from DEA
610613
"""
611614

612-
excel_file = get_sheet_location(tech_name, dea_sheet_names, input_data_dict)
613-
if excel_file is None:
615+
excel_file = get_sheet_location(tech_name, sheet_names_dict, input_data_dict)
616+
if excel_file == "Sheet not found" or excel_file == "Multiple sheets found":
614617
logger.info(f"excel file not found for technology: {tech_name}")
615-
return None
618+
return pd.DataFrame()
616619

617620
if tech_name == "battery":
618621
usecols = "B:J"
@@ -655,7 +658,7 @@ def get_data_DEA(
655658

656659
excel = pd.read_excel(
657660
excel_file,
658-
sheet_name=dea_sheet_names[tech_name],
661+
sheet_name=sheet_names_dict[tech_name],
659662
index_col=0,
660663
usecols=usecols,
661664
skiprows=skiprows,
@@ -1380,14 +1383,12 @@ def biochar_pyrolysis_harmonise_dea(df: pd.DataFrame) -> pd.DataFrame:
13801383
inplace=True,
13811384
)
13821385

1383-
# df = df.astype(float)
1384-
# df = df.mask(df.apply(pd.to_numeric, errors='coerce').isna(), df.astype(str).apply(lambda x: x.str.strip()))
1385-
13861386
return df
13871387

13881388

13891389
def get_data_from_DEA(
13901390
years: list,
1391+
sheet_names_dict: dict,
13911392
input_data_dictionary: dict,
13921393
offwind_no_grid_costs: bool = True,
13931394
expectation: str = None,
@@ -1399,6 +1400,8 @@ def get_data_from_DEA(
13991400
----------
14001401
years : list
14011402
years for which a cost assumption is provided
1403+
sheet_names_dict : dict
1404+
dictionary having the technology name as keys and Excel sheet names as values
14021405
input_data_dictionary : dict
14031406
dictionary where the keys are the path to the DEA inputs and the values are the sheet names
14041407
offwind_no_grid_costs : bool
@@ -1414,11 +1417,12 @@ def get_data_from_DEA(
14141417

14151418
data_by_tech_dict = {}
14161419

1417-
for tech_name, dea_tech in dea_sheet_names.items():
1420+
for tech_name, dea_tech in sheet_names_dict.items():
14181421
logger.info(f"{tech_name} in PyPSA corresponds to {dea_tech} in DEA database.")
14191422
df = get_data_DEA(
14201423
years,
14211424
tech_name,
1425+
sheet_names_dict,
14221426
input_data_dictionary,
14231427
offwind_no_grid_costs,
14241428
expectation,
@@ -3957,6 +3961,7 @@ def prepare_inflation_rate(fn: str) -> pd.DataFrame:
39573961
# create dictionary with raw data from DEA sheets
39583962
d_by_tech = get_data_from_DEA(
39593963
years_list,
3964+
dea_sheet_names,
39603965
data_in,
39613966
snakemake.config["offwind_no_gridcosts"],
39623967
expectation=snakemake.config["expectation"],

test/test_compile_cost_assumptions.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_get_sheet_location():
160160
"waste CHP CC": "inputs/technology_data_for_el_and_dh.xlsx",
161161
"biochar pyrolysis": "inputs/data_sheets_for_renewable_fuels.xlsx",
162162
"electrolysis small": "inputs/data_sheets_for_renewable_fuels.xlsx",
163-
"random tech": None,
163+
"random tech": "Sheet not found",
164164
}
165165
dea_sheet_names_dict = copy.deepcopy(dea_sheet_names)
166166
dea_sheet_names_dict["random tech"] = "random sheet"
@@ -250,13 +250,16 @@ def test_get_data_from_dea(config):
250250
"waste CHP CC": (16, 9),
251251
"biochar pyrolysis": (7, 9),
252252
"electrolysis small": (7, 9),
253+
"random tech": (0, 0),
253254
}
254255
excel_files = [
255256
v for k, v in snakemake_input_dictionary.items() if "dea" in k.casefold()
256257
]
257258
input_dea_files_dict = get_excel_sheets(excel_files)
259+
dea_sheet_names_dict = copy.deepcopy(dea_sheet_names)
260+
dea_sheet_names_dict["random tech"] = "random sheet"
258261
output_dictionary = get_data_from_DEA(
259-
config["years"], input_dea_files_dict, expectation=config["expectation"]
262+
config["years"], dea_sheet_names_dict, input_dea_files_dict, expectation=config["expectation"]
260263
)
261264
comparison_dictionary = {}
262265
for key, value in output_dictionary.items():

0 commit comments

Comments
 (0)