Skip to content

Commit 8b8af9b

Browse files
committed
MAINT: Remove self.assertAlmostEqual from testing
1 parent d647045 commit 8b8af9b

12 files changed

+85
-71
lines changed

pandas/tests/indexing/test_chaining_and_caching.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_slice_consolidate_invalidate_item_cache(self):
3232
# Assignment to wrong series
3333
df['bb'].iloc[0] = 0.17
3434
df._clear_item_cache()
35-
self.assertAlmostEqual(df['bb'][0], 0.17)
35+
tm.assert_almost_equal(df['bb'][0], 0.17)
3636

3737
def test_setitem_cache_updating(self):
3838
# GH 5424

pandas/tests/io/json/test_ujson.py

+50-37
Original file line numberDiff line numberDiff line change
@@ -738,37 +738,37 @@ def test_numericIntExp(self):
738738
def test_numericIntFrcExp(self):
739739
input = "1.337E40"
740740
output = ujson.decode(input)
741-
self.assertAlmostEqual(output, json.loads(input))
741+
tm.assert_almost_equal(output, json.loads(input))
742742

743743
def test_decodeNumericIntExpEPLUS(self):
744744
input = "1337E+9"
745745
output = ujson.decode(input)
746-
self.assertAlmostEqual(output, json.loads(input))
746+
tm.assert_almost_equal(output, json.loads(input))
747747

748748
def test_decodeNumericIntExpePLUS(self):
749749
input = "1.337e+40"
750750
output = ujson.decode(input)
751-
self.assertAlmostEqual(output, json.loads(input))
751+
tm.assert_almost_equal(output, json.loads(input))
752752

753753
def test_decodeNumericIntExpE(self):
754754
input = "1337E40"
755755
output = ujson.decode(input)
756-
self.assertAlmostEqual(output, json.loads(input))
756+
tm.assert_almost_equal(output, json.loads(input))
757757

758758
def test_decodeNumericIntExpe(self):
759759
input = "1337e40"
760760
output = ujson.decode(input)
761-
self.assertAlmostEqual(output, json.loads(input))
761+
tm.assert_almost_equal(output, json.loads(input))
762762

763763
def test_decodeNumericIntExpEMinus(self):
764764
input = "1.337E-4"
765765
output = ujson.decode(input)
766-
self.assertAlmostEqual(output, json.loads(input))
766+
tm.assert_almost_equal(output, json.loads(input))
767767

768768
def test_decodeNumericIntExpeMinus(self):
769769
input = "1.337e-4"
770770
output = ujson.decode(input)
771-
self.assertAlmostEqual(output, json.loads(input))
771+
tm.assert_almost_equal(output, json.loads(input))
772772

773773
def test_dumpToFile(self):
774774
f = StringIO()
@@ -1583,36 +1583,49 @@ def test_decodeArrayFaultyUnicode(self):
15831583
def test_decodeFloatingPointAdditionalTests(self):
15841584
places = 15
15851585

