Skip to content

Commit c4b83b7

Browse files
committed
TST/ENH: add locale testing utils
1 parent 4490249 commit c4b83b7

File tree

6 files changed

+236
-14
lines changed

6 files changed

+236
-14
lines changed

ci/install.sh

+5
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ time pip install $PIP_ARGS -r ci/requirements-${TRAVIS_PYTHON_VERSION}${JOB_TAG}
5757
time sudo apt-get install libatlas-base-dev gfortran
5858

5959

60+
# need to enable for locale testing
61+
time echo 'it_CH.UTF-8 UTF-8' | sudo tee -a /var/lib/locales/supported.d/it
62+
time sudo locale-gen
63+
64+
6065
# install gui for clipboard testing
6166
if [ -n "$GUI" ]; then
6267
echo "Using GUI clipboard: $GUI"

doc/source/release.rst

+3
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ Bug Fixes
578578
- Fix a bug with ``NDFrame.replace()`` which made replacement appear as
579579
though it was (incorrectly) using regular expressions (:issue:`5143`).
580580
- Fix better error message for to_datetime (:issue:`4928`)
581+
- Made sure different locales are tested on travis-ci (:issue:`4918`). Also
582+
adds a couple of utilities for getting locales and setting locales with a
583+
context manager.
581584

582585
pandas 0.12.0
583586
-------------

pandas/io/tests/test_data.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pandas.io.data import DataReader, SymbolWarning
1414
from pandas.util.testing import (assert_series_equal, assert_produces_warning,
1515
network, assert_frame_equal)
16+
import pandas.util.testing as tm
1617
from numpy.testing import assert_array_equal
1718

1819

@@ -44,9 +45,9 @@ def test_google(self):
4445
start = datetime(2010, 1, 1)
4546
end = datetime(2013, 1, 27)
4647

47-
self.assertEquals(
48-
web.DataReader("F", 'google', start, end)['Close'][-1],
49-
13.68)
48+
with tm.set_locale('en_US.UTF-8'):
49+
panel = web.DataReader("F", 'google', start, end)
50+
self.assertEquals(panel.Close[-1], 13.68)
5051

5152
self.assertRaises(Exception, web.DataReader, "NON EXISTENT TICKER",
5253
'google', start, end)
@@ -58,13 +59,15 @@ def test_get_quote_fails(self):
5859

5960
@network
6061
def test_get_goog_volume(self):
61-
df = web.get_data_google('GOOG')
62+
with tm.set_locale('en_US.UTF-8'):
63+
df = web.get_data_google('GOOG').sort_index()
6264
self.assertEqual(df.Volume.ix['OCT-08-2010'], 2863473)
6365

6466
@network
6567
def test_get_multi1(self):
6668
sl = ['AAPL', 'AMZN', 'GOOG']
67-
pan = web.get_data_google(sl, '2012')
69+
with tm.set_locale('en_US.UTF-8'):
70+
pan = web.get_data_google(sl, '2012')
6871

6972
def testit():
7073
ts = pan.Close.GOOG.index[pan.Close.AAPL > pan.Close.GOOG]
@@ -79,8 +82,9 @@ def testit():
7982
@network
8083
def test_get_multi2(self):
8184
with warnings.catch_warnings(record=True) as w:
82-
pan = web.get_data_google(['GE', 'MSFT', 'INTC'], 'JAN-01-12',
83-
'JAN-31-12')
85+
with tm.set_locale('en_US.UTF-8'):
86+
pan = web.get_data_google(['GE', 'MSFT', 'INTC'], 'JAN-01-12',
87+
'JAN-31-12')
8488
result = pan.Close.ix['01-18-12']
8589
assert_n_failed_equals_n_null_columns(w, result)
8690

pandas/tools/tests/test_util.py

+40-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import os
2-
import nose
2+
import locale
3+
import codecs
34
import unittest
45

6+
import nose
7+
58
import numpy as np
69
from numpy.testing import assert_equal
710

811
from pandas.tools.util import cartesian_product
12+
from pandas.util.testing import get_locales, set_locale
13+
14+
15+
CURRENT_LOCALE = locale.getlocale()
16+
LOCALE_OVERRIDE = os.environ.get('LOCALE_OVERRIDE', None)
17+
918

1019
class TestCartesianProduct(unittest.TestCase):
1120

@@ -16,6 +25,36 @@ def test_simple(self):
1625
np.array([ 1, 22, 1, 22, 1, 22])]
1726
assert_equal(result, expected)
1827

28+
29+
class TestLocaleUtils(unittest.TestCase):
30+
def test_get_locales(self):
31+
assert len(get_locales(prefix='en')) > 0
32+
33+
def test_get_locales_prefix(self):
34+
with set_locale('en_US.UTF-8'):
35+
assert len(get_locales(prefix='en')) > 0
36+
37+
def test_set_locale(self):
38+
old_locale = CURRENT_LOCALE
39+
40+
if LOCALE_OVERRIDE is not None:
41+
lang, enc = LOCALE_OVERRIDE.split('.')
42+
else:
43+
lang, enc = 'it_CH', 'UTF-8'
44+
45+
enc = codecs.lookup(enc).name
46+
new_locale = lang, enc
47+
48+
with set_locale(new_locale) as normalized_locale:
49+
new_lang, new_enc = normalized_locale.split('.')
50+
new_enc = codecs.lookup(enc).name
51+
normalized_locale = new_lang, new_enc
52+
self.assertEqual(normalized_locale, new_locale)
53+
54+
current_locale = locale.getlocale()
55+
self.assertEqual(current_locale, old_locale)
56+
57+
1958
if __name__ == '__main__':
2059
nose.runmodule(argv=[__file__, '-vvs', '-x', '--pdb', '--pdb-failure'],
2160
exit=False)

pandas/tslib.pyx

-4
Original file line numberDiff line numberDiff line change
@@ -2601,10 +2601,6 @@ cdef object _period_strftime(int64_t value, int freq, object fmt):
26012601

26022602
result = result.replace(str_extra_fmts[i], repl)
26032603

2604-
# Py3?
2605-
#if not PyString_Check(result):
2606-
#result = str(result)
2607-
26082604
if PY2:
26092605
result = result.decode('utf-8', 'ignore')
26102606

0 commit comments

Comments
 (0)