Skip to content

Commit

Permalink
fix(opta): Fix Opta F24 timestamp parsing
Browse files Browse the repository at this point in the history
Opta does not zero-pad milliseconds. Therefore, they were incorrectly parsed
by Python's default "%f" format code.

See also PySport#267
  • Loading branch information
probberechts committed Jan 20, 2024
1 parent 25045aa commit 25021d9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
5 changes: 5 additions & 0 deletions kloppy/infra/serializers/event/opta/deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion kloppy/tests/test_opta.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 25021d9

Please sign in to comment.