Skip to content

Commit 2c987d2

Browse files
committed
MAINT: Clean up pandas/util/testing.py
Transform testing methods to use more pytest idiom.
1 parent 8809b04 commit 2c987d2

32 files changed

+108
-178
lines changed

pandas/tests/groupby/test_timegrouper.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,7 @@ def test_groupby_with_timezone_selection(self):
571571
def test_timezone_info(self):
572572
# GH 11682
573573
# Timezone info lost when broadcasting scalar datetime to DataFrame
574-
tm._skip_if_no_pytz()
575-
import pytz
574+
pytz = pytest.importorskip("pytz")
576575

577576
df = pd.DataFrame({'a': [1], 'b': [datetime.now(pytz.utc)]})
578577
assert df['b'][0].tzinfo == pytz.utc

pandas/tests/indexes/datetimes/test_astype.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_astype_raises(self):
124124
pytest.raises(ValueError, idx.astype, 'datetime64[D]')
125125

126126
def test_index_convert_to_datetime_array(self):
127-
tm._skip_if_no_pytz()
127+
pytest.importorskip("pytz")
128128

129129
def _check_rng(rng):
130130
converted = rng.to_pydatetime()
@@ -143,8 +143,7 @@ def _check_rng(rng):
143143
_check_rng(rng_utc)
144144

145145
def test_index_convert_to_datetime_array_explicit_pytz(self):
146-
tm._skip_if_no_pytz()
147-
import pytz
146+
pytz = pytest.importorskip("pytz")
148147

149148
def _check_rng(rng):
150149
converted = rng.to_pydatetime()
@@ -164,8 +163,7 @@ def _check_rng(rng):
164163
_check_rng(rng_utc)
165164

166165
def test_index_convert_to_datetime_array_dateutil(self):
167-
tm._skip_if_no_dateutil()
168-
import dateutil
166+
dateutil = pytest.importorskip("dateutil")
169167

170168
def _check_rng(rng):
171169
converted = rng.to_pydatetime()
@@ -209,7 +207,7 @@ def test_to_period_microsecond(self):
209207
assert period[1] == Period('2007-01-01 10:11:13.789123Z', 'U')
210208

211209
def test_to_period_tz_pytz(self):
212-
tm._skip_if_no_pytz()
210+
pytest.importorskip("pytz")
213211
from dateutil.tz import tzlocal
214212
from pytz import utc as UTC
215213

@@ -240,8 +238,7 @@ def test_to_period_tz_pytz(self):
240238
tm.assert_index_equal(ts.to_period(), xp)
241239

242240
def test_to_period_tz_explicit_pytz(self):
243-
tm._skip_if_no_pytz()
244-
import pytz
241+
pytz = pytest.importorskip("pytz")
245242
from dateutil.tz import tzlocal
246243

247244
xp = date_range('1/1/2000', '4/1/2000').to_period()
@@ -271,8 +268,7 @@ def test_to_period_tz_explicit_pytz(self):
271268
tm.assert_index_equal(ts.to_period(), xp)
272269

273270
def test_to_period_tz_dateutil(self):
274-
tm._skip_if_no_dateutil()
275-
import dateutil
271+
dateutil = pytest.importorskip("dateutil")
276272
from dateutil.tz import tzlocal
277273

278274
xp = date_range('1/1/2000', '4/1/2000').to_period()

pandas/tests/indexes/datetimes/test_construction.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ def test_constructor_coverage(self):
350350
pytest.raises(ValueError, DatetimeIndex, periods=10, freq='D')
351351

352352
def test_constructor_datetime64_tzformat(self):
353-
# GH 6572
354-
tm._skip_if_no_pytz()
355-
import pytz
353+
# see gh-6572
354+
pytz = pytest.importorskip("pytz")
355+
356356
# ISO 8601 format results in pytz.FixedOffset
357357
for freq in ['AS', 'W-SUN']:
358358
idx = date_range('2013-01-01T00:00:00-05:00',
@@ -376,7 +376,7 @@ def test_constructor_datetime64_tzformat(self):
376376
tz='Asia/Tokyo')
377377
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
378378

