Skip to content

Commit

Permalink
datacollector: Add warning when returning empty dataframe with no rep…
Browse files Browse the repository at this point in the history
…orters defined

With this commit the get_model_vars_dataframe() and get_agent_vars_dataframe() raise warnings when no model_reporters or agent_reporteres are defined, and it thus returns an empty dataframe. This warning makes it clearer why it's returning an empty dataframe.

Two of my students were stuck on this quite a while, since they were requesting the get_model_vars_dataframe() while having defined only agent reporters.
  • Loading branch information
EwoutH authored and rht committed Mar 15, 2023
1 parent 9586328 commit 2e85667
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mesa/datacollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ def get_model_vars_dataframe(self):
The DataFrame has one column for each model variable, and the index is
(implicitly) the model tick.
"""
# Check if self.model_reporters dictionary is empty, if so raise warning
if not self.model_reporters:
raise UserWarning(
"No model reporters have been defined in the DataCollector, returning empty DataFrame."
)

return pd.DataFrame(self.model_vars)

def get_agent_vars_dataframe(self):
Expand All @@ -224,6 +230,12 @@ def get_agent_vars_dataframe(self):
The DataFrame has one column for each variable, with two additional
columns for tick and agent_id.
"""
# Check if self.agent_reporters dictionary is empty, if so raise warning
if not self.agent_reporters:
raise UserWarning(
"No agent reporters have been defined in the DataCollector, returning empty DataFrame."
)

all_records = itertools.chain.from_iterable(self._agent_records.values())
rep_names = list(self.agent_reporters)

Expand Down

0 comments on commit 2e85667

Please sign in to comment.