First Lap Time #189
Replies: 2 comments 12 replies
-
No, there isn't because the sector 1 time and lap time do not exist for the first lap of a race. You can take a look at https://www.fia.com/sites/default/files/decision-document/2022%20Spanish%20Grand%20Prix%20-%20Event%20Notes%20-%20Circuit%20Map%20v2.pdf for example. |
Beta Was this translation helpful? Give feedback.
-
You can get lap times for all laps, including the first lap, from the ergast API. For example: from fastf1.ergast import base_url, _parse_ergast, _parse_json_response, _headers
from fastf1.api import Cache
from pandas import json_normalize
import pandas as pd
def _parse_ergast(data, table="RaceTable:Races", as_df=False):
"""Parse specified table from ergast data."""
data_table = data['MRData']
for t in table.split(":"):
if t:
data_table = data_table[t]
return data_table if not as_df else json_normalize(data_table)
#http://ergast.com/api/f1/drivers
#http://ergast.com/api/f1/2010/drivers
#http://ergast.com/api/f1/2010/2/drivers
def fetch_driver(year, gp=None, as_df=False):
"""Get driver information from ergast API by year and optionally by race."""
if gp is None:
url = f"{base_url}/{year}/drivers.json"
else:
url = f"{base_url}/{year}/{gp}/drivers.json"
return _parse_ergast(_parse_json_response(
Cache.requests_get(url, headers=_headers)),
"DriverTable:Drivers", as_df
)
def ergast_driver_code_number(year, gp=None, typ="code", index="driverId"):
"""Get driver codes via ergast API."""
_driver_details = fetch_driver(year, gp, as_df=True)
_driver_details["permanentNumber"] = _driver_details["permanentNumber"].astype(int)
typ = "permanentNumber" if typ.startswith("num") else typ
if typ in ["code", "permanentNumber"]:
return _driver_details[["driverId", typ]].set_index(index).squeeze().to_dict()
return _driver_details[["code", "permanentNumber", "driverId"]]
ERGAST_CODES = ergast_driver_code_number(2022)
#http://ergast.com/api/f1/2011/5/laps/1
#http://ergast.com/api/f1/2011/5/drivers/alonso/laps/1
def fetch_laptimes(year, gp, lap=None, driver=None, as_df=False):
"""Get laptime information from ergast API by year and race,
and optionally by driver and/or lap."""
url = f"{base_url}/{year}/{gp}"
if driver:
url = f"{url}/drivers/{driver}"
url = f"{url}/laps"
if lap:
url = f"{url}/{lap}"
url = f"{url}.json"
_laps = _parse_ergast(_parse_json_response(
Cache.requests_get(url, headers=_headers))
)[0]["Laps"]
if as_df:
_df = pd.DataFrame(_laps[0]["Timings"])
_df['driverId'] = _df['driverId'].map(ERGAST_CODES)
return _df
return _laps
# Get laptimes for first lap for second race of 2022
fetch_laptimes(2022, 2, 1) |
Beta Was this translation helpful? Give feedback.
-
import pandas as pd
import fastf1 as ff1
from fastf1 import plotting
race = ff1.get_session(Year-1, Grand_Prix, 'R')
race.load()
laps = race.laps
ff1.Cache.enable_cache('cache')
pd.options.mode.chained_assignment = None
laps_rus = laps.pick_driver('RUS') # pick a driver
lap = laps_rus[laps_rus['LapNumber'] == 1].iloc[0]
print(lap)
When I print the info, the lap time and the Sector1 time are stated as "NaT". Is there a way to find the lap time and the time taken to complete the 1st sector?
Beta Was this translation helpful? Give feedback.
All reactions