Skip to content

Commit

Permalink
more robust conversion to pyarrow
Browse files Browse the repository at this point in the history
  • Loading branch information
jpn-- committed Mar 28, 2024
1 parent 8ed8fb9 commit 50c9f6d
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion activitysim/core/workflow/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,18 @@ def get_pyarrow(
if t is None:
raise KeyError(tablename)
if isinstance(t, pd.DataFrame):
t = pa.Table.from_pandas(t, preserve_index=True, columns=columns)
df = t
try:
t = pa.Table.from_pandas(df, preserve_index=True, columns=columns)
except (pa.ArrowTypeError, pa.ArrowInvalid):
# if there are object columns, try to convert them to categories
df = df.copy()
for k, dtype in df.dtypes.items():
if dtype.kind == "O":
df[k] = df[k].astype("str")
elif dtype == "boolean":
df[k] = df[k].astype("str")
t = pa.Table.from_pandas(df, preserve_index=True, columns=columns)
if isinstance(t, pa.Table):
if columns is not None:
t = t.select(columns)
Expand Down

0 comments on commit 50c9f6d

Please sign in to comment.