Skip to content

Commit d50f981

Browse files
gfyoungjreback
authored andcommitted
MAINT: Clean up pandas/util/testing.py (#16271)
Transform testing methods to use more pytest idiom. Also, assume that dateutil and pytz are installed for testing because there are core dependencies for pandas.
1 parent ea56550 commit d50f981

32 files changed

+90
-259
lines changed

pandas/tests/groupby/test_timegrouper.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
""" test with the TimeGrouper / grouping with datetimes """
22

33
import pytest
4+
import pytz
45

56
from datetime import datetime
67
import numpy as np
@@ -569,10 +570,8 @@ def test_groupby_with_timezone_selection(self):
569570
tm.assert_series_equal(df1, df2)
570571

571572
def test_timezone_info(self):
572-
# GH 11682
573-
# Timezone info lost when broadcasting scalar datetime to DataFrame
574-
tm._skip_if_no_pytz()
575-
import pytz
573+
# see gh-11682: Timezone info lost when broadcasting
574+
# scalar datetime to DataFrame
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

+4-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import pytest
22

3+
import pytz
4+
import dateutil
35
import numpy as np
46

57
from datetime import datetime
8+
from dateutil.tz import tzlocal
9+
610
import pandas as pd
711
import pandas.util.testing as tm
812
from pandas import (DatetimeIndex, date_range, Series, NaT, Index, Timestamp,
@@ -124,8 +128,6 @@ def test_astype_raises(self):
124128
pytest.raises(ValueError, idx.astype, 'datetime64[D]')
125129

126130
def test_index_convert_to_datetime_array(self):
127-
tm._skip_if_no_pytz()
128-
129131
def _check_rng(rng):
130132
converted = rng.to_pydatetime()
131133
assert isinstance(converted, np.ndarray)
@@ -143,9 +145,6 @@ def _check_rng(rng):
143145
_check_rng(rng_utc)
144146

145147
def test_index_convert_to_datetime_array_explicit_pytz(self):
146-
tm._skip_if_no_pytz()
147-
import pytz
148-
149148
def _check_rng(rng):
150149
converted = rng.to_pydatetime()
151150
assert isinstance(converted, np.ndarray)
@@ -164,9 +163,6 @@ 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
169-
170166
def _check_rng(rng):
171167
converted = rng.to_pydatetime()
172168
assert isinstance(converted, np.ndarray)
@@ -209,8 +205,6 @@ def test_to_period_microsecond(self):
209205
assert period[1] == Period('2007-01-01 10:11:13.789123Z', 'U')
210206

211207
def test_to_period_tz_pytz(self):
212-
tm._skip_if_no_pytz()
213-
from dateutil.tz import tzlocal
214208
from pytz import utc as UTC
215209

216210
xp = date_range('1/1/2000', '4/1/2000').to_period()
@@ -240,10 +234,6 @@ def test_to_period_tz_pytz(self):
240234
tm.assert_index_equal(ts.to_period(), xp)
241235

242236
def test_to_period_tz_explicit_pytz(self):
243-
tm._skip_if_no_pytz()
244-
import pytz
245-
from dateutil.tz import tzlocal
246-
247237
xp = date_range('1/1/2000', '4/1/2000').to_period()
248238

249239
ts = date_range('1/1/2000', '4/1/2000', tz=pytz.timezone('US/Eastern'))
@@ -271,10 +261,6 @@ def test_to_period_tz_explicit_pytz(self):
271261
tm.assert_index_equal(ts.to_period(), xp)
272262

273263
def test_to_period_tz_dateutil(self):
274-
tm._skip_if_no_dateutil()
275-
import dateutil
276-
from dateutil.tz import tzlocal
277-
278264
xp = date_range('1/1/2000', '4/1/2000').to_period()
279265

280266
ts = date_range('1/1/2000', '4/1/2000', tz='dateutil/US/Eastern')

pandas/tests/indexes/datetimes/test_construction.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
import pytz
34
import numpy as np
45
from datetime import timedelta
56

@@ -350,10 +351,7 @@ def test_constructor_coverage(self):
350351
pytest.raises(ValueError, DatetimeIndex, periods=10, freq='D')
351352

352353
def test_constructor_datetime64_tzformat(self):
353-
# GH 6572
354-
tm._skip_if_no_pytz()
355-
import pytz
356-
# ISO 8601 format results in pytz.FixedOffset
354+
# see gh-6572: ISO 8601 format results in pytz.FixedOffset
357355
for freq in ['AS', 'W-SUN']:
358356
idx = date_range('2013-01-01T00:00:00-05:00',
359357
'2016-01-01T23:59:59-05:00', freq=freq)
@@ -376,8 +374,6 @@ def test_constructor_datetime64_tzformat(self):
376374
tz='Asia/Tokyo')
377375
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)
378376

