Skip to content

Commit

Permalink
MAINT: Clean up pandas/util/testing.py
Browse files Browse the repository at this point in the history
Transform testing methods to use more
pytest idiom.
  • Loading branch information
gfyoung committed May 6, 2017
1 parent 8809b04 commit 2c987d2
Show file tree
Hide file tree
Showing 32 changed files with 108 additions and 178 deletions.
3 changes: 1 addition & 2 deletions pandas/tests/groupby/test_timegrouper.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,7 @@ def test_groupby_with_timezone_selection(self):
def test_timezone_info(self):
# GH 11682
# Timezone info lost when broadcasting scalar datetime to DataFrame
tm._skip_if_no_pytz()
import pytz
pytz = pytest.importorskip("pytz")

df = pd.DataFrame({'a': [1], 'b': [datetime.now(pytz.utc)]})
assert df['b'][0].tzinfo == pytz.utc
Expand Down
16 changes: 6 additions & 10 deletions pandas/tests/indexes/datetimes/test_astype.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def test_astype_raises(self):
pytest.raises(ValueError, idx.astype, 'datetime64[D]')

def test_index_convert_to_datetime_array(self):
tm._skip_if_no_pytz()
pytest.importorskip("pytz")

def _check_rng(rng):
converted = rng.to_pydatetime()
Expand All @@ -143,8 +143,7 @@ def _check_rng(rng):
_check_rng(rng_utc)

def test_index_convert_to_datetime_array_explicit_pytz(self):
tm._skip_if_no_pytz()
import pytz
pytz = pytest.importorskip("pytz")

def _check_rng(rng):
converted = rng.to_pydatetime()
Expand All @@ -164,8 +163,7 @@ def _check_rng(rng):
_check_rng(rng_utc)

def test_index_convert_to_datetime_array_dateutil(self):
tm._skip_if_no_dateutil()
import dateutil
dateutil = pytest.importorskip("dateutil")

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

def test_to_period_tz_pytz(self):
tm._skip_if_no_pytz()
pytest.importorskip("pytz")
from dateutil.tz import tzlocal
from pytz import utc as UTC

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

def test_to_period_tz_explicit_pytz(self):
tm._skip_if_no_pytz()
import pytz
pytz = pytest.importorskip("pytz")
from dateutil.tz import tzlocal

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

def test_to_period_tz_dateutil(self):
tm._skip_if_no_dateutil()
import dateutil
dateutil = pytest.importorskip("dateutil")
from dateutil.tz import tzlocal

xp = date_range('1/1/2000', '4/1/2000').to_period()
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/indexes/datetimes/test_construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,9 @@ def test_constructor_coverage(self):
pytest.raises(ValueError, DatetimeIndex, periods=10, freq='D')

def test_constructor_datetime64_tzformat(self):
# GH 6572
tm._skip_if_no_pytz()
import pytz
# see gh-6572
pytz = pytest.importorskip("pytz")

# ISO 8601 format results in pytz.FixedOffset
for freq in ['AS', 'W-SUN']:
idx = date_range('2013-01-01T00:00:00-05:00',
Expand All @@ -376,7 +376,7 @@ def test_constructor_datetime64_tzformat(self):
tz='Asia/Tokyo')
tm.assert_numpy_array_equal(idx.asi8, expected_i8.asi8)

tm._skip_if_no_dateutil()
pytest.importorskip("dateutil")

# Non ISO 8601 format results in dateutil.tz.tzoffset
for freq in ['AS', 'W-SUN']:
Expand Down
9 changes: 5 additions & 4 deletions pandas/tests/indexes/datetimes/test_date_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def test_range_bug(self):

def test_range_tz_pytz(self):
# GH 2906
tm._skip_if_no_pytz()
pytest.importorskip("pytz")
from pytz import timezone

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

def test_range_tz_dst_straddle_pytz(self):

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

def test_range_tz_dateutil(self):
# GH 2906
tm._skip_if_no_dateutil()
# see gh-2906
pytest.importorskip("dateutil")

