Repository: pandas-dev/pandas

Issue:
Failing timeseries tests on debian sparc buildbot
```
======================================================================
ERROR: test_to_timedelta_on_missing_values (pandas.tseries.tests.test_timedeltas.TestTimedeltas)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/tseries/tests/test_timedeltas.py", line 204, in test_to_timedelta_on_missing_values
    expected = Series([np.timedelta64(1000000000, 'ns'), timedelta_NaT], dtype='<m8[ns]')
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/core/series.py", line 218, in __init__
    raise_cast_failure=True)
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/core/series.py", line 2542, in _sanitize_array
    subarr = _try_cast(data, False)
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/core/series.py", line 2503, in _try_cast
    arr = _possibly_cast_to_datetime(arr, dtype)
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/core/common.py", line 1579, in _possibly_cast_to_datetime
    "cannot convert timedeltalike to dtype [%s]" % dtype)
TypeError: cannot convert timedeltalike to dtype [<m8[ns]]

======================================================================
FAIL: test_timedelta_ops_with_missing_values (pandas.tseries.tests.test_timedeltas.TestTimedeltas)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/tseries/tests/test_timedeltas.py", line 281, in test_timedelta_ops_with_missing_values
    assert_frame_equal(actual, dfn)
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/util/testing.py", line 493, in assert_frame_equal
    check_less_precise=check_less_precise)
  File "/home/buildslave/nd-bb-slave-sparc-sid/pandas-py3_x-sid-sparc/build/venv/lib/python3.3/site-packages/pandas-0.13.0rc1_124_g8cd34c6-py3.3-linux-sparc64.egg/pandas/util/testing.py", line 442, in assert_series_equal
    assert_almost_equal(left.values, right.values, check_less_precise)
  File "testing.pyx", line 58, in pandas._testing.assert_almost_equal (pandas/src/testing.c:2561)
  File "testing.pyx", line 93, in pandas._testing.assert_almost_equal (pandas/src/testing.c:1803)
  File "testing.pyx", line 107, in pandas._testing.assert_almost_equal (pandas/src/testing.c:2023)
AssertionError: First object is not null, second is null: numpy.timedelta64(9223372036854775807,'ns') != numpy.timedelta64('NaT','ns')
```

http://nipy.bic.berkeley.edu/builders/pandas-py3.x-sid-sparc/builds/491/steps/shell_4/logs/stdio



Discussion:

