theOehrly / Fast-F1

FastF1 is a python package for accessing and analyzing Formula 1 results, schedules, timing data and telemetry
https://docs.fastf1.dev
MIT License
2.46k stars 259 forks source link

[BUG] pick_track_status crashes when drivers retire #590

Closed Casper-Guo closed 4 months ago

Casper-Guo commented 4 months ago

Describe the issue:

In laps.pick_track_status, the contains, excludes, any, and none values of the how argument can cause crashes when a driver retires on track. The last laps of these drivers have NaN as their track status. When supplied to Pandas.Series.str.contains, NaN is returned which cannot be used to index the dataframe.

Two potential solutions:

  1. Handle the NaN case directly by passing the na argument to str.contains. The default behavior can depend on how inclusive/exclusive the how argument is.
  2. Add track status to the last lap of retired drivers.

Reproduce the code example:

import fastf1

session = fastf1.get_session(2024, 2, "R")
session.load(telemetry=False, weather=False)
session.laps.pick_track_status("467", how="none")

Error message:

File "/mnt/d/Projects/F1-Visualization/test.py", line 5, in <module>
    session.laps.pick_track_status("467", how="none")
  File "/home/robery/.local/lib/python3.11/site-packages/fastf1/core.py", line 3199, in pick_track_status
    return self[~self['TrackStatus'].str.contains('|'.join(status),
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robery/.local/lib/python3.11/site-packages/pandas/core/generic.py", line 1461, in __invert__
    new_data = self._mgr.apply(operator.invert)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/robery/.local/lib/python3.11/site-packages/pandas/core/internals/managers.py", line 350, in apply
    applied = b.apply(f, **kwargs)
              ^^^^^^^^^^^^^^^^^^^^
  File "/home/robery/.local/lib/python3.11/site-packages/pandas/core/internals/blocks.py", line 329, in apply
    result = func(self.values, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: bad operand type for unary ~: 'float'
Casper-Guo commented 4 months ago

Oops I forgot this is already reported in #575 (I even commented there). 😅