diff --git a/activitysim/abm/models/joint_tour_frequency.py b/activitysim/abm/models/joint_tour_frequency.py index cace98d7d..d2dc67bc8 100644 --- a/activitysim/abm/models/joint_tour_frequency.py +++ b/activitysim/abm/models/joint_tour_frequency.py @@ -137,7 +137,15 @@ def joint_tour_frequency( # - but we don't know the tour participants yet # - so we arbitrarily choose the first person in the household # - to be point person for the purpose of generating an index and setting origin - temp_point_persons = persons.loc[persons.PNUM == 1] + if "PNUM" in persons.columns: + temp_point_persons = persons.loc[persons.PNUM == 1] + else: + # if PNUM is not available, we can still get the first person in the household + temp_point_persons = ( + persons.sort_index() # ensure stable ordering + .groupby("household_id", as_index=False) + .first() + ) temp_point_persons["person_id"] = temp_point_persons.index temp_point_persons = temp_point_persons.set_index("household_id") temp_point_persons = temp_point_persons[["person_id", "home_zone_id"]] diff --git a/activitysim/abm/models/joint_tour_participation.py b/activitysim/abm/models/joint_tour_participation.py index 47bc2b8ff..2e5ec618d 100644 --- a/activitysim/abm/models/joint_tour_participation.py +++ b/activitysim/abm/models/joint_tour_participation.py @@ -451,14 +451,24 @@ def joint_tour_participation( PARTICIPANT_COLS = ["tour_id", "household_id", "person_id"] participants = candidates[participate][PARTICIPANT_COLS].copy() - # assign participant_num - # FIXME do we want something smarter than the participant with the lowest person_id? - participants["participant_num"] = ( - participants.sort_values(by=["tour_id", "person_id"]) - .groupby("tour_id") - .cumcount() - + 1 - ) + if estimator: + # In estimation mode, use participant_num from survey data to preserve consistency + # with the original survey data. ActivitySim treats participant_num=1 as the tour + # leader, so the joint tour in the tour table will be associated with the tour + # leader's person_id. We merge participant_num from survey data using the + # participant_id as the join key to ensure the correct tour leader is identified. + participants["participant_num"] = survey_participants_df.reindex( + participants.index + )["participant_num"] + else: + # assign participant_num + # FIXME do we want something smarter than the participant with the lowest person_id? + participants["participant_num"] = ( + participants.sort_values(by=["tour_id", "person_id"]) + .groupby("tour_id") + .cumcount() + + 1 + ) state.add_table("joint_tour_participants", participants)