Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pybaseball/team_game_logs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from io import StringIO
import pandas as pd
from bs4 import BeautifulSoup

Expand All @@ -17,7 +18,7 @@ def get_table(season: int, team: str, log_type: str) -> pd.DataFrame:
table = soup.find("table", attrs=dict(id=table_id))
if table is None:
raise RuntimeError("Table with expected id not found on scraped page.")
data = pd.read_html(str(table))[0]
data = pd.read_html(StringIO(str(table)))[0]
return data


Expand All @@ -33,7 +34,8 @@ def postprocess(data: pd.DataFrame) -> pd.DataFrame:
data.rename(repl_dict, axis=1, inplace=True)
data["Home"] = data["Home"].isnull() # '@' if away, empty if home
data = data[data["Game"].str.match(r"\d+")] # drop empty month rows
data = data.apply(pd.to_numeric, errors="ignore")
for column in data.columns:
data[column] = pd.to_numeric(data[column], errors='coerce').fillna(data[column])
data["Game"] = data["Game"].astype(int)
return data.reset_index(drop=True)

Expand Down
6 changes: 6 additions & 0 deletions tests/pybaseball/data/team_game_logs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Game,Date,Home,Opp,Rslt,PA,AB,R,H,2B,3B,HR,RBI,BB,IBB,SO,HBP,SH,SF,ROE,GDP,SB,CS,BA,OBP,SLG,OPS,LOB,NumPlayers,Thr,OppStart
1,Mar 28,True,LAA,"W,11-3",41,33,11,10,2,0,2,11,6,0,5,0,0,2,1,0,1,0,0.303,0.39,0.545,0.936,6,11,L,P.Sandoval(27)
2,Mar 30,True,LAA,"W,13-4",41,38,13,14,5,1,2,13,2,0,6,0,0,1,0,0,0,0,0.338,0.39,0.634,1.024,4,12,R,G.Canning(34)
3,Mar 31,True,LAA,"L,1-4",36,30,1,3,0,0,0,1,5,0,9,1,0,0,1,0,0,0,0.267,0.347,0.475,0.823,8,13,L,R.Detmers(63)
4,Apr 1,True,KCR,"W,6-4",35,34,6,9,1,0,2,6,1,0,7,0,0,0,0,0,0,0,0.267,0.333,0.474,0.807,4,11,R,M.Wacha(53)
5,Apr 2,True,KCR,"L,1-4",31,30,1,3,2,0,0,1,1,0,6,0,0,0,0,0,0,0,0.236,0.299,0.418,0.717,3,9,R,A.Marsh(73)
15 changes: 15 additions & 0 deletions tests/pybaseball/test_team_game_logs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pandas as pd
from pybaseball.team_game_logs import team_game_logs
import pytest


@pytest.fixture(name="sample_processed_result")
def sample_processed_result() -> pd.DataFrame:
return pd.read_csv('team_gamelogs.csv')


def test_team_game_logs(sample_processed_result):
test_output = team_game_logs(2024, "BAL", "batting")
test_output.reset_index(drop=True, inplace=True)
test_output_first_five = test_output.head(5)
pd.testing.assert_frame_equal(test_output_first_five, sample_processed_result, check_dtype=False)