Repository: pandas-dev/pandas

Issue:
COMPAT: some compatibility fixes with new numpies



Discussion:

Resolving patch:
diff --git a/pandas/compat/numpy_compat.py b/pandas/compat/numpy_compat.py
--- a/pandas/compat/numpy_compat.py
+++ b/pandas/compat/numpy_compat.py
@@ -1,5 +1,6 @@
 """ support numpy compatiblitiy across versions """
 
+import re
 import numpy as np
 from distutils.version import LooseVersion
 from pandas.compat import string_types, string_and_binary_types
@@ -24,11 +25,14 @@
                       'this pandas version'.format(_np_version))
 
 
+_tz_regex = re.compile('[+-]0000$')
+
+
 def tz_replacer(s):
     if isinstance(s, string_types):
         if s.endswith('Z'):
             s = s[:-1]
-        elif s.endswith('-0000'):
+        elif _tz_regex.search(s):
             s = s[:-5]
     return s
 


Test patch:
diff --git a/pandas/computation/tests/test_eval.py b/pandas/computation/tests/test_eval.py
--- a/pandas/computation/tests/test_eval.py
+++ b/pandas/computation/tests/test_eval.py
@@ -50,13 +50,7 @@ def _eval_single_bin(lhs, cmp1, rhs, engine):
         try:
             return c(lhs, rhs)
         except ValueError as e:
-            try:
-                msg = e.message
-            except AttributeError:
-                msg = e
-            msg = u(msg)
-            if msg == u('negative number cannot be raised to a fractional'
-                        ' power'):
+            if str(e).startswith('negative number cannot be raised to a fractional power'):
                 return np.nan
             raise
     return c(lhs, rhs)
@@ -306,17 +300,9 @@ def get_expected_pow_result(self, lhs, rhs):
         try:
             expected = _eval_single_bin(lhs, '**', rhs, self.engine)
         except ValueError as e:
-            msg = 'negative number cannot be raised to a fractional power'
-            try:
-                emsg = e.message
-            except AttributeError:
-                emsg = e
-
-            emsg = u(emsg)
-
-            if emsg == msg:
+            if str(e).startswith('negative number cannot be raised to a fractional power'):
                 if self.engine == 'python':
-                    raise nose.SkipTest(emsg)
+                    raise nose.SkipTest(str(e))
                 else:
                     expected = np.nan
             else:
diff --git a/pandas/io/tests/test_excel.py b/pandas/io/tests/test_excel.py
--- a/pandas/io/tests/test_excel.py
+++ b/pandas/io/tests/test_excel.py
@@ -2174,19 +2174,17 @@ def check_called(func):
             del called_save[:]
             del called_write_cells[:]
 
-        register_writer(DummyClass)
-        writer = ExcelWriter('something.test')
-        tm.assertIsInstance(writer, DummyClass)
-        df = tm.makeCustomDataframe(1, 1)
-        panel = tm.makePanel()
-        func = lambda: df.to_excel('something.test')
-        check_called(func)
-        check_called(lambda: panel.to_excel('something.test'))
-        val = get_option('io.excel.xlsx.writer')
-        set_option('io.excel.xlsx.writer', 'dummy')
-        check_called(lambda: df.to_excel('something.xlsx'))
-        check_called(lambda: df.to_excel('something.xls', engine='dummy'))
-        set_option('io.excel.xlsx.writer', val)
+        with pd.option_context('io.excel.xlsx.writer', 'dummy'):
+            register_writer(DummyClass)
+            writer = ExcelWriter('something.test')
+            tm.assertIsInstance(writer, DummyClass)
+            df = tm.makeCustomDataframe(1, 1)
+            panel = tm.makePanel()
+            func = lambda: df.to_excel('something.test')
+            check_called(func)
+            check_called(lambda: panel.to_excel('something.test'))
+            check_called(lambda: df.to_excel('something.xlsx'))
+            check_called(lambda: df.to_excel('something.xls', engine='dummy'))
 
 
 if __name__ == '__main__':
diff --git a/pandas/io/tests/test_gbq.py b/pandas/io/tests/test_gbq.py
--- a/pandas/io/tests/test_gbq.py
+++ b/pandas/io/tests/test_gbq.py
@@ -15,6 +15,7 @@
 from pandas.core.frame import DataFrame
 import pandas.io.gbq as gbq
 import pandas.util.testing as tm
+from pandas.compat.numpy_compat import np_datetime64_compat
 
 PROJECT_ID = None
 PRIVATE_KEY_JSON_PATH = None
@@ -289,7 +290,7 @@ def test_should_return_bigquery_floats_as_python_floats(self):
 
     def test_should_return_bigquery_timestamps_as_numpy_datetime(self):
         result = gbq._parse_entry('0e9', 'TIMESTAMP')
-        tm.assert_equal(result, np.datetime64('1970-01-01T00:00:00Z'))
+        tm.assert_equal(result, np_datetime64_compat('1970-01-01T00:00:00Z'))
 
     def test_should_return_bigquery_booleans_as_python_booleans(self):
         result = gbq._parse_entry('false', 'BOOLEAN')
