Enhancement Proposal: Input of data using string - A Simplified Approach to Inputting Data into MESSAGEix without MS Excel #779
-
Dear Researchers, I present to you a Simplified Approach to Inputting Data into MESSAGEix without MS Excel. This improved method for inputting data into MESSAGEix offers a user-friendly tabular format, eliminating the need for MS Excel files and version management of codes and xlsx files separately. It utilizes comma-separated values (CSV) fed to a string, which is then merged with the
This method can be introduced in Westeros tutorials to help new users write and see data as well as code in single ipynb file. Major advantage over using This is my small and humble contribution in open-source community of MESSAGEix. Thank you for patient reading! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Thanks, @shreeyashn20! This is indeed a fluent use of pandas and Python features for data input. We do similar things in many places, for instance in message-ix-models. Here I'll give some refinements/alternate approaches to achieve the same task that your example does. To be clear, any code that achieves the intended result is fine! Different users may choose different approaches for their own reasons. First, we can use from io import StringIO
import pandas as pd
data_var_cost = """
node_loc technology mode time value unit
India coal standard year 5 $/kWh
India gas standard year 6 $/kWh
India oil standard year 7 $/kWh
"""
df_var_cost = pd.read_fwf(StringIO(data_var_cost)) Second, we can use the built-in from message_ix import make_df
tmp = make_df("var_cost", **df_var_cost)
…with Third, we can use the utility function year_df = scen.vintage_and_active_years() so for a self-contained example let's use: year_df = pd.DataFrame(
[[2020, 2020], [2020, 2025], [2025, 2025]],
columns=["year_vtg", "year_act"],
) We broadcast from message_ix_models.util import broadcast
df = tmp.pipe(broadcast, year_df)
Putting this all together: from io import StringIO
import pandas as pd
from message_ix import make_df
from message_ix_models.util import broadcast
# Name of the parameter for which to create data
name = "var_cost"
data = """
node_loc technology mode time value unit
India coal standard year 5 $/kWh
India gas standard year 6 $/kWh
India oil standard year 7 $/kWh
"""
data_years = """
year_vtg year_act
2020 2020
2020 2025
2025 2025
"""
df_base = pd.read_fwf(StringIO(data))
df_year = pd.read_fwf(StringIO(data_years))
df = make_df(name, **df_base).pipe(broadcast, df_year)
with scenario.transact(f"Add data for {name}"):
scenario.add_par(name, df) |
Beta Was this translation helpful? Give feedback.
Thanks, @shreeyashn20! This is indeed a fluent use of pandas and Python features for data input. We do similar things in many places, for instance in message-ix-models. Here I'll give some refinements/alternate approaches to achieve the same task that your example does. To be clear, any code that achieves the intended result is fine! Different users may choose different approaches for their own reasons.
First, we can use
pandas.read_fwf()
which allows an input string that is perhaps even more reader-friendly: