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.54k stars 264 forks source link

[BUG] Strange error using pick_lap() with variable within for loop #589

Closed fifisaac closed 6 months ago

fifisaac commented 6 months ago

Describe the issue:

Using the pick_lap() function with a variable as a parameter within a for loop returns an error, but works correctly with a value passed as a parameter instead

Reproduce the code example:

# Code that fails to work
import fastf1

drivers = ['1', '4']

session = fastf1.get_session(2024, 1, 'R') 
session.load(weather=False)
lap = 2

for i in drivers:
    lap = session.laps.pick_driver(i).pick_lap(lap)

# Code that works
import fastf1

drivers = ['1', '4']

session = fastf1.get_session(2024, 1, 'R') 
session.load(weather=False)

for i in drivers:
    lap = session.laps.pick_driver(i).pick_lap(2)

Error message:

Traceback (most recent call last):
  File "c:\Users\fifac\Documents\A LEVEL CS PROJECT\.git\import fastf1.py", line 11, in <module>
    lap = session.laps.pick_driver(i).pick_lap(lap)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\fifac\Documents\A LEVEL CS PROJECT\env\Lib\site-packages\fastf1\core.py", line 2944, in pick_lap
    return self[self['LapNumber'] == lap_number]
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\fifac\Documents\A LEVEL CS PROJECT\env\Lib\site-packages\pandas\core\ops\common.py", line 76, in new_method
    return method(self, other)
           ^^^^^^^^^^^^^^^^^^^
  File "c:\Users\fifac\Documents\A LEVEL CS PROJECT\env\Lib\site-packages\pandas\core\arraylike.py", line 40, in __eq__
    return self._cmp_method(other, operator.eq)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\fifac\Documents\A LEVEL CS PROJECT\env\Lib\site-packages\pandas\core\frame.py", line 7897, in _cmp_method
    self, other = self._align_for_op(other, axis, flex=False, level=None)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\fifac\Documents\A LEVEL CS PROJECT\env\Lib\site-packages\pandas\core\frame.py", line 8205, in _align_for_op
    raise ValueError(
ValueError: Operands are not aligned. Do `left, right = left.align(right, axis=1, copy=False)` before operating.
fifisaac commented 6 months ago

Apologies, just realised pick_lap() is deprecated

Casper-Guo commented 6 months ago

Jump in to say that the error is not because pick_lap itself. You are reassigning the lap variable in the first iteration of the loop which causes the subsequent iterations to fail.

fifisaac commented 6 months ago

Jump in to say that the error is not because pick_lap itself. You are reassigning the lap variable in the first iteration of the loop which causes the subsequent iterations to fail.

Thank you! Spent so much time banging my head against the wall I missed that