From 793a804481c0663e754e7a0494e2e3299e746b27 Mon Sep 17 00:00:00 2001 From: "Soroosh.Mani" Date: Mon, 24 Apr 2023 13:19:45 -0400 Subject: [PATCH] Update pandas calls to support version 2 API --- stormevents/nhc/track.py | 6 ++++-- stormevents/usgs/events.py | 2 +- tests/test_atcf.py | 22 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/stormevents/nhc/track.py b/stormevents/nhc/track.py index cd71f2b..6e317d8 100644 --- a/stormevents/nhc/track.py +++ b/stormevents/nhc/track.py @@ -550,7 +550,9 @@ def atcf(self, advisory: ATCF_Advisory = None) -> DataFrame: integer_na_value = -99999 for column in float_columns: atcf.loc[pandas.isna(atcf[column]), column] = integer_na_value - atcf.loc[:, column] = atcf.loc[:, column].round(0).astype(int) + # Due to update in pandas 2 + atcf.loc[:, column] = atcf.loc[:, column].round(0) + atcf = atcf.astype({col: int for col in float_columns}) atcf["basin"] = atcf["basin"].str.pad(2) atcf["storm_number"] = atcf["storm_number"].astype("string").str.pad(3) @@ -654,7 +656,7 @@ def atcf(self, advisory: ATCF_Advisory = None) -> DataFrame: for column in atcf.select_dtypes(include=["string"]).columns: atcf[column] = atcf[column].str.replace( - re.compile(str(integer_na_value)), "" + re.compile(str(integer_na_value)), "", regex=True ) return atcf diff --git a/stormevents/usgs/events.py b/stormevents/usgs/events.py index edb089b..3e7c4c6 100644 --- a/stormevents/usgs/events.py +++ b/stormevents/usgs/events.py @@ -194,7 +194,7 @@ def usgs_flood_storms(year: int = None) -> DataFrame: else: matching_event["nhc_name"] = storm["name"] matching_event["nhc_code"] = storm.name - events = events.append(matching_event) + events.loc[len(events)] = matching_event events = events.loc[ ~pandas.isna(events["nhc_code"]), diff --git a/tests/test_atcf.py b/tests/test_atcf.py index 3d41c78..395ecf5 100644 --- a/tests/test_atcf.py +++ b/tests/test_atcf.py @@ -1,4 +1,5 @@ import pytest +from datetime import datetime from stormevents.nhc.atcf import ATCF_FileDeck from stormevents.nhc.atcf import atcf_files @@ -18,13 +19,26 @@ def test_atcf_url(): def test_atcf_nhc_codes(): - a_realtime = atcf_files(file_deck=ATCF_FileDeck.ADVISORY, mode=ATCF_Mode.REALTIME) - abf_realtime = atcf_files(mode=ATCF_Mode.REALTIME) + # Some months of the year this will return empty, resulting in + # the test to fail! Also testing for all (mode=ATCF_Mode.HISTORICAL) + # can be very slow. + # a_realtime = atcf_files(file_deck=ATCF_FileDeck.ADVISORY, mode=ATCF_Mode.REALTIME) + # abf_realtime = atcf_files(mode=ATCF_Mode.REALTIME) + # Using -2 to avoid test failure in months when the prior year's data + # has not been moved to the the archive url. + ref_year = datetime.now().year - 2 + a_historical = atcf_files( + file_deck=ATCF_FileDeck.ADVISORY, mode=ATCF_Mode.HISTORICAL, year=[ref_year] + ) + abf_historical = atcf_files(mode=ATCF_Mode.HISTORICAL, year=[ref_year]) a_2014_2015 = atcf_files(file_deck=ATCF_FileDeck.ADVISORY, year=range(2014, 2015)) abf_2014_2015 = atcf_files(year=range(2014, 2015)) - assert len(a_realtime) > 0 - assert len(abf_realtime) > 0 + # assert len(a_realtime) > 0 + # assert len(abf_realtime) > 0 + assert len(a_historical) > 0 + assert len(abf_historical) > 0 + assert len(a_historical) < len(abf_historical) assert len(a_2014_2015) > 0 assert len(abf_2014_2015) > 0