379-
tm._skip_if_no_dateutil()
379+
pytest.importorskip("dateutil")
380380

381381
# Non ISO 8601 format results in dateutil.tz.tzoffset
382382
for freq in ['AS', 'W-SUN']:

pandas/tests/indexes/datetimes/test_date_range.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def test_range_bug(self):
300300

301301
def test_range_tz_pytz(self):
302302
# GH 2906
303-
tm._skip_if_no_pytz()
303+
pytest.importorskip("pytz")
304304
from pytz import timezone
305305

306306
tz = timezone('US/Eastern')
@@ -324,7 +324,7 @@ def test_range_tz_pytz(self):
324324

325325
def test_range_tz_dst_straddle_pytz(self):
326326

327-
tm._skip_if_no_pytz()
327+
pytest.importorskip("pytz")
328328
from pytz import timezone
329329
tz = timezone('US/Eastern')
330330
dates = [(tz.localize(datetime(2014, 3, 6)),
@@ -349,8 +349,9 @@ def test_range_tz_dst_straddle_pytz(self):
349349
assert np.all(dr.hour == 0)
350350

351351
def test_range_tz_dateutil(self):
352-
# GH 2906
353-
tm._skip_if_no_dateutil()
352+
# see gh-2906
353+
pytest.importorskip("dateutil")
354+
354355
# Use maybe_get_tz to fix filename in tz under dateutil.
355356
from pandas._libs.tslib import maybe_get_tz
356357
tz = lambda x: maybe_get_tz('dateutil/' + x)

pandas/tests/indexes/datetimes/test_datetime.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,8 @@ def test_map(self):
364364

365365
def test_iteration_preserves_tz(self):
366366

367-
tm._skip_if_no_dateutil()
368-
369-
# GH 8890
370-
import dateutil
367+
# see gh-8890
368+
dateutil = pytest.importorskip("dateutil")
371369
index = date_range("2012-01-01", periods=3, freq='H', tz='US/Eastern')
372370

373371
for i, ts in enumerate(index):

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ def test_insert(self):
9797
assert result.name == expected.name
9898
assert result.freq is None
9999

100-
# GH 7299
101-
tm._skip_if_no_pytz()
102-
import pytz
100+
# see gh-7299
101+
pytz = pytest.importorskip("pytz")
103102

104103
idx = date_range('1/1/2000', periods=3, freq='D', tz='Asia/Tokyo',
105104
name='idx')

pandas/tests/indexes/datetimes/test_ops.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,13 +1177,11 @@ def test_summary(self):
11771177
self.rng[2:2].summary()
11781178

11791179
def test_summary_pytz(self):
1180-
tm._skip_if_no_pytz()
1181-
import pytz
1180+
pytz = pytest.importorskip("pytz")
11821181
bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()
11831182

11841183
def test_summary_dateutil(self):
1185-
tm._skip_if_no_dateutil()
1186-
import dateutil
1184+
dateutil = pytest.importorskip("dateutil")
11871185
bdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()
11881186

11891187
def test_equals(self):
@@ -1279,13 +1277,11 @@ def test_summary(self):
12791277
self.rng[2:2].summary()
12801278

12811279
def test_summary_pytz(self):
1282-
tm._skip_if_no_pytz()
1283-
import pytz
1280+
pytz = pytest.importorskip("pytz")
12841281
cdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()
12851282

12861283
def test_summary_dateutil(self):
1287-
tm._skip_if_no_dateutil()
1288-
import dateutil
1284+
dateutil = pytest.importorskip("dateutil")
12891285
cdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()
12901286

12911287
def test_equals(self):

pandas/tests/indexes/datetimes/test_setops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from datetime import datetime
22

3+
import pytest
34
import numpy as np
45

56
import pandas as pd
@@ -306,7 +307,7 @@ def test_intersection_bug(self):
306307
tm.assert_index_equal(result, b)
307308

308309
def test_month_range_union_tz_pytz(self):
309-
tm._skip_if_no_pytz()
310+
pytest.importorskip("pytz")
310311
from pytz import timezone
311312
tz = timezone('US/Eastern')
312313

@@ -325,7 +326,7 @@ def test_month_range_union_tz_pytz(self):
325326

326327
def test_month_range_union_tz_dateutil(self):
327328
tm._skip_if_windows_python_3()
328-
tm._skip_if_no_dateutil()
329+
pytest.importorskip("dateutil")
329330
from pandas._libs.tslib import _dateutil_gettz as timezone
330331
tz = timezone('US/Eastern')
331332

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,8 @@ def test_to_datetime_tz(self):
245245

246246
def test_to_datetime_tz_pytz(self):
247247

248-
# xref 8260
249-
tm._skip_if_no_pytz()
250-
import pytz
248+
# see gh-8260
249+
pytz = pytest.importorskip("pytz")
251250

252251
us_eastern = pytz.timezone('US/Eastern')
253252
arr = np.array([us_eastern.localize(datetime(year=2000, month=1, day=1,
@@ -1124,8 +1123,6 @@ def test_parsers_quarter_invalid(self):
11241123
pytest.raises(ValueError, tools.parse_time_string, case)
11251124

11261125
def test_parsers_dayfirst_yearfirst(self):
1127-
tm._skip_if_no_dateutil()
1128-
11291126
# OK
11301127
# 2.5.1 10-11-12 [dayfirst=0, yearfirst=0] -> 2012-10-11 00:00:00
11311128
# 2.5.2 10-11-12 [dayfirst=0, yearfirst=1] -> 2012-10-11 00:00:00
@@ -1166,7 +1163,7 @@ def test_parsers_dayfirst_yearfirst(self):
11661163
# 2.5.2 20/12/21 [dayfirst=1, yearfirst=0] -> 2021-12-20 00:00:00
11671164
# 2.5.3 20/12/21 [dayfirst=1, yearfirst=0] -> 2021-12-20 00:00:00
11681165

1169-
import dateutil
1166+
dateutil = pytest.importorskip("dateutil")
11701167
is_lt_253 = dateutil.__version__ < LooseVersion('2.5.3')
11711168

11721169
# str : dayfirst, yearfirst, expected
@@ -1221,7 +1218,7 @@ def test_parsers_dayfirst_yearfirst(self):
12211218
assert result4 == expected
12221219

12231220
def test_parsers_timestring(self):
1224-
tm._skip_if_no_dateutil()
1221+
pytest.importorskip("dateutil")
12251222
from dateutil.parser import parse
12261223

12271224
# must be the same as dateutil result

pandas/tests/io/formats/test_format.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,17 +1548,15 @@ def get_ipython():
15481548

15491549
def test_pprint_pathological_object(self):
15501550
"""
1551-
if the test fails, the stack will overflow and nose crash,
1552-
but it won't hang.
1551+
If the test fails, it at least won't hang.
15531552
"""
15541553

15551554
class A:
1556-
15571555
def __getitem__(self, key):
15581556
return 3 # obviously simplified
15591557

15601558
df = DataFrame([A()])
1561-
repr(df) # just don't dine
1559+
repr(df) # just don't die
15621560

15631561
def test_float_trim_zeros(self):
15641562
vals = [2.08430917305e+10, 3.52205017305e+10, 2.30674817305e+10,
@@ -2508,7 +2506,7 @@ def test_no_tz(self):
25082506
assert str(ts_nanos_micros) == "1970-01-01 00:00:00.000001200"
25092507

25102508
def test_tz_pytz(self):
2511-
tm._skip_if_no_pytz()
2509+
pytest.importorskip("pytz")
25122510

25132511
import pytz
25142512

@@ -2522,8 +2520,7 @@ def test_tz_pytz(self):
25222520
assert str(dt_datetime_us) == str(Timestamp(dt_datetime_us))
25232521

25242522
def test_tz_dateutil(self):
2525-
tm._skip_if_no_dateutil()
2526-
import dateutil
2523+
dateutil = pytest.importorskip("dateutil")
25272524
utc = dateutil.tz.tzutc()
25282525

25292526
dt_date = datetime(2013, 1, 2, tzinfo=utc)

0 commit comments

Comments
 (0)