Skip to content

Commit 47ca0c8

Browse files
authored
Merge pull request #203 from bkemper24/main
updates to tests for Pandas 3 changes
2 parents aea32ea + f09ceb8 commit 47ca0c8

4 files changed

Lines changed: 77 additions & 17 deletions

File tree

swat/tests/cas/test_bygroups.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,17 @@ def assertTablesEqual(self, a, b, fillna=-999999, sortby=SORT_KEYS,
112112
a = a.sort_values(sortby)
113113
b = b.sort_values(sortby)
114114
self.assertEqual(list(a.columns), list(b.columns))
115-
a = a.fillna(value=fillna)
116-
b = b.fillna(value=fillna)
115+
116+
if pd_version >= (2, 2, 0) and pd_version < (3, 0, 0):
117+
# fix 2.2 and 2.3 FutureWarning:
118+
# Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated
119+
with pd.option_context('future.no_silent_downcasting', True):
120+
a = a.fillna(value=fillna)
121+
b = b.fillna(value=fillna)
122+
else:
123+
a = a.fillna(value=fillna)
124+
b = b.fillna(value=fillna)
125+
117126
for lista, listb in zip(list(a.to_records(index=include_index)),
118127
list(b.to_records(index=include_index))):
119128
lista = list(lista)
@@ -701,7 +710,8 @@ def test_column_max(self):
701710
sortby=['Origin', 'EngineSize'])
702711

703712
@unittest.skipIf(pd_version < (0, 16, 0), 'Need newer version of Pandas')
704-
@unittest.skipIf(pd_version >= (1, 0, 0), 'Raises AssertionError in Pandas 1')
713+
@unittest.skipIf(pd_version >= (1, 0, 0) and pd_version < (2, 0, 0),
714+
'Raises AssertionError in Pandas 1')
705715
def test_max(self):
706716
df = self.get_cars_df().sort_values(SORT_KEYS)
707717
tbl = self.table.sort_values(SORT_KEYS)

swat/tests/cas/test_datamsg.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,13 @@ def test_text(self):
350350
self.assertTablesEqual(f, s, sortby=SORT_KEYS)
351351

352352
def test_json(self):
353+
import io
353354
df = self.table.to_frame()
354355
jsondf = df.to_json()
355356

356-
dmh = swat.datamsghandlers.JSON(jsondf)
357+
# Pandas 3 no longer supports passing a json text string.
358+
# You must pass a file path or object with a read method
359+
dmh = swat.datamsghandlers.JSON(io.StringIO(jsondf))
357360

358361
tbl = self.s.addtable(table='cars', **dmh.args.addtable).casTable
359362

swat/tests/cas/test_table.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,13 +1162,16 @@ def test_describe(self):
11621162
self.assertEqual(desc.loc['freq'].tolist(), dfdesc.loc['freq'].tolist())
11631163

11641164
# Percentiles
1165+
# Pandas < 3 always includes percentile 0.5 even if you don't ask for it
1166+
# Starting with Pandas 3, percentile 0.5 is not included unless you ask for it
1167+
# CASTable always includes 0.5, regardless of pandas version.
11651168
desc = self.table.describe(percentiles=[0.3, 0.7])
1166-
dfdesc = df.describe(percentiles=[0.3, 0.7])
1169+
dfdesc = df.describe(percentiles=[0.3, 0.5, 0.7])
11671170
self.assertEqual(desc.index.tolist(), dfdesc.index.tolist())
11681171
self.assertEqual(desc.columns.tolist(), dfdesc.columns.tolist())
11691172

11701173
desc = self.table.describe(percentiles=0.4)
1171-
dfdesc = df.describe(percentiles=[0.4])
1174+
dfdesc = df.describe(percentiles=[0.4, 0.5])
11721175
self.assertEqual(desc.index.tolist(), dfdesc.index.tolist())
11731176
self.assertEqual(desc.columns.tolist(), dfdesc.columns.tolist())
11741177

@@ -1536,17 +1539,39 @@ def test_mode(self):
15361539
tblgrp = tbl[['Make', 'Type']].groupby(['Make'])
15371540

15381541
# TODO: Pandas mode sets columns with all unique values to NaN
1539-
self.assertEqual(
1540-
dfgrp.get_group('Acura').mode()[['Type']].to_csv(index=False),
1541-
tblgrp.mode().loc['Acura', ['Type']].dropna(how='all').to_csv(index=False))
1542+
if pd_version >= (2, 2, 0):
1543+
# Syntax Change in pandas 3.
1544+
# Future Warning in Pandas 2.2+
1545+
# When grouping with a length-1 list-like,
1546+
# you will need to pass a length-1 tuple to get_group
1547+
self.assertEqual(
1548+
dfgrp.get_group(('Acura',)).mode()[['Type']].to_csv(index=False),
1549+
tblgrp.mode().loc['Acura', ['Type']].dropna(how='all')
1550+
.to_csv(index=False))
1551+
else:
1552+
self.assertEqual(
1553+
dfgrp.get_group('Acura').mode()[['Type']].to_csv(index=False),
1554+
tblgrp.mode().loc['Acura', ['Type']].dropna(how='all')
1555+
.to_csv(index=False))
15421556

