From 2e856679f41043b19a3376794e4c48c17c5c17a1 Mon Sep 17 00:00:00 2001 From: Ewout ter Hoeven Date: Tue, 14 Mar 2023 16:35:54 +0100 Subject: [PATCH] datacollector: Add warning when returning empty dataframe with no reporters 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. --- mesa/datacollection.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/mesa/datacollection.py b/mesa/datacollection.py index dd9eb375cf5..fcaedc7c8c8 100644 --- a/mesa/datacollection.py +++ b/mesa/datacollection.py @@ -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): @@ -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)