1586-
self.assertAlmostEqual(-1.1234567893,
1587-
ujson.loads("-1.1234567893"), places=places)
1588-
self.assertAlmostEqual(-1.234567893,
1589-
ujson.loads("-1.234567893"), places=places)
1590-
self.assertAlmostEqual(-1.34567893,
1591-
ujson.loads("-1.34567893"), places=places)
1592-
self.assertAlmostEqual(-1.4567893,
1593-
ujson.loads("-1.4567893"), places=places)
1594-
self.assertAlmostEqual(-1.567893,
1595-
ujson.loads("-1.567893"), places=places)
1596-
self.assertAlmostEqual(-1.67893,
1597-
ujson.loads("-1.67893"), places=places)
1598-
self.assertAlmostEqual(-1.7893, ujson.loads("-1.7893"), places=places)
1599-
self.assertAlmostEqual(-1.893, ujson.loads("-1.893"), places=places)
1600-
self.assertAlmostEqual(-1.3, ujson.loads("-1.3"), places=places)
1601-
1602-
self.assertAlmostEqual(1.1234567893, ujson.loads(
1603-
"1.1234567893"), places=places)
1604-
self.assertAlmostEqual(1.234567893, ujson.loads(
1605-
"1.234567893"), places=places)
1606-
self.assertAlmostEqual(
1607-
1.34567893, ujson.loads("1.34567893"), places=places)
1608-
self.assertAlmostEqual(
1609-
1.4567893, ujson.loads("1.4567893"), places=places)
1610-
self.assertAlmostEqual(
1611-
1.567893, ujson.loads("1.567893"), places=places)
1612-
self.assertAlmostEqual(1.67893, ujson.loads("1.67893"), places=places)
1613-
self.assertAlmostEqual(1.7893, ujson.loads("1.7893"), places=places)
1614-
self.assertAlmostEqual(1.893, ujson.loads("1.893"), places=places)
1615-
self.assertAlmostEqual(1.3, ujson.loads("1.3"), places=places)
1586+
tm.assert_almost_equal(-1.1234567893,
1587+
ujson.loads("-1.1234567893"),
1588+
check_less_precise=places)
1589+
tm.assert_almost_equal(-1.234567893,
1590+
ujson.loads("-1.234567893"),
1591+
check_less_precise=places)
1592+
tm.assert_almost_equal(-1.34567893,
1593+
ujson.loads("-1.34567893"),
1594+
check_less_precise=places)
1595+
tm.assert_almost_equal(-1.4567893,
1596+
ujson.loads("-1.4567893"),
1597+
check_less_precise=places)
1598+
tm.assert_almost_equal(-1.567893,
1599+
ujson.loads("-1.567893"),
1600+
check_less_precise=places)
1601+
tm.assert_almost_equal(-1.67893,
1602+
ujson.loads("-1.67893"),
1603+
check_less_precise=places)
1604+
tm.assert_almost_equal(-1.7893, ujson.loads("-1.7893"),
1605+
check_less_precise=places)
1606+
tm.assert_almost_equal(-1.893, ujson.loads("-1.893"),
1607+
check_less_precise=places)
1608+
tm.assert_almost_equal(-1.3, ujson.loads("-1.3"),
1609+
check_less_precise=places)
1610+
1611+
tm.assert_almost_equal(1.1234567893, ujson.loads(
1612+
"1.1234567893"), check_less_precise=places)
1613+
tm.assert_almost_equal(1.234567893, ujson.loads(
1614+
"1.234567893"), check_less_precise=places)
1615+
tm.assert_almost_equal(
1616+
1.34567893, ujson.loads("1.34567893"), check_less_precise=places)
1617+
tm.assert_almost_equal(
1618+
1.4567893, ujson.loads("1.4567893"), check_less_precise=places)
1619+
tm.assert_almost_equal(
1620+
1.567893, ujson.loads("1.567893"), check_less_precise=places)
1621+
tm.assert_almost_equal(1.67893, ujson.loads("1.67893"),
1622+
check_less_precise=places)
1623+
tm.assert_almost_equal(1.7893, ujson.loads("1.7893"),
1624+
check_less_precise=places)
1625+
tm.assert_almost_equal(1.893, ujson.loads("1.893"),
1626+
check_less_precise=places)
1627+
tm.assert_almost_equal(1.3, ujson.loads("1.3"),
1628+
check_less_precise=places)
16161629

16171630
def test_encodeBigSet(self):
16181631
s = set()

