Skip to content

Commit

Permalink
Add date as property on Match (#215)
Browse files Browse the repository at this point in the history
* Add date as property on Match

* Fix typo

* Add test with no date

* Fix lint

* Try test with copy

---------

Co-authored-by: Swopper050 <bram@witoos.nl>
  • Loading branch information
Swopper050 and Swopper050 authored Apr 29, 2024
1 parent 3cd8741 commit 4d29937
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cicd_main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI/CD workflow
on: [push, pull_request]

jobs:
buld-and-test:
build-and-test:
# Set up operating system
runs-on: ubuntu-latest

Expand Down
32 changes: 19 additions & 13 deletions databallpy/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,23 +160,29 @@ def event_timestamp_is_precise(self) -> bool:
def is_synchronised(self) -> bool:
return self._is_synchronised

@property
def date(self) -> pd.Timestamp | None:
if "start_datetime_td" in self.periods.columns:
return self.periods.loc[
self.periods["period_id"] == 1, "start_datetime_td"
].iloc[0]
elif "start_datetime_ed" in self.periods.columns:
return self.periods.loc[
self.periods["period_id"] == 1, "start_datetime_ed"
].iloc[0]

return None

@property
def name(self) -> str:
home_text = f"{self.home_team_name} {self.home_score}"
away_text = f"{self.away_score} {self.away_team_name}"
if "start_datetime_td" in self.periods.columns:
date = str(
self.periods.loc[
self.periods["period_id"] == 1, "start_datetime_td"
].iloc[0]
)[:19]
else:
date = str(
self.periods.loc[
self.periods["period_id"] == 1, "start_datetime_ed"
].iloc[0]
)[:19]
return f"{home_text} - {away_text} {date}"

date = self.date
if date is None:
return f"{home_text} - {away_text}"

return f"{home_text} - {away_text} {date.strftime('%Y-%m-%d %H:%M:%S')}"

@requires_tracking_data
def home_players_column_ids(self) -> list[str]:
Expand Down
12 changes: 12 additions & 0 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,11 @@ def test__repr__(self):
def test_match__eq__(self):
assert not self.expected_match_tracab_opta == pd.DataFrame()

def test_match_date(self):
assert self.expected_match_tracab_opta.date == pd.Timestamp(
"2023-01-14 16:46:39.720000+0100", tz="Europe/Amsterdam"
)

def test_match_name(self):
assert (
self.expected_match_tracab_opta.name
Expand All @@ -997,6 +1002,13 @@ def test_match_name(self):
self.expected_match_opta.name == "TeamOne 3 - 1 TeamTwo 2023-01-22 12:18:32"
)

def test_match_name_no_date(self):
match = self.expected_match_tracab_opta.copy()
match.periods = match.periods.drop(
columns=["start_datetime_td", "start_datetime_ed"], errors="ignore"
)
assert match.name == "TeamOne 3 - 1 TeamTwo"

def test_match_home_players_column_ids(self):
assert self.expected_match_tracab_opta.home_players_column_ids() == [
"home_34",
Expand Down

0 comments on commit 4d29937

Please sign in to comment.