Skip to content

Commit 65ed3af

Browse files
committed
COMPAT: some compatibility fixes with new numpies
closes #13010
1 parent 8001e15 commit 65ed3af

File tree

11 files changed

+60
-61
lines changed

11 files changed

+60
-61
lines changed

pandas/compat/numpy_compat.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" support numpy compatiblitiy across versions """
22

3+
import re
34
import numpy as np
45
from distutils.version import LooseVersion
56
from pandas.compat import string_types, string_and_binary_types
@@ -24,11 +25,14 @@
2425
'this pandas version'.format(_np_version))
2526

2627

28+
_tz_regex = re.compile('[+-]0000$')
29+
30+
2731
def tz_replacer(s):
2832
if isinstance(s, string_types):
2933
if s.endswith('Z'):
3034
s = s[:-1]
31-
elif s.endswith('-0000'):
35+
elif _tz_regex.search(s):
3236
s = s[:-5]
3337
return s
3438

pandas/computation/tests/test_eval.py

+3-17
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ def _eval_single_bin(lhs, cmp1, rhs, engine):
5050
try:
5151
return c(lhs, rhs)
5252
except ValueError as e:
53-
try:
54-
msg = e.message
55-
except AttributeError:
56-
msg = e
57-
msg = u(msg)
58-
if msg == u('negative number cannot be raised to a fractional'
59-
' power'):
53+
if str(e).startswith('negative number cannot be raised to a fractional power'):
6054
return np.nan
6155
raise
6256
return c(lhs, rhs)
@@ -306,17 +300,9 @@ def get_expected_pow_result(self, lhs, rhs):
306300
try:
307301
expected = _eval_single_bin(lhs, '**', rhs, self.engine)
308302
except ValueError as e:
309-
msg = 'negative number cannot be raised to a fractional power'
310-
try:
311-
emsg = e.message
312-
except AttributeError:
313-
emsg = e
314-
315-
emsg = u(emsg)
316-
317-
if emsg == msg:
303+
if str(e).startswith('negative number cannot be raised to a fractional power'):
318304
if self.engine == 'python':
319-
raise nose.SkipTest(emsg)
305+
raise nose.SkipTest(str(e))
320306
else:
321307
expected = np.nan
322308
else:

pandas/io/tests/test_excel.py

+11-13
Original file line numberDiff line numberDiff line change
@@ -2174,19 +2174,17 @@ def check_called(func):
21742174
del called_save[:]
21752175
del called_write_cells[:]
21762176

2177-
register_writer(DummyClass)
2178-
writer = ExcelWriter('something.test')
2179-
tm.assertIsInstance(writer, DummyClass)
2180-
df = tm.makeCustomDataframe(1, 1)
2181-
panel = tm.makePanel()
2182-
func = lambda: df.to_excel('something.test')
2183-
check_called(func)
2184-
check_called(lambda: panel.to_excel('something.test'))
2185-
val = get_option('io.excel.xlsx.writer')
2186-
set_option('io.excel.xlsx.writer', 'dummy')
2187-
check_called(lambda: df.to_excel('something.xlsx'))
2188-
check_called(lambda: df.to_excel('something.xls', engine='dummy'))
2189-
set_option('io.excel.xlsx.writer', val)
2177+
with pd.option_context('io.excel.xlsx.writer', 'dummy'):
2178+
register_writer(DummyClass)
2179+
writer = ExcelWriter('something.test')
2180+
tm.assertIsInstance(writer, DummyClass)
2181+
df = tm.makeCustomDataframe(1, 1)
2182+
panel = tm.makePanel()
2183+
func = lambda: df.to_excel('something.test')
2184+
check_called(func)
2185+
check_called(lambda: panel.to_excel('something.test'))
2186+
check_called(lambda: df.to_excel('something.xlsx'))
2187+
check_called(lambda: df.to_excel('something.xls', engine='dummy'))
21902188

21912189

21922190
if __name__ == '__main__':

pandas/io/tests/test_gbq.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pandas.core.frame import DataFrame
1616
import pandas.io.gbq as gbq
1717
import pandas.util.testing as tm
18+
from pandas.compat.numpy_compat import np_datetime64_compat
1819

1920
PROJECT_ID = None
2021
PRIVATE_KEY_JSON_PATH = None
@@ -289,7 +290,7 @@ def test_should_return_bigquery_floats_as_python_floats(self):
289290

290291
def test_should_return_bigquery_timestamps_as_numpy_datetime(self):
291292
result = gbq._parse_entry('0e9', 'TIMESTAMP')
292-
tm.assert_equal(result, np.datetime64('1970-01-01T00:00:00Z'))
293+
tm.assert_equal(result, np_datetime64_compat('1970-01-01T00:00:00Z'))
293294