pandas/tests/plotting/common.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,10 @@ def _check_ticks_props(self, axes, xlabelsize=None, xrot=None,
292292

293293
for label in labels:
294294
if xlabelsize is not None:
295-
self.assertAlmostEqual(label.get_fontsize(),
295+
tm.assert_almost_equal(label.get_fontsize(),
296296
xlabelsize)
297297
if xrot is not None:
298-
self.assertAlmostEqual(label.get_rotation(), xrot)
298+
tm.assert_almost_equal(label.get_rotation(), xrot)
299299

300300
if ylabelsize or yrot:
301301
if isinstance(ax.yaxis.get_minor_formatter(), NullFormatter):
@@ -306,10 +306,10 @@ def _check_ticks_props(self, axes, xlabelsize=None, xrot=None,
306306

307307
for label in labels:
308308
if ylabelsize is not None:
309-
self.assertAlmostEqual(label.get_fontsize(),
309+
tm.assert_almost_equal(label.get_fontsize(),
310310
ylabelsize)
311311
if yrot is not None:
312-
self.assertAlmostEqual(label.get_rotation(), yrot)
312+
tm.assert_almost_equal(label.get_rotation(), yrot)
313313

314314
def _check_ax_scales(self, axes, xaxis='linear', yaxis='linear'):
315315
"""

pandas/tests/plotting/test_frame.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,8 @@ def _check_bar_alignment(self, df, kind='bar', stacked=False,
10361036

10371037
# GH 7498
10381038
# compare margins between lim and bar edges
1039-
self.assertAlmostEqual(ax_min, min_edge - 0.25)
1040-
self.assertAlmostEqual(ax_max, max_edge + 0.25)
1039+
tm.assert_almost_equal(ax_min, min_edge - 0.25)
1040+
tm.assert_almost_equal(ax_max, max_edge + 0.25)
10411041

10421042
p = ax.patches[0]
10431043
if kind == 'bar' and (stacked is True or subplots is True):
@@ -1061,10 +1061,10 @@ def _check_bar_alignment(self, df, kind='bar', stacked=False,
10611061

10621062
if align == 'center':
10631063
# Check whether the bar locates on center
1064-
self.assertAlmostEqual(axis.get_ticklocs()[0], center)
1064+
tm.assert_almost_equal(axis.get_ticklocs()[0], center)
10651065
elif align == 'edge':
10661066
# Check whether the bar's edge starts from the tick
1067-
self.assertAlmostEqual(axis.get_ticklocs()[0], edge)
1067+
tm.assert_almost_equal(axis.get_ticklocs()[0], edge)
10681068
else:
10691069
raise ValueError
10701070

@@ -1314,13 +1314,13 @@ def test_hist_df(self):
13141314
ax = series.plot.hist(normed=True, cumulative=True, bins=4)
13151315
# height of last bin (index 5) must be 1.0
13161316
rects = [x for x in ax.get_children() if isinstance(x, Rectangle)]
1317-
self.assertAlmostEqual(rects[-1].get_height(), 1.0)
1317+
tm.assert_almost_equal(rects[-1].get_height(), 1.0)
13181318
tm.close()
13191319

13201320
ax = series.plot.hist(cumulative=True, bins=4)
13211321
rects = [x for x in ax.get_children() if isinstance(x, Rectangle)]
13221322

1323-
self.assertAlmostEqual(rects[-2].get_height(), 100.0)
1323+
tm.assert_almost_equal(rects[-2].get_height(), 100.0)
13241324
tm.close()
13251325

13261326
# if horizontal, yticklabels are rotated

pandas/tests/plotting/test_hist_method.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def test_hist_df_legacy(self):
196196
ax = ser.hist(normed=True, cumulative=True, bins=4)
197197
# height of last bin (index 5) must be 1.0
198198
rects = [x for x in ax.get_children() if isinstance(x, Rectangle)]
199-
self.assertAlmostEqual(rects[-1].get_height(), 1.0)
199+
tm.assert_almost_equal(rects[-1].get_height(), 1.0)
200200

201201
tm.close()
202202
ax = ser.hist(log=True)
@@ -286,7 +286,7 @@ def test_grouped_hist_legacy(self):
286286
for ax in axes.ravel():
287287
rects = [x for x in ax.get_children() if isinstance(x, Rectangle)]
288288
height = rects[-1].get_height()
289-
self.assertAlmostEqual(height, 1.0)
289+
tm.assert_almost_equal(height, 1.0)
290290
self._check_ticks_props(axes, xlabelsize=xf, xrot=xrot,
291291
ylabelsize=yf, yrot=yrot)
292292

pandas/tests/plotting/test_series.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ def test_bar_log(self):
222222
ymin = 0.0007943282347242822 if self.mpl_ge_2_0_0 else 0.001
223223
ymax = 0.12589254117941673 if self.mpl_ge_2_0_0 else .10000000000000001
224224
res = ax.get_ylim()
225-
self.assertAlmostEqual(res[0], ymin)
226-
self.assertAlmostEqual(res[1], ymax)
225+
tm.assert_almost_equal(res[0], ymin)
226+
tm.assert_almost_equal(res[1], ymax)
227227
tm.assert_numpy_array_equal(ax.yaxis.get_ticklocs(), expected)
228228
tm.close()
229229

230230
ax = Series([0.1, 0.01, 0.001]).plot(log=True, kind='barh')
231231
res = ax.get_xlim()
232-
self.assertAlmostEqual(res[0], ymin)
233-
self.assertAlmostEqual(res[1], ymax)
232+
tm.assert_almost_equal(res[0], ymin)
233+
tm.assert_almost_equal(res[1], ymax)
234234
tm.assert_numpy_array_equal(ax.xaxis.get_ticklocs(), expected)
235235

236236
@slow

pandas/tests/series/test_analytics.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def test_median(self):
123123

124124
# test with integers, test failure
125125
int_ts = Series(np.ones(10, dtype=int), index=lrange(10))
126-
self.assertAlmostEqual(np.median(int_ts), int_ts.median())
126+
tm.assert_almost_equal(np.median(int_ts), int_ts.median())
127127

128128
def test_mode(self):
129129
# No mode should be found.
@@ -298,7 +298,7 @@ def test_kurt(self):
298298
labels=[[0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 1, 2],
299299
[0, 1, 0, 1, 0, 1]])
300300
s = Series(np.random.randn(6), index=index)
301-
self.assertAlmostEqual(s.kurt(), s.kurt(level=0)['bar'])
301+
tm.assert_almost_equal(s.kurt(), s.kurt(level=0)['bar'])
302302

303303
# test corner cases, kurt() returns NaN unless there's at least 4
304304
# values
@@ -743,10 +743,10 @@ def test_corr(self):
743743
import scipy.stats as stats
744744

745745
# full overlap
746-
self.assertAlmostEqual(self.ts.corr(self.ts), 1)
746+
tm.assert_almost_equal(self.ts.corr(self.ts), 1)
747747

748748
# partial overlap
749-
self.assertAlmostEqual(self.ts[:15].corr(self.ts[5:]), 1)
749+
tm.assert_almost_equal(self.ts[:15].corr(self.ts[5:]), 1)
750750

751751
assert isnull(self.ts[:15].corr(self.ts[5:], min_periods=12))
752752

@@ -766,7 +766,7 @@ def test_corr(self):
766766
B = tm.makeTimeSeries()
767767
result = A.corr(B)
768768
expected, _ = stats.pearsonr(A, B)
769-
self.assertAlmostEqual(result, expected)
769+
tm.assert_almost_equal(result, expected)
770770

771771
def test_corr_rank(self):
772772
tm._skip_if_no_scipy()
@@ -780,11 +780,11 @@ def test_corr_rank(self):
780780
A[-5:] = A[:5]
781781
result = A.corr(B, method='kendall')
782782
expected = stats.kendalltau(A, B)[0]
783-
self.assertAlmostEqual(result, expected)
783+
tm.assert_almost_equal(result, expected)
784784

785785
result = A.corr(B, method='spearman')
786786
expected = stats.spearmanr(A, B)[0]
787-
self.assertAlmostEqual(result, expected)
787+
tm.assert_almost_equal(result, expected)
788788

789789
# these methods got rewritten in 0.8
790790
if scipy.__version__ < LooseVersion('0.9'):
@@ -800,15 +800,15 @@ def test_corr_rank(self):
800800
1.17258718, -1.06009347, -0.10222060, -0.89076239, 0.89372375])
801801
kexp = 0.4319297
802802
sexp = 0.5853767
803-
self.assertAlmostEqual(A.corr(B, method='kendall'), kexp)
804-
self.assertAlmostEqual(A.corr(B, method='spearman'), sexp)
803+
tm.assert_almost_equal(A.corr(B, method='kendall'), kexp)
804+
tm.assert_almost_equal(A.corr(B, method='spearman'), sexp)
805805

806806
def test_cov(self):
807807
# full overlap
808-
self.assertAlmostEqual(self.ts.cov(self.ts), self.ts.std() ** 2)
808+
tm.assert_almost_equal(self.ts.cov(self.ts), self.ts.std() ** 2)
809809

810810
# partial overlap
811-
self.assertAlmostEqual(self.ts[:15].cov(self.ts[5:]),
811+
tm.assert_almost_equal(self.ts[:15].cov(self.ts[5:]),
812812
self.ts[5:15].std() ** 2)
813813

814814
# No overlap

pandas/tests/series/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ def test_getitem_setitem_integers(self):
558558

559559
assert s.iloc[0] == s['a']
560560
s.iloc[0] = 5
561-
self.assertAlmostEqual(s['a'], 5)
561+
tm.assert_almost_equal(s['a'], 5)
562562

563563
def test_getitem_box_float64(self):
564564
value = self.ts[5]

pandas/tests/test_nanops.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ def test_all_finite(self):
922922

923923
def test_ground_truth(self):
924924
skew = nanops.nanskew(self.samples)
925-
self.assertAlmostEqual(skew, self.actual_skew)
925+
tm.assert_almost_equal(skew, self.actual_skew)
926926

927927
def test_axis(self):
928928
samples = np.vstack([self.samples,
@@ -972,7 +972,7 @@ def test_all_finite(self):
972972

973973
def test_ground_truth(self):
974974
kurt = nanops.nankurt(self.samples)
975-
self.assertAlmostEqual(kurt, self.actual_kurt)
975+
tm.assert_almost_equal(kurt, self.actual_kurt)
976976

977977
def test_axis(self):
978978
samples = np.vstack([self.samples,

pandas/tests/test_window.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ def get_result(obj, window, min_periods=None, freq=None, center=False):
14061406
trunc_series = self.series[::2].truncate(prev_date, last_date)
14071407
trunc_frame = self.frame[::2].truncate(prev_date, last_date)
14081408

1409-
self.assertAlmostEqual(series_result[-1],
1409+
tm.assert_almost_equal(series_result[-1],
14101410
static_comp(trunc_series))
14111411

14121412
tm.assert_series_equal(frame_result.xs(last_date),

pandas/tests/tseries/test_timezones.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ def test_string_index_alias_tz_aware(self):
729729
ts = Series(np.random.randn(len(rng)), index=rng)
730730

731731
result = ts['1/3/2000']
732-
self.assertAlmostEqual(result, ts[2])
732+
tm.assert_almost_equal(result, ts[2])
733733

734734
def test_fixed_offset(self):
735735
dates = [datetime(2000, 1, 1, tzinfo=fixed_off),

pandas/util/testing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ def round_trip_pickle(obj, path=None):
140140
def assert_almost_equal(left, right, check_exact=False,
141141
check_dtype='equiv', check_less_precise=False,
142142
**kwargs):
143-
"""Check that left and right Index are equal.
143+
"""
144+
Check that the left and right objects are approximately equal.
144145
145146
Parameters
146147
----------

0 commit comments

Comments
 (0)