import tif1
def analyze_session(year, event, session_type, lib="pandas"):
"""Analyze session with any backend."""
session = tif1.get_session(year, event, session_type, lib=lib)
laps = session.laps
# These operations work with both backends
total_laps = len(laps)
drivers = laps["Driver"].unique()
print(f"Backend: {lib}")
print(f"Total laps: {total_laps}")
print(f"Drivers: {len(drivers)}")
# Backend-specific aggregation
if lib == "polars":
import polars as pl
fastest = (
laps.group_by("Driver")
.agg(pl.col("LapTime").min().alias("fastest"))
.sort("fastest")
)
else: # pandas
fastest = (
laps.groupby("Driver", observed=True)["LapTime"]
.min()
.sort_values()
.to_frame("fastest")
)
print("\nFastest laps:")
print(fastest.head(5))
return fastest
# Test with both backends
print("=" * 60)
analyze_session(2025, "Monaco Grand Prix", "Race", lib="pandas")
print("\n" + "=" * 60)
analyze_session(2025, "Monaco Grand Prix", "Race", lib="polars")