# Use maybe_get_tz to fix filename in tz under dateutil.
from pandas._libs.tslib import maybe_get_tz
tz = lambda x: maybe_get_tz('dateutil/' + x)
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,8 @@ def test_map(self):

def test_iteration_preserves_tz(self):

tm._skip_if_no_dateutil()

# GH 8890
import dateutil
# see gh-8890
dateutil = pytest.importorskip("dateutil")
index = date_range("2012-01-01", periods=3, freq='H', tz='US/Eastern')

for i, ts in enumerate(index):
Expand Down
5 changes: 2 additions & 3 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,8 @@ def test_insert(self):
assert result.name == expected.name
assert result.freq is None

# GH 7299
tm._skip_if_no_pytz()
import pytz
# see gh-7299
pytz = pytest.importorskip("pytz")

idx = date_range('1/1/2000', periods=3, freq='D', tz='Asia/Tokyo',
name='idx')
Expand Down
12 changes: 4 additions & 8 deletions pandas/tests/indexes/datetimes/test_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,13 +1177,11 @@ def test_summary(self):
self.rng[2:2].summary()

def test_summary_pytz(self):
tm._skip_if_no_pytz()
import pytz
pytz = pytest.importorskip("pytz")
bdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()

def test_summary_dateutil(self):
tm._skip_if_no_dateutil()
import dateutil
dateutil = pytest.importorskip("dateutil")
bdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()

def test_equals(self):
Expand Down Expand Up @@ -1279,13 +1277,11 @@ def test_summary(self):
self.rng[2:2].summary()

def test_summary_pytz(self):
tm._skip_if_no_pytz()
import pytz
pytz = pytest.importorskip("pytz")
cdate_range('1/1/2005', '1/1/2009', tz=pytz.utc).summary()

def test_summary_dateutil(self):
tm._skip_if_no_dateutil()
import dateutil
dateutil = pytest.importorskip("dateutil")
cdate_range('1/1/2005', '1/1/2009', tz=dateutil.tz.tzutc()).summary()

def test_equals(self):
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/indexes/datetimes/test_setops.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime

import pytest
import numpy as np

import pandas as pd
Expand Down Expand Up @@ -306,7 +307,7 @@ def test_intersection_bug(self):
tm.assert_index_equal(result, b)

def test_month_range_union_tz_pytz(self):
tm._skip_if_no_pytz()
pytest.importorskip("pytz")
from pytz import timezone
tz = timezone('US/Eastern')

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

def test_month_range_union_tz_dateutil(self):
tm._skip_if_windows_python_3()
tm._skip_if_no_dateutil()
pytest.importorskip("dateutil")
from pandas._libs.tslib import _dateutil_gettz as timezone
tz = timezone('US/Eastern')

Expand Down
11 changes: 4 additions & 7 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ def test_to_datetime_tz(self):

def test_to_datetime_tz_pytz(self):

# xref 8260
tm._skip_if_no_pytz()
import pytz
# see gh-8260
pytz = pytest.importorskip("pytz")

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

def test_parsers_dayfirst_yearfirst(self):
tm._skip_if_no_dateutil()

# OK
# 2.5.1 10-11-12 [dayfirst=0, yearfirst=0] -> 2012-10-11 00:00:00
# 2.5.2 10-11-12 [dayfirst=0, yearfirst=1] -> 2012-10-11 00:00:00
Expand Down Expand Up @@ -1166,7 +1163,7 @@ def test_parsers_dayfirst_yearfirst(self):
# 2.5.2 20/12/21 [dayfirst=1, yearfirst=0] -> 2021-12-20 00:00:00
# 2.5.3 20/12/21 [dayfirst=1, yearfirst=0] -> 2021-12-20 00:00:00

import dateutil
dateutil = pytest.importorskip("dateutil")
is_lt_253 = dateutil.__version__ < LooseVersion('2.5.3')

