Skip to content

Commit

Permalink
fix: df_constructor when data contains DF/SRS
Browse files Browse the repository at this point in the history
  • Loading branch information
adamamer20 committed Aug 25, 2024
1 parent c8a11fe commit bb12591
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mesa_frames/concrete/pandas/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,17 @@ def _df_constructor(
elif isinstance(data, pl.DataFrame):
df = data.to_pandas()
else:
df = pd.DataFrame(data=data, columns=columns, index=index)
# We need to try setting the index after,
# otherwise if data contains DF/SRS, the values will not be aligned to the index
try:
df = pd.DataFrame(data=data, columns=columns)
if index is not None:
df.index = index
except ValueError as e:
if str(e) == "If using all scalar values, you must pass an index":
df = pd.DataFrame(data=data, columns=columns, index=index)
else:
raise e
if dtypes:
df = df.astype(dtypes)
if index_cols:
Expand Down

0 comments on commit bb12591

Please sign in to comment.