Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate stable vehicle type dtype categories #807

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions activitysim/abm/models/vehicle_type_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,18 @@ def iterate_vehicle_type_choice(
# convert alternative names to categoricals
# body_type, fuel_type, vehicle_type
# age should be a int becuase it is used as a numeric value in utilities

# although the next three categorical types are not fundamentally "ordered",
# they are defined to be lexicographically sorted to ensure that sharrow
# recognizes them as a stable category dtype when compiling the model
body_type_cat = pd.api.types.CategoricalDtype(
alts_cats_dict["body_type"], ordered=False
sorted(alts_cats_dict["body_type"]), ordered=False
)
fuel_type_cat = pd.api.types.CategoricalDtype(
alts_cats_dict["fuel_type"], ordered=False
sorted(alts_cats_dict["fuel_type"]), ordered=False
)
vehicle_type_cat = pd.api.types.CategoricalDtype(
list(set(alts_wide["vehicle_type"])) + [""], ordered=False
[""] + sorted(set(alts_wide["vehicle_type"])), ordered=False
)

alts_wide["body_type"] = alts_wide["body_type"].astype(body_type_cat)
Expand All @@ -399,7 +403,7 @@ def iterate_vehicle_type_choice(
alts_wide = alts_long = None
alts = model_spec.columns
vehicle_type_cat = pd.api.types.CategoricalDtype(
list(set(alts)) + [""], ordered=False
[""] + sorted(set(alts)), ordered=False
)

# - preparing choosers for iterating
Expand Down
Loading