# str : dayfirst, yearfirst, expected
Expand Down Expand Up @@ -1221,7 +1218,7 @@ def test_parsers_dayfirst_yearfirst(self):
assert result4 == expected

def test_parsers_timestring(self):
tm._skip_if_no_dateutil()
pytest.importorskip("dateutil")
from dateutil.parser import parse

# must be the same as dateutil result
Expand Down
11 changes: 4 additions & 7 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -1548,17 +1548,15 @@ def get_ipython():

def test_pprint_pathological_object(self):
"""
if the test fails, the stack will overflow and nose crash,
but it won't hang.
If the test fails, it at least won't hang.
"""

class A:

def __getitem__(self, key):
return 3 # obviously simplified

df = DataFrame([A()])
repr(df) # just don't dine
repr(df) # just don't die

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

def test_tz_pytz(self):
tm._skip_if_no_pytz()
pytest.importorskip("pytz")

import pytz

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

def test_tz_dateutil(self):
tm._skip_if_no_dateutil()
import dateutil
dateutil = pytest.importorskip("dateutil")
utc = dateutil.tz.tzutc()

dt_date = datetime(2013, 1, 2, tzinfo=utc)
Expand Down
10 changes: 4 additions & 6 deletions pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,18 +396,16 @@ def test_encodeTimeConversion(self):
assert expected == output

def test_encodeTimeConversion_pytz(self):
# GH11473 to_json segfaults with timezone-aware datetimes
tm._skip_if_no_pytz()
import pytz
# see gh-11473: to_json segfaults with timezone-aware datetimes
pytz = pytest.importorskip("pytz")
test = datetime.time(10, 12, 15, 343243, pytz.utc)
output = ujson.encode(test)
expected = '"%s"' % test.isoformat()
assert expected == output

def test_encodeTimeConversion_dateutil(self):
# GH11473 to_json segfaults with timezone-aware datetimes
tm._skip_if_no_dateutil()
import dateutil
# see gh-11473: to_json segfaults with timezone-aware datetimes
dateutil = pytest.importorskip("dateutil")
test = datetime.time(10, 12, 15, 343243, dateutil.tz.tzutc())
output = ujson.encode(test)
expected = '"%s"' % test.isoformat()
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/test_pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5243,7 +5243,7 @@ def _compare_with_tz(self, a, b):
def test_append_with_timezones_dateutil(self):

from datetime import timedelta
tm._skip_if_no_dateutil()
pytest.importorskip("dateutil")

# use maybe_get_tz instead of dateutil.tz.gettz to handle the windows
# filename issues.
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
This is a common base class used for various plotting tests
"""

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


def _skip_if_no_scipy_gaussian_kde():
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_boxplot_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

""" Test cases for .boxplot method """

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


def _skip_if_mpl_14_or_dev_boxplot():
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from pandas.tests.plotting.common import (TestPlotBase,
_skip_if_no_scipy_gaussian_kde)

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


class TestTSPlot(TestPlotBase):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
pandas.tools.plotting
"""

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


class TestDeprecatedNameSpace(TestPlotBase):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
_skip_if_no_scipy_gaussian_kde,
_ok_for_gaussian_kde)

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


class TestDataFramePlots(TestPlotBase):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from pandas.tests.plotting.common import TestPlotBase

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


class TestDataFrameGroupByPlots(TestPlotBase):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_hist_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pandas.tests.plotting.common import (TestPlotBase, _check_plot_works)


tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


class TestSeriesPlots(TestPlotBase):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pandas.tests.plotting.common import (TestPlotBase, _check_plot_works,
_ok_for_gaussian_kde)

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


class TestSeriesPlots(TestPlotBase):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/plotting/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
_skip_if_no_scipy_gaussian_kde,
_ok_for_gaussian_kde)

tm._skip_module_if_no_mpl()
tm._skip_if_no_mpl()


class TestSeriesPlots(TestPlotBase):
Expand Down
Loading

0 comments on commit 2c987d2

Please sign in to comment.