Skip to content

Commit a0974c3

Browse files
author
Samson Gebre
committed
Fix error message
1 parent a9f3130 commit a0974c3

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

‎src/PowerPlatform/Dataverse/utils/_pandas.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ def dataframe_to_records(df: pd.DataFrame, na_as_null: bool = False) -> List[Dic
4141
When True, missing values are included as None (sends null to Dataverse, clearing the field).
4242
:raises ValueError: If ``df`` has ``MultiIndex`` columns. Tuple column keys
4343
do not round-trip through Dataverse's JSON encoder and produce a
44-
confusing error far from the call site. Flatten first.
44+
confusing error far from the call site. Flatten to string column namesfirst.
4545
"""
4646
if isinstance(df.columns, pd.MultiIndex):
4747
raise ValueError(
48-
"MultiIndex columns are not supported. Flatten via " "df.columns = df.columns.to_flat_index() first."
48+
"MultiIndex columns are not supported. Flatten to string column names first, "
49+
"for example: df.columns = ['_'.join(map(str, col)) for col in df.columns.to_flat_index()]."
4950
)
5051
records = []
5152
for row in df.to_dict(orient="records"):

‎tests/unit/test_pandas_helpers.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,19 @@ def test_multiindex_columns_raises(self):
198198
df = pd.DataFrame([["x", "y"]], columns=cols)
199199
with self.assertRaises(ValueError) as ctx:
200200
dataframe_to_records(df)
201-
self.assertIn("MultiIndex", str(ctx.exception))
202-
self.assertIn("to_flat_index", str(ctx.exception))
201+
msg = str(ctx.exception)
202+
self.assertIn("MultiIndex", msg)
203+
# The remediation must produce string column names, not tuples.
204+
self.assertIn("'_'.join(map(str, col)) for col in df.columns.to_flat_index()", msg)
205+
206+
def test_multiindex_remediation_actually_works(self):
207+
"""The exact remediation in the error message must produce a
208+
DataFrame that dataframe_to_records can serialize."""
209+
cols = pd.MultiIndex.from_tuples([("a", "firstname"), ("a", "lastname")])
210+
df = pd.DataFrame([["x", "y"]], columns=cols)
211+
df.columns = ["_".join(map(str, col)) for col in df.columns.to_flat_index()]
212+
records = dataframe_to_records(df)
213+
self.assertEqual(records, [{"a_firstname": "x", "a_lastname": "y"}])
203214

204215
def test_mixed_types(self):
205216
"""DataFrame with mixed types (str, int, float, None, Timestamp) converts correctly."""

0 commit comments

Comments
 (0)