15431557
dfgrp = df[['Cylinders', 'MPG_City']].groupby(['Cylinders'])
15441558
tblgrp = tbl[['Cylinders', 'MPG_City']].groupby(['Cylinders'])
15451559

15461560
# TODO: Pandas mode sets columns with all unique values to NaN
1547-
self.assertEqual(
1548-
dfgrp.get_group(6.0).mode()[['MPG_City']].to_csv(index=False),
1549-
tblgrp.mode().loc[6.0, ['MPG_City']].dropna(how='all').to_csv(index=False))
1561+
if pd_version >= (2, 2, 0):
1562+
# Syntax Change in pandas 3.
1563+
# Future Warning in Pandas 2.2+
1564+
# When grouping with a length-1 list-like,
1565+
# you will need to pass a length-1 tuple to get_group
1566+
self.assertEqual(
1567+
dfgrp.get_group((6.0,)).mode()[['MPG_City']].to_csv(index=False),
1568+
tblgrp.mode().loc[6.0, ['MPG_City']].dropna(how='all')
1569+
.to_csv(index=False))
1570+
else:
1571+
self.assertEqual(
1572+
dfgrp.get_group(6.0).mode()[['MPG_City']].to_csv(index=False),
1573+
tblgrp.mode().loc[6.0, ['MPG_City']].dropna(how='all')
1574+
.to_csv(index=False))
15501575

15511576
def test_median(self):
15521577
df = self.get_cars_df()
@@ -4652,7 +4677,9 @@ def test_to_html(self):
46524677

46534678
html = tbl.to_html(index=False)
46544679

4655-
df2 = pd.read_html(html)[0]
4680+
# Starting with Pandas 3 you can no longer
4681+
# pass an html string to pandas read_html.
4682+
df2 = pd.read_html(io.StringIO(html))[0]
46564683

46574684
df['Model'] = df['Model'].str.strip()
46584685

swat/utils/testing.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737

3838
RE_TYPE = type(re.compile(r''))
3939

40+
pd_version = tuple([int(x) for x in re.match(r'^(\d+)\.(\d+)\.(\d+)',
41+
pd.__version__).groups()])
4042

4143
warnings.filterwarnings('ignore', category=OptionWarning)
4244
warnings.filterwarnings('ignore', category=RuntimeWarning)
@@ -93,8 +95,17 @@ def assertTablesEqual(self, a, b, fillna=-999999, sortby=None, precision=None):
9395
a = a.sort_values(sortby, na_position='first')
9496
b = b.sort_values(sortby, na_position='first')
9597
self.assertEqual(list(a.columns), list(b.columns))
96-
a = a.fillna(value=fillna)
97-
b = b.fillna(value=fillna)
98+
99+
if pd_version >= (2, 2, 0) and pd_version < (3, 0, 0):
100+
# fix 2.2 and 2.3 FutureWarning:
101+
# Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated
102+
with pd.option_context('future.no_silent_downcasting', True):
103+
a = a.fillna(value=fillna)
104+
b = b.fillna(value=fillna)
105+
else:
106+
a = a.fillna(value=fillna)
107+
b = b.fillna(value=fillna)
108+
98109
if precision is not None:
99110
a = a.round(decimals=precision)
100111
b = b.round(decimals=precision)
@@ -108,8 +119,17 @@ def assertColsEqual(self, a, b, fillna=-999999, sort=False, precision=None):
108119
a = a.to_series()
109120
if hasattr(b, 'to_series'):
110121
b = b.to_series()
111-
a = a.fillna(value=fillna)
112-
b = b.fillna(value=fillna)
122+
123+
if pd_version >= (2, 2, 0) and pd_version < (3, 0, 0):
124+
# fix 2.2 and 2.3 FutureWarning:
125+
# Downcasting object dtype arrays on .fillna, .ffill, .bfill is deprecated
126+
with pd.option_context('future.no_silent_downcasting', True):
127+
a = a.fillna(value=fillna)
128+
b = b.fillna(value=fillna)
129+
else:
130+
a = a.fillna(value=fillna)
131+
b = b.fillna(value=fillna)
132+
113133
if precision is not None:
114134
a = a.round(decimals=precision)
115135
b = b.round(decimals=precision)

0 commit comments

Comments
 (0)