diff --git a/pandas/io/tests/test_packers.py b/pandas/io/tests/test_packers.py
--- a/pandas/io/tests/test_packers.py
+++ b/pandas/io/tests/test_packers.py
@@ -1,5 +1,6 @@
 import nose
 
+import warnings
 import os
 import datetime
 import numpy as np
@@ -519,7 +520,8 @@ def test_sparse_frame(self):
 
     def test_sparse_panel(self):
 
-        with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
+        with warnings.catch_warnings(record=True):
+
             items = ['x', 'y', 'z']
             p = Panel(dict((i, tm.makeDataFrame().ix[:2, :2]) for i in items))
             sp = p.to_sparse()
diff --git a/pandas/io/tests/test_pytables.py b/pandas/io/tests/test_pytables.py
--- a/pandas/io/tests/test_pytables.py
+++ b/pandas/io/tests/test_pytables.py
@@ -848,11 +848,11 @@ def test_append(self):
 
             # uints - test storage of uints
             uint_data = DataFrame({
-                'u08': Series(np.random.random_integers(0, high=255, size=5),
+                'u08': Series(np.random.randint(0, high=255, size=5),
                               dtype=np.uint8),
-                'u16': Series(np.random.random_integers(0, high=65535, size=5),
+                'u16': Series(np.random.randint(0, high=65535, size=5),
                               dtype=np.uint16),
-                'u32': Series(np.random.random_integers(0, high=2**30, size=5),
+                'u32': Series(np.random.randint(0, high=2**30, size=5),
                               dtype=np.uint32),
                 'u64': Series([2**58, 2**59, 2**60, 2**61, 2**62],
                               dtype=np.uint64)}, index=np.arange(5))
diff --git a/pandas/tests/formats/test_format.py b/pandas/tests/formats/test_format.py
--- a/pandas/tests/formats/test_format.py
+++ b/pandas/tests/formats/test_format.py
@@ -28,7 +28,7 @@
     import IPython
     if IPython.__version__ < LooseVersion('3.0.0'):
         div_style = ' style="max-width:1500px;overflow:auto;"'
-except ImportError:
+except (ImportError, AttributeError):
     pass
 
 from pandas import DataFrame, Series, Index, Timestamp, MultiIndex, date_range, NaT
diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py
--- a/pandas/tests/indexes/test_base.py
+++ b/pandas/tests/indexes/test_base.py
@@ -21,6 +21,7 @@
                     CategoricalIndex, DatetimeIndex, TimedeltaIndex,
                     PeriodIndex)
 from pandas.util.testing import assert_almost_equal
+from pandas.compat.numpy_compat import np_datetime64_compat
 
 import pandas.core.config as cf
 
@@ -250,18 +251,18 @@ def test_constructor_dtypes(self):
 
         for idx in [Index(np.array([1, 2, 3], dtype=int), dtype='category'),
                     Index([1, 2, 3], dtype='category'),
-                    Index(np.array([np.datetime64('2011-01-01'),
-                                    np.datetime64('2011-01-02')]), dtype='category'),
+                    Index(np.array([np_datetime64_compat('2011-01-01'),
+                                    np_datetime64_compat('2011-01-02')]), dtype='category'),
                     Index([datetime(2011, 1, 1), datetime(2011, 1, 2)], dtype='category')]:
             self.assertIsInstance(idx, CategoricalIndex)
 
-        for idx in [Index(np.array([np.datetime64('2011-01-01'),
-                                    np.datetime64('2011-01-02')])),
+        for idx in [Index(np.array([np_datetime64_compat('2011-01-01'),
+                                    np_datetime64_compat('2011-01-02')])),
                     Index([datetime(2011, 1, 1), datetime(2011, 1, 2)])]:
             self.assertIsInstance(idx, DatetimeIndex)
 
-        for idx in [Index(np.array([np.datetime64('2011-01-01'),
-                                    np.datetime64('2011-01-02')]), dtype=object),
+        for idx in [Index(np.array([np_datetime64_compat('2011-01-01'),
+                                    np_datetime64_compat('2011-01-02')]), dtype=object),
                     Index([datetime(2011, 1, 1),
                            datetime(2011, 1, 2)], dtype=object)]:
             self.assertNotIsInstance(idx, DatetimeIndex)
@@ -442,8 +443,8 @@ def test_nanosecond_index_access(self):
 
         self.assertEqual(
             first_value,
-            x[Timestamp(np.datetime64('2013-01-01 00:00:00.000000050+0000',
-                                      'ns'))])
+            x[Timestamp(np_datetime64_compat('2013-01-01 00:00:00.000000050+0000',
+                                             'ns'))])
 
     def test_comparators(self):
         index = self.dateIndex
diff --git a/pandas/tests/test_algos.py b/pandas/tests/test_algos.py
--- a/pandas/tests/test_algos.py
+++ b/pandas/tests/test_algos.py
@@ -11,6 +11,7 @@
 import pandas.core.algorithms as algos
 import pandas.util.testing as tm
 import pandas.hashtable as hashtable
+from pandas.compat.numpy_compat import np_array_datetime64_compat
 
 
 class TestMatch(tm.TestCase):
@@ -275,9 +276,10 @@ def test_on_index_object(self):
 
     def test_datetime64_dtype_array_returned(self):
         # GH 9431
-        expected = np.array(['2015-01-03T00:00:00.000000000+0000',
-                             '2015-01-01T00:00:00.000000000+0000'],
-                            dtype='M8[ns]')
+        expected = np_array_datetime64_compat(
+            ['2015-01-03T00:00:00.000000000+0000',
+             '2015-01-01T00:00:00.000000000+0000'],
+            dtype='M8[ns]')
 
         dt_index = pd.to_datetime(['2015-01-03T00:00:00.000000000+0000',
                                    '2015-01-01T00:00:00.000000000+0000',
diff --git a/pandas/tests/test_base.py b/pandas/tests/test_base.py
--- a/pandas/tests/test_base.py
+++ b/pandas/tests/test_base.py
@@ -14,6 +14,7 @@
 from pandas import (Series, Index, DatetimeIndex, TimedeltaIndex, PeriodIndex,
                     Timedelta)
 from pandas.compat import u, StringIO
+from pandas.compat.numpy_compat import np_array_datetime64_compat
 from pandas.core.base import (FrozenList, FrozenNDArray, PandasDelegate,
                               NoNewAttributesMixin)
 from pandas.tseries.base import DatetimeIndexOpsMixin
@@ -663,10 +664,10 @@ def test_value_counts_inferred(self):
             expected_s = Series([3, 2, 1], index=idx)
             tm.assert_series_equal(s.value_counts(), expected_s)
 
-            expected = np.array(['2010-01-01 00:00:00Z',
-                                 '2009-01-01 00:00:00Z',
-                                 '2008-09-09 00:00:00Z'],
-                                dtype='datetime64[ns]')
+            expected = np_array_datetime64_compat(['2010-01-01 00:00:00Z',
+                                                   '2009-01-01 00:00:00Z',
+                                                   '2008-09-09 00:00:00Z'],
+                                                  dtype='datetime64[ns]')
             if isinstance(s, DatetimeIndex):
                 expected = DatetimeIndex(expected)
                 self.assertTrue(s.unique().equals(expected))
diff --git a/pandas/tseries/tests/test_converter.py b/pandas/tseries/tests/test_converter.py
--- a/pandas/tseries/tests/test_converter.py
+++ b/pandas/tseries/tests/test_converter.py
@@ -8,6 +8,7 @@
 from pandas.compat import u
 import pandas.util.testing as tm
 from pandas.tseries.offsets import Second, Milli, Micro
+from pandas.compat.numpy_compat import np_datetime64_compat
 
 try:
     import pandas.tseries.converter as converter
@@ -50,16 +51,16 @@ def test_conversion(self):
         self.assertEqual(rs, xp)
 
         # also testing datetime64 dtype (GH8614)
-        rs = self.dtc.convert(np.datetime64('2012-01-01'), None, None)
+        rs = self.dtc.convert(np_datetime64_compat('2012-01-01'), None, None)
         self.assertEqual(rs, xp)
 
-        rs = self.dtc.convert(np.datetime64(
-            '2012-01-01 00:00:00+00:00'), None, None)
+        rs = self.dtc.convert(np_datetime64_compat(
+            '2012-01-01 00:00:00+0000'), None, None)
         self.assertEqual(rs, xp)
 
         rs = self.dtc.convert(np.array([
-            np.datetime64('2012-01-01 00:00:00+00:00'),
-            np.datetime64('2012-01-02 00:00:00+00:00')]), None, None)
+            np_datetime64_compat('2012-01-01 00:00:00+0000'),
+            np_datetime64_compat('2012-01-02 00:00:00+0000')]), None, None)
         self.assertEqual(rs[0], xp)
 
     def test_conversion_float(self):
@@ -142,16 +143,19 @@ def test_conversion(self):
         self.assertEqual(rs, xp)
 
         # FIXME
-        # rs = self.pc.convert(np.datetime64('2012-01-01'), None, self.axis)
+        # rs = self.pc.convert(
+        #        np_datetime64_compat('2012-01-01'), None, self.axis)
         # self.assertEqual(rs, xp)
         #
-        # rs = self.pc.convert(np.datetime64('2012-01-01 00:00:00+00:00'),
+        # rs = self.pc.convert(
+        #        np_datetime64_compat('2012-01-01 00:00:00+0000'),
         #                      None, self.axis)
         # self.assertEqual(rs, xp)
         #
         # rs = self.pc.convert(np.array([
-        #     np.datetime64('2012-01-01 00:00:00+00:00'),
-        #     np.datetime64('2012-01-02 00:00:00+00:00')]), None, self.axis)
+        #     np_datetime64_compat('2012-01-01 00:00:00+0000'),
+        #     np_datetime64_compat('2012-01-02 00:00:00+0000')]),
+        #                          None, self.axis)
         # self.assertEqual(rs[0], xp)
 
     def test_integer_passthrough(self):