Skip to content

Commit 6a06d50

Browse files
WillAydjreback
authored andcommitted
Created decorators for skip_if locale functions (#18745)
1 parent d2fd22e commit 6a06d50

File tree

5 files changed

+35
-39
lines changed

5 files changed

+35
-39
lines changed

pandas/tests/indexes/datetimes/test_tools.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from pandas.core.dtypes.common import is_datetime64_ns_dtype
2323
from pandas.util import testing as tm
2424
import pandas.util._test_decorators as td
25-
from pandas.util.testing import assert_series_equal, _skip_if_has_locale
25+
from pandas.util.testing import assert_series_equal
2626
from pandas import (isna, to_datetime, Timestamp, Series, DataFrame,
2727
Index, DatetimeIndex, NaT, date_range, compat)
2828

@@ -144,11 +144,10 @@ def test_to_datetime_format_time(self, cache):
144144
for s, format, dt in data:
145145
assert to_datetime(s, format=format, cache=cache) == dt
146146

147+
@td.skip_if_has_locale
147148
@pytest.mark.parametrize('cache', [True, False])
148149
def test_to_datetime_with_non_exact(self, cache):
149150
# GH 10834
150-
tm._skip_if_has_locale()
151-
152151
# 8904
153152
# exact kw
154153
if sys.version_info < (2, 7):
@@ -830,11 +829,10 @@ def test_to_datetime_with_space_in_series(self, cache):
830829
result_ignore = to_datetime(s, errors='ignore', cache=cache)
831830
tm.assert_series_equal(result_ignore, s)
832831

832+
@td.skip_if_has_locale
833833
@pytest.mark.parametrize('cache', [True, False])
834834
def test_to_datetime_with_apply(self, cache):
835835
# this is only locale tested with US/None locales
836-
tm._skip_if_has_locale()
837-
838836
# GH 5195
839837
# with a format and coerce a single item to_datetime fails
840838
td = Series(['May 04', 'Jun 02', 'Dec 11'], index=[1, 2, 3])
@@ -1023,9 +1021,9 @@ def test_dayfirst(self, cache):
10231021

10241022
class TestGuessDatetimeFormat(object):
10251023

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

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

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

@@ -1393,9 +1391,9 @@ def test_parsers_timestring(self, cache):
13931391
assert result4 == exp_now
13941392
assert result5 == exp_now
13951393

1394+
@td.skip_if_has_locale
13961395
def test_parsers_time(self):
13971396
# GH11818
1398-
_skip_if_has_locale()
13991397
strings = ["14:15", "1415", "2:15pm", "0215pm", "14:15:00", "141500",
14001398
"2:15:00pm", "021500pm", time(14, 15)]
14011399
expected = time(14, 15)

pandas/tests/scalar/test_parsing.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import numpy as np
77
import pytest
88
from dateutil.parser import parse
9+
10+
import pandas.util._test_decorators as td
911
from pandas.conftest import is_dateutil_le_261, is_dateutil_gt_261
1012
from pandas import compat
1113
from pandas.util import testing as tm
@@ -66,6 +68,7 @@ def test_parsers_monthfreq(self):
6668

6769
class TestGuessDatetimeFormat(object):
6870

71+
@td.skip_if_not_us_locale
6972
@is_dateutil_le_261
7073
@pytest.mark.parametrize(
7174
"string, format",
@@ -79,11 +82,10 @@ class TestGuessDatetimeFormat(object):
7982
'%Y-%m-%d %H:%M:%S.%f')])
8083
def test_guess_datetime_format_with_parseable_formats(
8184
self, string, format):
82-
tm._skip_if_not_us_locale()
83-
8485
result = parsing._guess_datetime_format(string)
8586
assert result == format
8687

88+
@td.skip_if_not_us_locale
8789
@is_dateutil_gt_261
8890
@pytest.mark.parametrize(
8991
"string",
@@ -92,8 +94,6 @@ def test_guess_datetime_format_with_parseable_formats(
9294
'2011-12-30 00:00:00.000000'])
9395
def test_guess_datetime_format_with_parseable_formats_gt_261(
9496
self, string):
95-
tm._skip_if_not_us_locale()
96-
9797
result = parsing._guess_datetime_format(string)
9898
assert result is None
9999

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