Resolving patch:
diff --git a/pandas/core/common.py b/pandas/core/common.py
--- a/pandas/core/common.py
+++ b/pandas/core/common.py
@@ -40,14 +40,17 @@ class AmbiguousIndexError(PandasError, KeyError):
 
 
 _POSSIBLY_CAST_DTYPES = set([np.dtype(t)
-                             for t in ['M8[ns]', 'm8[ns]', 'O', 'int8',
+                             for t in ['M8[ns]', '>M8[ns]', '<M8[ns]',
+                                       'm8[ns]', '>m8[ns]', '<m8[ns]',
+                                       'O', 'int8',
                                        'uint8', 'int16', 'uint16', 'int32',
                                        'uint32', 'int64', 'uint64']])
 
 _NS_DTYPE = np.dtype('M8[ns]')
 _TD_DTYPE = np.dtype('m8[ns]')
 _INT64_DTYPE = np.dtype(np.int64)
-_DATELIKE_DTYPES = set([np.dtype(t) for t in ['M8[ns]', 'm8[ns]']])
+_DATELIKE_DTYPES = set([np.dtype(t) for t in ['M8[ns]', '<M8[ns]', '>M8[ns]',
+                                              'm8[ns]', '<m8[ns]', '>m8[ns]']])
 
 
 # define abstract base classes to enable isinstance type checking on our
@@ -1572,11 +1575,17 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
 
             # force the dtype if needed
             if is_datetime64 and dtype != _NS_DTYPE:
-                raise TypeError(
-                    "cannot convert datetimelike to dtype [%s]" % dtype)
+                if dtype.name == 'datetime64[ns]':
+                    dtype = _NS_DTYPE
+                else:
+                    raise TypeError(
+                        "cannot convert datetimelike to dtype [%s]" % dtype)
             elif is_timedelta64 and dtype != _TD_DTYPE:
-                raise TypeError(
-                    "cannot convert timedeltalike to dtype [%s]" % dtype)
+                if dtype.name == 'timedelta64[ns]':
+                    dtype = _TD_DTYPE
+                else:
+                    raise TypeError(
+                        "cannot convert timedeltalike to dtype [%s]" % dtype)
 
             if np.isscalar(value):
                 if value == tslib.iNaT or isnull(value):
diff --git a/pandas/core/internals.py b/pandas/core/internals.py
--- a/pandas/core/internals.py
+++ b/pandas/core/internals.py
@@ -1245,8 +1245,10 @@ def _try_operate(self, values):
     def _try_coerce_result(self, result):
         """ reverse of try_coerce_args / try_operate """
         if isinstance(result, np.ndarray):
+            mask = isnull(result)
             if result.dtype.kind in ['i', 'f', 'O']:
                 result = result.astype('m8[ns]')
+            result[mask] = tslib.iNaT
         elif isinstance(result, np.integer):
             result = np.timedelta64(result)
         return result


Test patch:
diff --git a/pandas/io/tests/test_stata.py b/pandas/io/tests/test_stata.py
--- a/pandas/io/tests/test_stata.py
+++ b/pandas/io/tests/test_stata.py
@@ -14,6 +14,10 @@
 from pandas.util.misc import is_little_endian
 from pandas import compat
 
+def skip_if_not_little_endian():
+    if not is_little_endian():
+        raise nose.SkipTest("known failure of test on non-little endian")
+
 class TestStata(tm.TestCase):
 
     def setUp(self):
@@ -145,9 +149,7 @@ def test_read_dta4(self):
         tm.assert_frame_equal(parsed_13, expected)
 
     def test_read_write_dta5(self):
-        if not is_little_endian():
-            raise nose.SkipTest("known failure of test_write_dta5 on "
-                                "non-little endian")
+        skip_if_not_little_endian()
 
         original = DataFrame([(np.nan, np.nan, np.nan, np.nan, np.nan)],
                              columns=['float_miss', 'double_miss', 'byte_miss',
@@ -161,9 +163,7 @@ def test_read_write_dta5(self):
                                   original)
 
     def test_write_dta6(self):
-        if not is_little_endian():
-            raise nose.SkipTest("known failure of test_write_dta6 on "
-                                "non-little endian")
+        skip_if_not_little_endian()
 
         original = self.read_csv(self.csv3)
         original.index.name = 'index'
@@ -193,9 +193,7 @@ def test_read_dta9(self):
         tm.assert_frame_equal(parsed, expected)
 
     def test_read_write_dta10(self):
-        if not is_little_endian():
-            raise nose.SkipTest("known failure of test_write_dta10 on "
-                                "non-little endian")
+        skip_if_not_little_endian()
 
         original = DataFrame(data=[["string", "object", 1, 1.1,
                                     np.datetime64('2003-12-25')]],
@@ -232,6 +230,8 @@ def test_encoding(self):
             self.assert_(isinstance(result, unicode))
 
     def test_read_write_dta11(self):
+        skip_if_not_little_endian()
+
         original = DataFrame([(1, 2, 3, 4)],
                              columns=['good', compat.u('b\u00E4d'), '8number', 'astringwithmorethan32characters______'])
         formatted = DataFrame([(1, 2, 3, 4)],
@@ -248,6 +248,8 @@ def test_read_write_dta11(self):
             tm.assert_frame_equal(written_and_read_again.set_index('index'), formatted)
 
     def test_read_write_dta12(self):
+        skip_if_not_little_endian()
+
         original = DataFrame([(1, 2, 3, 4)],
                              columns=['astringwithmorethan32characters_1', 'astringwithmorethan32characters_2', '+', '-'])
         formatted = DataFrame([(1, 2, 3, 4)],