From aef3668deed09c245f463e627536f17b433e49c7 Mon Sep 17 00:00:00 2001 From: Casper Guo <89810860+Casper-Guo@users.noreply.github.com> Date: Thu, 9 Nov 2023 15:50:15 -0500 Subject: [PATCH] Fix inconsistency in pick_fastest, add deprecation warning for future behavior change (#476) --- fastf1/core.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/fastf1/core.py b/fastf1/core.py index faa6e37ee..bb9867a59 100644 --- a/fastf1/core.py +++ b/fastf1/core.py @@ -2844,18 +2844,32 @@ def pick_fastest(self, only_by_time: bool = False) -> "Lap": Returns: instance of :class:`Lap` """ + # TODO: Deprecate returning empty lap object when there is no lap + # that matches definion if only_by_time: laps = self # all laps else: # select only laps marked as personal fastest - laps = self.loc[self['IsPersonalBest'] == True] # noqa: E712 comparison with True + laps = self.loc[self['IsPersonalBest'] == True] # noqa: E712 if not laps.size: + warnings.warn(("None will be returned instead of an empty Lap " + "object when there are no laps that satisfies " + "the definition for fastest lap starting from " + "version 3.3"), + DeprecationWarning) + return Lap(index=self.columns, dtype=object).__finalize__(self) + + if laps['LapTime'].isna().all(): + warnings.warn(("None will be returned instead of an empty Lap " + "object when there is no recorded LapTime for " + "any lap starting from version 3.3"), + DeprecationWarning) return Lap(index=self.columns, dtype=object).__finalize__(self) lap = laps.loc[laps['LapTime'].idxmin()] if isinstance(lap, pd.DataFrame): - # More laps, same time + # Multiple laps, same time lap = lap.iloc[0] # take first clocked return lap