294295
def test_should_return_bigquery_booleans_as_python_booleans(self):
295296
result = gbq._parse_entry('false', 'BOOLEAN')

pandas/io/tests/test_packers.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import nose
22

3+
import warnings
34
import os
45
import datetime
56
import numpy as np
@@ -519,7 +520,8 @@ def test_sparse_frame(self):
519520

520521
def test_sparse_panel(self):
521522

522-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
523+
with warnings.catch_warnings(record=True):
524+
523525
items = ['x', 'y', 'z']
524526
p = Panel(dict((i, tm.makeDataFrame().ix[:2, :2]) for i in items))
525527
sp = p.to_sparse()

pandas/io/tests/test_pytables.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -848,11 +848,11 @@ def test_append(self):
848848

849849
# uints - test storage of uints
850850
uint_data = DataFrame({
851-
'u08': Series(np.random.random_integers(0, high=255, size=5),
851+
'u08': Series(np.random.randint(0, high=255, size=5),
852852
dtype=np.uint8),
853-
'u16': Series(np.random.random_integers(0, high=65535, size=5),
853+
'u16': Series(np.random.randint(0, high=65535, size=5),
854854
dtype=np.uint16),
855-
'u32': Series(np.random.random_integers(0, high=2**30, size=5),
855+
'u32': Series(np.random.randint(0, high=2**30, size=5),
856856
dtype=np.uint32),
857857
'u64': Series([2**58, 2**59, 2**60, 2**61, 2**62],
858858
dtype=np.uint64)}, index=np.arange(5))

pandas/tests/formats/test_format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import IPython
2929
if IPython.__version__ < LooseVersion('3.0.0'):
3030
div_style = ' style="max-width:1500px;overflow:auto;"'
31-
except ImportError:
31+
except (ImportError, AttributeError):
3232
pass
3333

3434
from pandas import DataFrame, Series, Index, Timestamp, MultiIndex, date_range, NaT

pandas/tests/indexes/test_base.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
CategoricalIndex, DatetimeIndex, TimedeltaIndex,
2222
PeriodIndex)
2323
from pandas.util.testing import assert_almost_equal
24+
from pandas.compat.numpy_compat import np_datetime64_compat
2425

2526
import pandas.core.config as cf
2627

@@ -250,18 +251,18 @@ def test_constructor_dtypes(self):
250251

251252
for idx in [Index(np.array([1, 2, 3], dtype=int), dtype='category'),
252253
Index([1, 2, 3], dtype='category'),
253-
Index(np.array([np.datetime64('2011-01-01'),
254-
np.datetime64('2011-01-02')]), dtype='category'),
254+
Index(np.array([np_datetime64_compat('2011-01-01'),
255+
np_datetime64_compat('2011-01-02')]), dtype='category'),
255256
Index([datetime(2011, 1, 1), datetime(2011, 1, 2)], dtype='category')]:
256257
self.assertIsInstance(idx, CategoricalIndex)
257258

258-
for idx in [Index(np.array([np.datetime64('2011-01-01'),
259-
np.datetime64('2011-01-02')])),
259+
for idx in [Index(np.array([np_datetime64_compat('2011-01-01'),
260+
np_datetime64_compat('2011-01-02')])),
260261
Index([datetime(2011, 1, 1), datetime(2011, 1, 2)])]:
261262
self.assertIsInstance(idx, DatetimeIndex)
262263

263-
for idx in [Index(np.array([np.datetime64('2011-01-01'),
264-
np.datetime64('2011-01-02')]), dtype=object),
264+
for idx in [Index(np.array([np_datetime64_compat('2011-01-01'),
265+
np_datetime64_compat('2011-01-02')]), dtype=object),
265266
Index([datetime(2011, 1, 1),
266267
datetime(2011, 1, 2)], dtype=object)]:
267268
self.assertNotIsInstance(idx, DatetimeIndex)
@@ -442,8 +443,8 @@ def test_nanosecond_index_access(self):
442443

443444
self.assertEqual(
444445
first_value,
445-
x[Timestamp(np.datetime64('2013-01-01 00:00:00.000000050+0000',
446-
'ns'))])
446+
x[Timestamp(np_datetime64_compat('2013-01-01 00:00:00.000000050+0000',
447+
'ns'))])
447448

448449
def test_comparators(self):
449450
index = self.dateIndex

pandas/tests/test_algos.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import pandas.core.algorithms as algos
1212
import pandas.util.testing as tm
1313
import pandas.hashtable as hashtable
14+
from pandas.compat.numpy_compat import np_array_datetime64_compat
1415

1516

1617
class TestMatch(tm.TestCase):
@@ -275,9 +276,10 @@ def test_on_index_object(self):
275276

276277
def test_datetime64_dtype_array_returned(self):
277278
# GH 9431
278-
expected = np.array(['2015-01-03T00:00:00.000000000+0000',
279-
'2015-01-01T00:00:00.000000000+0000'],
280-
dtype='M8[ns]')
279+
expected = np_array_datetime64_compat(
280+
['2015-01-03T00:00:00.000000000+0000',
281+
'2015-01-01T00:00:00.000000000+0000'],
282+
dtype='M8[ns]')
281283

282284
dt_index = pd.to_datetime(['2015-01-03T00:00:00.000000000+0000',
283285
'2015-01-01T00:00:00.000000000+0000',

pandas/tests/test_base.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from pandas import (Series, Index, DatetimeIndex, TimedeltaIndex, PeriodIndex,
1515
Timedelta)
1616
from pandas.compat import u, StringIO
17+
from pandas.compat.numpy_compat import np_array_datetime64_compat
1718
from pandas.core.base import (FrozenList, FrozenNDArray, PandasDelegate,
1819
NoNewAttributesMixin)
1920
from pandas.tseries.base import DatetimeIndexOpsMixin
@@ -663,10 +664,10 @@ def test_value_counts_inferred(self):
663664
expected_s = Series([3, 2, 1], index=idx)
664665
tm.assert_series_equal(s.value_counts(), expected_s)
665666

666-
expected = np.array(['2010-01-01 00:00:00Z',
667-
'2009-01-01 00:00:00Z',
668-
'2008-09-09 00:00:00Z'],
669-
dtype='datetime64[ns]')
667+
expected = np_array_datetime64_compat(['2010-01-01 00:00:00Z',
668+
'2009-01-01 00:00:00Z',
669+
'2008-09-09 00:00:00Z'],
670+
dtype='datetime64[ns]')
670671
if isinstance(s, DatetimeIndex):
671672
expected = DatetimeIndex(expected)
672673
self.assertTrue(s.unique().equals(expected))

pandas/tseries/tests/test_converter.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pandas.compat import u
99
import pandas.util.testing as tm
1010
from pandas.tseries.offsets import Second, Milli, Micro
11+
from pandas.compat.numpy_compat import np_datetime64_compat
1112

1213
try:
1314
import pandas.tseries.converter as converter
@@ -50,16 +51,16 @@ def test_conversion(self):
5051
self.assertEqual(rs, xp)
5152

5253
# also testing datetime64 dtype (GH8614)
53-
rs = self.dtc.convert(np.datetime64('2012-01-01'), None, None)
54+
rs = self.dtc.convert(np_datetime64_compat('2012-01-01'), None, None)
5455
self.assertEqual(rs, xp)
5556

56-
rs = self.dtc.convert(np.datetime64(
57-
'2012-01-01 00:00:00+00:00'), None, None)
57+
rs = self.dtc.convert(np_datetime64_compat(
58+
'2012-01-01 00:00:00+0000'), None, None)
5859
self.assertEqual(rs, xp)
5960

6061
rs = self.dtc.convert(np.array([
61-
np.datetime64('2012-01-01 00:00:00+00:00'),
62-
np.datetime64('2012-01-02 00:00:00+00:00')]), None, None)
62+
np_datetime64_compat('2012-01-01 00:00:00+0000'),
63+
np_datetime64_compat('2012-01-02 00:00:00+0000')]), None, None)
6364
self.assertEqual(rs[0], xp)
6465

6566
def test_conversion_float(self):
@@ -142,16 +143,19 @@ def test_conversion(self):
142143
self.assertEqual(rs, xp)
143144

144145
# FIXME
145-
# rs = self.pc.convert(np.datetime64('2012-01-01'), None, self.axis)
146+
# rs = self.pc.convert(
147+
# np_datetime64_compat('2012-01-01'), None, self.axis)
146148
# self.assertEqual(rs, xp)
147149
#
148-
# rs = self.pc.convert(np.datetime64('2012-01-01 00:00:00+00:00'),
150+
# rs = self.pc.convert(
151+
# np_datetime64_compat('2012-01-01 00:00:00+0000'),
149152
# None, self.axis)
150153
# self.assertEqual(rs, xp)
151154
#
152155
# rs = self.pc.convert(np.array([
153-
# np.datetime64('2012-01-01 00:00:00+00:00'),
154-
# np.datetime64('2012-01-02 00:00:00+00:00')]), None, self.axis)
156+
# np_datetime64_compat('2012-01-01 00:00:00+0000'),
157+
# np_datetime64_compat('2012-01-02 00:00:00+0000')]),
158+
# None, self.axis)
155159
# self.assertEqual(rs[0], xp)
156160

157161
def test_integer_passthrough(self):

0 commit comments

Comments
 (0)