Skip to content

Commit 9ed4f7d

Browse files
committed
BUG: big endian timedelta fix (GH5779)
1 parent e18da0e commit 9ed4f7d

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

pandas/core/common.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ class AmbiguousIndexError(PandasError, KeyError):
4040

4141

4242
_POSSIBLY_CAST_DTYPES = set([np.dtype(t)
43-
for t in ['M8[ns]', 'm8[ns]', 'O', 'int8',
43+
for t in ['M8[ns]', '>M8[ns]', '<M8[ns]',
44+
'm8[ns]', '>m8[ns]', '<m8[ns]',
45+
'O', 'int8',
4446
'uint8', 'int16', 'uint16', 'int32',
4547
'uint32', 'int64', 'uint64']])
4648

4749
_NS_DTYPE = np.dtype('M8[ns]')
4850
_TD_DTYPE = np.dtype('m8[ns]')
4951
_INT64_DTYPE = np.dtype(np.int64)
50-
_DATELIKE_DTYPES = set([np.dtype(t) for t in ['M8[ns]', 'm8[ns]']])
52+
_DATELIKE_DTYPES = set([np.dtype(t) for t in ['M8[ns]', '<M8[ns]', '>M8[ns]',
53+
'm8[ns]', '<m8[ns]', '>m8[ns]']])
5154

5255

5356
# define abstract base classes to enable isinstance type checking on our
@@ -1572,11 +1575,17 @@ def _possibly_cast_to_datetime(value, dtype, coerce=False):
15721575

15731576
# force the dtype if needed
15741577
if is_datetime64 and dtype != _NS_DTYPE:
1575-
raise TypeError(
1576-
"cannot convert datetimelike to dtype [%s]" % dtype)
1578+
if dtype.name == 'datetime64[ns]':
1579+
dtype = _NS_DTYPE
1580+
else:
1581+
raise TypeError(
1582+
"cannot convert datetimelike to dtype [%s]" % dtype)
15771583
elif is_timedelta64 and dtype != _TD_DTYPE:
1578-
raise TypeError(
1579-
"cannot convert timedeltalike to dtype [%s]" % dtype)
1584+
if dtype.name == 'timedelta64[ns]':
1585+
dtype = _TD_DTYPE
1586+
else:
1587+
raise TypeError(
1588+
"cannot convert timedeltalike to dtype [%s]" % dtype)
15801589

15811590
if np.isscalar(value):
15821591
if value == tslib.iNaT or isnull(value):

pandas/core/internals.py

+2
Original file line numberDiff line numberDiff line change
@@ -1245,8 +1245,10 @@ def _try_operate(self, values):
12451245
def _try_coerce_result(self, result):
12461246
""" reverse of try_coerce_args / try_operate """
12471247
if isinstance(result, np.ndarray):
1248+
mask = isnull(result)
12481249
if result.dtype.kind in ['i', 'f', 'O']:
12491250
result = result.astype('m8[ns]')
1251+
result[mask] = tslib.iNaT
12501252
elif isinstance(result, np.integer):
12511253
result = np.timedelta64(result)
12521254
return result

0 commit comments

Comments
 (0)