From c8c263e1b71e21ca868607d5428f31b8b09b4b2b Mon Sep 17 00:00:00 2001 From: Forest Savage <96553407+forest-savage1234@users.noreply.github.com> Date: Sun, 30 Aug 2026 22:58:24 -0800 Subject: [PATCH] Use keyword-only orient with pandas DataFrame.to_dict --- pm4py/util/pandas_utils.py | 4 ++-- tests/pandas_utils_test.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/pandas_utils_test.py diff --git a/pm4py/util/pandas_utils.py b/pm4py/util/pandas_utils.py index d5a402a19..9ef5eee45 100644 --- a/pm4py/util/pandas_utils.py +++ b/pm4py/util/pandas_utils.py @@ -60,7 +60,7 @@ def to_dict_records(df): if is_polars_lazyframe(df): return df.collect().to_dicts() - return df.to_dict("records") + return df.to_dict(orient="records") def to_dict_index(df): @@ -84,7 +84,7 @@ def to_dict_index(df): for idx, row in enumerate(collected_df.iter_rows(named=True)) } - return df.to_dict("index") + return df.to_dict(orient="index") def insert_index( diff --git a/tests/pandas_utils_test.py b/tests/pandas_utils_test.py new file mode 100644 index 000000000..8eea2c6ff --- /dev/null +++ b/tests/pandas_utils_test.py @@ -0,0 +1,33 @@ +import unittest + +import pandas as pd + +from pm4py.util import pandas_utils + + +class PandasUtilsTest(unittest.TestCase): + def test_to_dict_uses_keyword_only_orient(self): + """Tests that pandas to_dict is called with a keyword-only orient.""" + + class KeywordOnlyDataFrame: + def to_dict(self, *, orient): + return orient + + dataframe = KeywordOnlyDataFrame() + + self.assertEqual(pandas_utils.to_dict_records(dataframe), "records") + self.assertEqual(pandas_utils.to_dict_index(dataframe), "index") + + dataframe = pd.DataFrame([{"case": "A", "event": "start"}]) + self.assertEqual( + pandas_utils.to_dict_records(dataframe), + [{"case": "A", "event": "start"}], + ) + self.assertEqual( + pandas_utils.to_dict_index(dataframe), + {0: {"case": "A", "event": "start"}}, + ) + + +if __name__ == "__main__": + unittest.main()