Skip to content

Created decorators for skip_if locale functions #18745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions pandas/tests/indexes/datetimes/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pandas.core.dtypes.common import is_datetime64_ns_dtype
from pandas.util import testing as tm
import pandas.util._test_decorators as td
from pandas.util.testing import assert_series_equal, _skip_if_has_locale
from pandas.util.testing import assert_series_equal
from pandas import (isna, to_datetime, Timestamp, Series, DataFrame,
Index, DatetimeIndex, NaT, date_range, compat)

Expand Down Expand Up @@ -144,11 +144,10 @@ def test_to_datetime_format_time(self, cache):
for s, format, dt in data:
assert to_datetime(s, format=format, cache=cache) == dt

@td.skip_if_has_locale
@pytest.mark.parametrize('cache', [True, False])
def test_to_datetime_with_non_exact(self, cache):
# GH 10834
tm._skip_if_has_locale()

# 8904
# exact kw
if sys.version_info < (2, 7):
Expand Down Expand Up @@ -830,11 +829,10 @@ def test_to_datetime_with_space_in_series(self, cache):
result_ignore = to_datetime(s, errors='ignore', cache=cache)
tm.assert_series_equal(result_ignore, s)

@td.skip_if_has_locale
@pytest.mark.parametrize('cache', [True, False])
def test_to_datetime_with_apply(self, cache):
# this is only locale tested with US/None locales
tm._skip_if_has_locale()

# GH 5195
# with a format and coerce a single item to_datetime fails
td = Series(['May 04', 'Jun 02', 'Dec 11'], index=[1, 2, 3])
Expand Down Expand Up @@ -1023,9 +1021,9 @@ def test_dayfirst(self, cache):

class TestGuessDatetimeFormat(object):

@td.skip_if_not_us_locale
@is_dateutil_le_261
def test_guess_datetime_format_for_array(self):
tm._skip_if_not_us_locale()
expected_format = '%Y-%m-%d %H:%M:%S.%f'
dt_string = datetime(2011, 12, 30, 0, 0, 0).strftime(expected_format)

Expand All @@ -1044,9 +1042,9 @@ def test_guess_datetime_format_for_array(self):
[np.nan, np.nan, np.nan], dtype='O'))
assert format_for_string_of_nans is None

@td.skip_if_not_us_locale
@is_dateutil_gt_261
def test_guess_datetime_format_for_array_gt_261(self):
tm._skip_if_not_us_locale()
expected_format = '%Y-%m-%d %H:%M:%S.%f'
dt_string = datetime(2011, 12, 30, 0, 0, 0).strftime(expected_format)

Expand Down Expand Up @@ -1393,9 +1391,9 @@ def test_parsers_timestring(self, cache):
assert result4 == exp_now
assert result5 == exp_now

@td.skip_if_has_locale
def test_parsers_time(self):
# GH11818
_skip_if_has_locale()
strings = ["14:15", "1415", "2:15pm", "0215pm", "14:15:00", "141500",
"2:15:00pm", "021500pm", time(14, 15)]
expected = time(14, 15)
Expand Down
18 changes: 6 additions & 12 deletions pandas/tests/scalar/test_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import numpy as np
import pytest
from dateutil.parser import parse

import pandas.util._test_decorators as td
from pandas.conftest import is_dateutil_le_261, is_dateutil_gt_261
from pandas import compat
from pandas.util import testing as tm
Expand Down Expand Up @@ -66,6 +68,7 @@ def test_parsers_monthfreq(self):

class TestGuessDatetimeFormat(object):

@td.skip_if_not_us_locale
@is_dateutil_le_261
@pytest.mark.parametrize(
"string, format",
Expand All @@ -79,11 +82,10 @@ class TestGuessDatetimeFormat(object):
'%Y-%m-%d %H:%M:%S.%f')])
def test_guess_datetime_format_with_parseable_formats(
self, string, format):
tm._skip_if_not_us_locale()

result = parsing._guess_datetime_format(string)
assert result == format

@td.skip_if_not_us_locale
@is_dateutil_gt_261
@pytest.mark.parametrize(
"string",
Expand All @@ -92,8 +94,6 @@ def test_guess_datetime_format_with_parseable_formats(
'2011-12-30 00:00:00.000000'])
def test_guess_datetime_format_with_parseable_formats_gt_261(
self, string):
tm._skip_if_not_us_locale()

