From 25021d9fe8eb0a588a287d07695e5030a9c7d3f1 Mon Sep 17 00:00:00 2001 From: Pieter Robberechts Date: Sat, 20 Jan 2024 12:53:36 +0100 Subject: [PATCH] fix(opta): Fix Opta F24 timestamp parsing Opta does not zero-pad milliseconds. Therefore, they were incorrectly parsed by Python's default "%f" format code. See also #267 --- kloppy/infra/serializers/event/opta/deserializer.py | 5 +++++ kloppy/tests/test_opta.py | 1 - 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/kloppy/infra/serializers/event/opta/deserializer.py b/kloppy/infra/serializers/event/opta/deserializer.py index 01a81e25..e2bbce35 100644 --- a/kloppy/infra/serializers/event/opta/deserializer.py +++ b/kloppy/infra/serializers/event/opta/deserializer.py @@ -246,6 +246,11 @@ def _parse_f24_datetime(dt_str: str) -> float: + def zero_pad_milliseconds(timestamp): + parts = timestamp.split(".") + return ".".join(parts[:-1] + ["{:03d}".format(int(parts[-1]))]) + + dt_str = zero_pad_milliseconds(dt_str) return ( datetime.strptime(dt_str, "%Y-%m-%dT%H:%M:%S.%f") .replace(tzinfo=pytz.utc) diff --git a/kloppy/tests/test_opta.py b/kloppy/tests/test_opta.py index 684fa2e9..f4be28a4 100644 --- a/kloppy/tests/test_opta.py +++ b/kloppy/tests/test_opta.py @@ -56,7 +56,6 @@ def dataset(base_dir) -> EventDataset: return dataset -@pytest.mark.xfail def test_parse_f24_datetime(): """Test if the F24 datetime is correctly parsed""" # timestamps have millisecond precision