Skip to content

Commit

Permalink
Update pandas calls to support version 2 API
Browse files Browse the repository at this point in the history
  • Loading branch information
SorooshMani-NOAA committed Apr 24, 2023
1 parent 9cb5d77 commit 793a804
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
6 changes: 4 additions & 2 deletions stormevents/nhc/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stormevents/usgs/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
22 changes: 18 additions & 4 deletions tests/test_atcf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from datetime import datetime

from stormevents.nhc.atcf import ATCF_FileDeck
from stormevents.nhc.atcf import atcf_files
Expand All @@ -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

Expand Down

0 comments on commit 793a804

Please sign in to comment.