You can use pd.option_context() to set the option 'mode.use_inf_as_na' to True temporarily. This will make operations treat infinite values as NaN within the specified context.