From cac6171cd54047e54a6f280ebc5e4511f7e9ef51 Mon Sep 17 00:00:00 2001 From: Pieter Robberechts Date: Thu, 9 May 2024 21:55:21 +0200 Subject: [PATCH] fix(fbref): Handle games with missing events Fixes #547 --- soccerdata/fbref.py | 4 +++- tests/test_FBref.py | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/soccerdata/fbref.py b/soccerdata/fbref.py index 32bfb19c..af20a141 100644 --- a/soccerdata/fbref.py +++ b/soccerdata/fbref.py @@ -1000,10 +1000,12 @@ def read_events( "event_type": event_type, } ) - df_match_events = pd.DataFrame(match_events).sort_values(by="minute") + df_match_events = pd.DataFrame(match_events) df_match_events["game"] = game["game"] df_match_events["league"] = game["league"] df_match_events["season"] = game["season"] + if len(df_match_events) > 0: + df_match_events.sort_values(by="minute", inplace=True) events.append(df_match_events) if len(events) == 0: diff --git a/tests/test_FBref.py b/tests/test_FBref.py index 7b860f10..d2f5dcb0 100644 --- a/tests/test_FBref.py +++ b/tests/test_FBref.py @@ -99,6 +99,12 @@ def test_read_events_yellow_for_manager() -> None: assert "Pepe Bordalás" in yellow_cards["player1"].tolist() +def test_missing_events() -> None: + fbref = sd.FBref("FRA-Ligue 1", "19-20") + events = fbref.read_events(match_id="1d845950") + assert len(events) == 0 + + def test_read_shot_events(fbref_ligue1: FBref) -> None: assert isinstance(fbref_ligue1.read_shot_events(match_id="796787da"), pd.DataFrame)