379-
tm._skip_if_no_dateutil()
380-
381377
# Non ISO 8601 format results in dateutil.tz.tzoffset
382378
for freq in ['AS', 'W-SUN']:
383379
idx = date_range('2013/1/1 0:00:00-5:00', '2016/1/1 23:59:59-5:00',

pandas/tests/indexes/datetimes/test_date_range.py

+4-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
import numpy as np
9+
from pytz import timezone
910
from datetime import datetime, timedelta, time
1011

1112
import pandas as pd
@@ -299,10 +300,7 @@ def test_range_bug(self):
299300
tm.assert_index_equal(result, DatetimeIndex(exp_values))
300301

301302
def test_range_tz_pytz(self):
302-
# GH 2906
303-
tm._skip_if_no_pytz()
304-
from pytz import timezone
305-
303+
# see gh-2906
306304
tz = timezone('US/Eastern')
307305
start = tz.localize(datetime(2011, 1, 1))
308306
end = tz.localize(datetime(2011, 1, 3))
@@ -323,9 +321,6 @@ def test_range_tz_pytz(self):
323321
assert dr[2] == end
324322

325323
def test_range_tz_dst_straddle_pytz(self):
326-
327-
tm._skip_if_no_pytz()
328-
from pytz import timezone
329324
tz = timezone('US/Eastern')
330325
dates = [(tz.localize(datetime(2014, 3, 6)),
331326
tz.localize(datetime(2014, 3, 12))),
@@ -349,8 +344,8 @@ def test_range_tz_dst_straddle_pytz(self):
349344
assert np.all(dr.hour == 0)
350345

351346
def test_range_tz_dateutil(self):
352-
# GH 2906
353-
tm._skip_if_no_dateutil()
347+
# see gh-2906
348+
354349
# Use maybe_get_tz to fix filename in tz under dateutil.
355350
from pandas._libs.tslib import maybe_get_tz
356351
tz = lambda x: maybe_get_tz('dateutil/' + x)

pandas/tests/indexes/datetimes/test_datetime.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import numpy as np
44
from datetime import date, timedelta, time
55

6+
import dateutil
67
import pandas as pd
78
import pandas.util.testing as tm
89
from pandas.compat import lrange
@@ -363,11 +364,7 @@ def test_map(self):
363364
tm.assert_index_equal(result, exp)
364365

365366
def test_iteration_preserves_tz(self):
366-
367-
tm._skip_if_no_dateutil()
368-
369-
# GH 8890
370-
import dateutil
367+
# see gh-8890
371368
index = date_range("2012-01-01", periods=3, freq='H', tz='US/Eastern')
372369

373370
for i, ts in enumerate(index):

pandas/tests/indexes/datetimes/test_indexing.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22

3+
import pytz
34
import numpy as np
45
import pandas as pd
56
import pandas.util.testing as tm
@@ -97,10 +98,7 @@ def test_insert(self):
9798
assert result.name == expected.name
9899
assert result.freq is None
99100

100-
# GH 7299
101-
tm._skip_if_no_pytz()
102-
import pytz
103-
101+
# see gh-7299
104102
idx = date_range('1/1/2000', periods=3, freq='D', tz='Asia/Tokyo',
105103
name='idx')
106104
with pytest.raises(ValueError):

pandas/tests/indexes/datetimes/test_ops.py

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import pytz
12
import pytest
3+
import dateutil
24
import warnings
35
import numpy as np
46
from datetime import timedelta
@@ -1177,13 +1179,9 @@ def test_summary(self):
11771179
self.rng[2:2].summary()
11781180

11791181
def test_summary_pytz(self):
1180-
tm._skip_if_no_pytz()
1181-
import pytz
11821182
bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()
11831183

11841184
def test_summary_dateutil(self):
1185-
tm._skip_if_no_dateutil()
1186-
import dateutil
11871185
bdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()
11881186

11891187
def test_equals(self):
@@ -1279,13 +1277,9 @@ 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
12841280
cdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()
12851281

12861282
def test_summary_dateutil(self):
1287-
tm._skip_if_no_dateutil()
1288-
import dateutil
12891283
cdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()
12901284

12911285
def test_equals(self):

pandas/tests/indexes/datetimes/test_setops.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,6 @@ def test_intersection_bug(self):
306306
tm.assert_index_equal(result, b)
307307

308308
def test_month_range_union_tz_pytz(self):
309-
tm._skip_if_no_pytz()
310309
from pytz import timezone
311310
tz = timezone('US/Eastern')
312311

@@ -325,7 +324,7 @@ def test_month_range_union_tz_pytz(self):
325324

326325
def test_month_range_union_tz_dateutil(self):
327326
tm._skip_if_windows_python_3()
328-
tm._skip_if_no_dateutil()
327+
329328
from pandas._libs.tslib import _dateutil_gettz as timezone
330329
tz = timezone('US/Eastern')
331330

pandas/tests/indexes/datetimes/test_tools.py

+4-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
""" test to_datetime """
22

33
import sys
4+
import pytz
45
import pytest
56
import locale
67
import calendar
8+
import dateutil
79
import numpy as np
10+
from dateutil.parser import parse
811
from datetime import datetime, date, time
912
from distutils.version import LooseVersion
1013

@@ -244,11 +247,7 @@ def test_to_datetime_tz(self):
244247
pytest.raises(ValueError, lambda: pd.to_datetime(arr))
245248

246249
def test_to_datetime_tz_pytz(self):
247-
248-
# xref 8260
249-
tm._skip_if_no_pytz()
250-
import pytz
251-
250+
# see gh-8260
252251
us_eastern = pytz.timezone('US/Eastern')
253252
arr = np.array([us_eastern.localize(datetime(year=2000, month=1, day=1,
254253
hour=3, minute=0)),
@@ -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,6 @@ 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
11701166
is_lt_253 = dateutil.__version__ < LooseVersion('2.5.3')
11711167

11721168
# str : dayfirst, yearfirst, expected
@@ -1187,7 +1183,6 @@ def test_parsers_dayfirst_yearfirst(self):
11871183
(True, True,
11881184
datetime(2020, 12, 21))]}
11891185

1190-
from dateutil.parser import parse
11911186
for date_str, values in compat.iteritems(cases):
11921187
for dayfirst, yearfirst, expected in values:
11931188

@@ -1221,9 +1216,6 @@ def test_parsers_dayfirst_yearfirst(self):
12211216
assert result4 == expected
12221217

12231218
def test_parsers_timestring(self):
1224-
tm._skip_if_no_dateutil()
1225-
from dateutil.parser import parse
1226-
12271219
# must be the same as dateutil result
12281220
cases = {'10:15': (parse('10:15'), datetime(1, 1, 1, 10, 15)),
12291221
'9:05': (parse('9:05'), datetime(1, 1, 1, 9, 5))}
@@ -1365,7 +1357,6 @@ def test_parsers_iso8601(self):
13651357
class TestArrayToDatetime(object):
13661358

13671359
def test_try_parse_dates(self):
1368-
from dateutil.parser import parse
13691360
arr = np.array(['5/1/2000', '6/1/2000', '7/1/2000'], dtype=object)
13701361

13711362
result = lib.try_parse_dates(arr, dayfirst=True)

pandas/tests/io/formats/test_format.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from __future__ import print_function
88
import re
99

10+
import pytz
11+
import dateutil
1012
import itertools
1113
from operator import methodcaller
1214
import os
@@ -1548,17 +1550,15 @@ def get_ipython():
15481550

15491551
def test_pprint_pathological_object(self):
15501552
"""
1551-
if the test fails, the stack will overflow and nose crash,
1552-
but it won't hang.
1553+
If the test fails, it at least won't hang.
15531554
"""
15541555

15551556
class A:
1556-
15571557
def __getitem__(self, key):
15581558
return 3 # obviously simplified
15591559

15601560
df = DataFrame([A()])
1561-
repr(df) # just don't dine
1561+
repr(df) # just don't die
15621562

15631563
def test_float_trim_zeros(self):
15641564
vals = [2.08430917305e+10, 3.52205017305e+10, 2.30674817305e+10,
@@ -2508,10 +2508,6 @@ def test_no_tz(self):
25082508
assert str(ts_nanos_micros) == "1970-01-01 00:00:00.000001200"
25092509

25102510
def test_tz_pytz(self):
2511-
tm._skip_if_no_pytz()
2512-
2513-
import pytz
2514-
25152511
dt_date = datetime(2013, 1, 2, tzinfo=pytz.utc)
25162512
assert str(dt_date) == str(Timestamp(dt_date))
25172513

@@ -2522,8 +2518,6 @@ def test_tz_pytz(self):
25222518
assert str(dt_datetime_us) == str(Timestamp(dt_datetime_us))
25232519

25242520
def test_tz_dateutil(self):
2525-
tm._skip_if_no_dateutil()
2526-
import dateutil
25272521
utc = dateutil.tz.tzutc()
25282522

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

0 commit comments

Comments
 (0)