diff --git a/pybaseball/team_results.py b/pybaseball/team_results.py index 6e7c44ce..796b9615 100644 --- a/pybaseball/team_results.py +++ b/pybaseball/team_results.py @@ -72,7 +72,7 @@ def get_table(soup: BeautifulSoup, team: str) -> pd.DataFrame: df = df.rename(columns=df.iloc[0]) df = df.reindex(df.index.drop(0)) df = df.drop('', axis=1) #not a useful column - df['Attendance'].replace(r'^Unknown$', np.nan, regex=True, inplace = True) # make this a NaN so the column can benumeric + df['Attendance'] = df['Attendance'].replace(r'^Unknown$', np.nan, regex=True) # make this a NaN so the column can be numeric return df def process_win_streak(data: pd.DataFrame) -> pd.DataFrame: diff --git a/tests/pybaseball/data/team_results.html b/tests/pybaseball/data/team_results.html new file mode 100644 index 00000000..3c933b0a --- /dev/null +++ b/tests/pybaseball/data/team_results.html @@ -0,0 +1,12 @@ + + + + + + + + + + +
Gm#DateTmOppHAWLRRAInnRankGBWinLossSaveTimeDNStreakAttendance
2019-04-01NYYBALHomeW5391--SmithAJonesBNone3:01D+1Unknownx
2019-04-02NYYBALHomeL24921.0DoeCRoeDNone2:55N-140,000x
Description row to be skipped
+ diff --git a/tests/pybaseball/test_team_results.py b/tests/pybaseball/test_team_results.py new file mode 100644 index 00000000..42f0232b --- /dev/null +++ b/tests/pybaseball/test_team_results.py @@ -0,0 +1,19 @@ +from typing import Callable + +from bs4 import BeautifulSoup + +from pybaseball.team_results import get_table + + +def test_get_table_unknown_attendance_becomes_nan(get_data_file_contents: Callable[[str], str]) -> None: + # Regression test for #459: 'Unknown' attendance values must be converted to + # NaN so the column can be made numeric. The previous chained-assignment + # `inplace=True` form silently failed under pandas copy-on-write (and emitted + # a FutureWarning), leaving the value as the string 'Unknown'. + soup = BeautifulSoup(get_data_file_contents('team_results.html'), 'lxml') + + result = get_table(soup, 'NYY') + + assert 'Attendance' in result.columns + assert result['Attendance'].isna().any() + assert 'Unknown' not in result['Attendance'].tolist()