result = parsing._guess_datetime_format(string)
assert result is None

Expand All @@ -118,6 +118,7 @@ def test_guess_datetime_format_with_dayfirst_gt_261(self, dayfirst):
ambiguous_string, dayfirst=dayfirst)
assert result is None

@td.skip_if_has_locale
@is_dateutil_le_261
@pytest.mark.parametrize(
"string, format",
Expand All @@ -127,13 +128,10 @@ def test_guess_datetime_format_with_dayfirst_gt_261(self, dayfirst):
('30/Dec/2011 00:00:00', '%d/%b/%Y %H:%M:%S')])
def test_guess_datetime_format_with_locale_specific_formats(
self, string, format):
# The month names will vary depending on the locale, in which
# case these wont be parsed properly (dateutil can't parse them)
tm._skip_if_has_locale()

result = parsing._guess_datetime_format(string)
assert result == format

@td.skip_if_has_locale
@is_dateutil_gt_261
@pytest.mark.parametrize(
"string",
Expand All @@ -143,10 +141,6 @@ def test_guess_datetime_format_with_locale_specific_formats(
'30/Dec/2011 00:00:00'])
def test_guess_datetime_format_with_locale_specific_formats_gt_261(
self, string):
# The month names will vary depending on the locale, in which
# case these wont be parsed properly (dateutil can't parse them)
tm._skip_if_has_locale()

result = parsing._guess_datetime_format(string)
assert result is None

Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/series/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import pandas as pd
import pandas.util.testing as tm
import pandas.util._test_decorators as td
from pandas._libs.tslib import iNaT
from pandas.compat import lrange, StringIO, product
from pandas.core.indexes.timedeltas import TimedeltaIndex
Expand All @@ -17,7 +18,7 @@
Timestamp, to_datetime, offsets,
timedelta_range)
from pandas.util.testing import (assert_series_equal, assert_almost_equal,
assert_frame_equal, _skip_if_has_locale)
assert_frame_equal)

from pandas.tests.series.common import TestData

Expand Down Expand Up @@ -738,10 +739,9 @@ def test_between_time_types(self):
pytest.raises(ValueError, series.between_time,
datetime(2010, 1, 2, 1), datetime(2010, 1, 2, 5))

@td.skip_if_has_locale
def test_between_time_formats(self):
# GH11818
_skip_if_has_locale()

rng = date_range('1/1/2000', '1/5/2000', freq='5min')
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)

Expand Down
20 changes: 20 additions & 0 deletions pandas/util/_test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def test_foo():
"""

import pytest
import locale
from distutils.version import LooseVersion

from pandas.compat import is_platform_windows, is_platform_32bit, PY3
Expand Down Expand Up @@ -81,6 +82,18 @@ def _skip_if_mpl_1_5():
mod.use("Agg", warn=False)


def _skip_if_has_locale():
lang, _ = locale.getlocale()
if lang is not None:
return True


def _skip_if_not_us_locale():
lang, _ = locale.getlocale()
if lang != 'en_US':
return True


skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
reason="Missing matplotlib dependency")
skip_if_mpl_1_5 = pytest.mark.skipif(_skip_if_mpl_1_5(),
Expand All @@ -92,3 +105,10 @@ def _skip_if_mpl_1_5():
skip_if_windows_python_3 = pytest.mark.skipif(is_platform_windows() and PY3,
reason=("not used on python3/"
"win32"))
skip_if_has_locale = pytest.mark.skipif(_skip_if_has_locale(),
reason="Specific locale is set {lang}"
.format(lang=locale.getlocale()[0]))
skip_if_not_us_locale = pytest.mark.skipif(_skip_if_not_us_locale(),
reason="Specific locale is set "
"{lang}".format(
lang=locale.getlocale()[0]))
16 changes: 0 additions & 16 deletions pandas/util/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,6 @@ def skip_if_no_ne(engine='numexpr'):
installed=_NUMEXPR_INSTALLED))


def _skip_if_has_locale():
import locale
lang, _ = locale.getlocale()
if lang is not None:
import pytest
pytest.skip("Specific locale is set {lang}".format(lang=lang))


def _skip_if_not_us_locale():
import locale
lang, _ = locale.getlocale()
if lang != 'en_US':
import pytest
pytest.skip("Specific locale is set {lang}".format(lang=lang))


def _skip_if_no_mock():
try:
import mock # noqa
Expand Down