Skip to content

Commit

Permalink
make int downcast an option
Browse files Browse the repository at this point in the history
  • Loading branch information
i-am-sijia committed Dec 7, 2023
1 parent 37e7ab5 commit cbe6310
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 3 additions & 1 deletion activitysim/core/assign.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,8 @@ def rng_lognormal(random_draws, mu, sigma, broadcast=True, scale=False):
# we stored result in dict - convert to df
variables = util.df_from_dict(variables, index=df.index)

util.auto_opt_pd_dtypes(variables, inplace=True)
util.auto_opt_pd_dtypes(
variables, downcast_int=False, downcast_float=False, inplace=True
)

return variables, trace_results, trace_assigned_locals
10 changes: 7 additions & 3 deletions activitysim/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def quick_loc_series(loc_list, target_series):
return df.right


def assign_in_place(df, df2, downcast_float=False):
def assign_in_place(df, df2, downcast_int=False, downcast_float=False):
"""
update existing row values in df from df2, adding columns to df if they are not there
Expand Down Expand Up @@ -383,11 +383,11 @@ def assign_in_place(df, df2, downcast_float=False):
if pd.api.types.is_object_dtype(df[c]):
df[c] = df[c].astype("category")

auto_opt_pd_dtypes(df, downcast_float, inplace=True)
auto_opt_pd_dtypes(df, downcast_int, downcast_float, inplace=True)


def auto_opt_pd_dtypes(
df_: pd.DataFrame, downcast_float=False, inplace=False
df_: pd.DataFrame, downcast_int=False, downcast_float=False, inplace=False
) -> Optional[pd.DataFrame]:
"""
Automatically downcast Number dtypes for minimal possible,
Expand All @@ -397,6 +397,8 @@ def auto_opt_pd_dtypes(
----------
df_ : pd.DataFrame
assignment left-hand-side (dest)
downcast_int: bool
if True, downcast int columns if possible
downcast_float: bool
if True, downcast float columns if possible
inplace: bool
Expand Down Expand Up @@ -429,6 +431,8 @@ def auto_opt_pd_dtypes(
continue
# Handle integer types
if pd.api.types.is_integer_dtype(dtype):
if not downcast_int:
continue
# there is a bug in pandas to_numeric
# when convert int and floats gt 16777216
# https://github.com/pandas-dev/pandas/issues/43693
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ def stop_frequency_university_parking(

park_trips = park_to_campus | park_from_campus

# check if parking_name is in the purpose category
if not parking_name in trip_choosers.purpose.cat.categories:
trip_choosers.purpose = trip_choosers.purpose.cat.add_categories(
[parking_name]
)
trip_choosers.loc[park_trips, "purpose"] = parking_name
trip_choosers.loc[park_trips, "destination_logsum"] = pd.NA
trip_choosers.loc[park_trips, "destination"] = trip_choosers.loc[
Expand Down

0 comments on commit cbe6310

Please sign in to comment.