121+
@td.skip_if_has_locale
121122
@is_dateutil_le_261
122123
@pytest.mark.parametrize(
123124
"string, format",
@@ -127,13 +128,10 @@ def test_guess_datetime_format_with_dayfirst_gt_261(self, dayfirst):
127128
('30/Dec/2011 00:00:00', '%d/%b/%Y %H:%M:%S')])
128129
def test_guess_datetime_format_with_locale_specific_formats(
129130
self, string, format):
130-
# The month names will vary depending on the locale, in which
131-
# case these wont be parsed properly (dateutil can't parse them)
132-
tm._skip_if_has_locale()
133-
134131
result = parsing._guess_datetime_format(string)
135132
assert result == format
136133

134+
@td.skip_if_has_locale
137135
@is_dateutil_gt_261
138136
@pytest.mark.parametrize(
139137
"string",
@@ -143,10 +141,6 @@ def test_guess_datetime_format_with_locale_specific_formats(
143141
'30/Dec/2011 00:00:00'])
144142
def test_guess_datetime_format_with_locale_specific_formats_gt_261(
145143
self, string):
146-
# The month names will vary depending on the locale, in which
147-
# case these wont be parsed properly (dateutil can't parse them)
148-
tm._skip_if_has_locale()
149-
150144
result = parsing._guess_datetime_format(string)
151145
assert result is None
152146

pandas/tests/series/test_timeseries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pandas as pd
1010
import pandas.util.testing as tm
11+
import pandas.util._test_decorators as td
1112
from pandas._libs.tslib import iNaT
1213
from pandas.compat import lrange, StringIO, product
1314
from pandas.core.indexes.timedeltas import TimedeltaIndex
@@ -17,7 +18,7 @@
1718
Timestamp, to_datetime, offsets,
1819
timedelta_range)
1920
from pandas.util.testing import (assert_series_equal, assert_almost_equal,
20-
assert_frame_equal, _skip_if_has_locale)
21+
assert_frame_equal)
2122

2223
from pandas.tests.series.common import TestData
2324

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

742+
@td.skip_if_has_locale
741743
def test_between_time_formats(self):
742744
# GH11818
743-
_skip_if_has_locale()
744-
745745
rng = date_range('1/1/2000', '1/5/2000', freq='5min')
746746
ts = DataFrame(np.random.randn(len(rng), 2), index=rng)
747747

pandas/util/_test_decorators.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def test_foo():
2525
"""
2626

2727
import pytest
28+
import locale
2829
from distutils.version import LooseVersion
2930

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

8384

85+
def _skip_if_has_locale():
86+
lang, _ = locale.getlocale()
87+
if lang is not None:
88+
return True
89+
90+
91+
def _skip_if_not_us_locale():
92+
lang, _ = locale.getlocale()
93+
if lang != 'en_US':
94+
return True
95+
96+
8497
skip_if_no_mpl = pytest.mark.skipif(_skip_if_no_mpl(),
8598
reason="Missing matplotlib dependency")
8699
skip_if_mpl_1_5 = pytest.mark.skipif(_skip_if_mpl_1_5(),
@@ -92,3 +105,10 @@ def _skip_if_mpl_1_5():
92105
skip_if_windows_python_3 = pytest.mark.skipif(is_platform_windows() and PY3,
93106
reason=("not used on python3/"
94107
"win32"))
108+
skip_if_has_locale = pytest.mark.skipif(_skip_if_has_locale(),
109+
reason="Specific locale is set {lang}"
110+
.format(lang=locale.getlocale()[0]))
111+
skip_if_not_us_locale = pytest.mark.skipif(_skip_if_not_us_locale(),
112+
reason="Specific locale is set "
113+
"{lang}".format(
114+
lang=locale.getlocale()[0]))

pandas/util/testing.py

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -389,22 +389,6 @@ def skip_if_no_ne(engine='numexpr'):
389389
installed=_NUMEXPR_INSTALLED))
390390

391391

392-
def _skip_if_has_locale():
393-
import locale
394-
lang, _ = locale.getlocale()
395-
if lang is not None:
396-
import pytest
397-
pytest.skip("Specific locale is set {lang}".format(lang=lang))
398-
399-
400-
def _skip_if_not_us_locale():
401-
import locale
402-
lang, _ = locale.getlocale()
403-
if lang != 'en_US':
404-
import pytest
405-
pytest.skip("Specific locale is set {lang}".format(lang=lang))
406-
407-
408392
def _skip_if_no_mock():
409393
try:
410394
import mock # noqa

0 commit comments

Comments
 (0)