From 025127c171dad00cb66d478b80fea3fde217dc90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Est=C3=A8ve?= Date: Tue, 5 Feb 2013 23:44:14 +0000 Subject: [PATCH] BUG: Creating DatetimeIndex from empty array with datetime64 dtype raises IndexError --- pandas/tests/test_common.py | 4 ++++ pandas/tseries/index.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pandas/tests/test_common.py b/pandas/tests/test_common.py index 5b1d6c31403cb..40fc2e702402a 100644 --- a/pandas/tests/test_common.py +++ b/pandas/tests/test_common.py @@ -106,6 +106,10 @@ def test_isnull_datetime(): assert(mask[0]) assert(not mask[1:].any()) +def test_datetimeindex_from_empty_datetime64_array(): + for unit in [ 'ms', 'us', 'ns' ]: + idx = DatetimeIndex(np.array([], dtype='datetime64[%s]' % unit)) + assert(len(idx) == 0) def test_any_none(): assert(com._any_none(1, 2, 3, None)) diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index e45c21084e45f..04d19842ee846 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -227,7 +227,7 @@ def __new__(cls, data=None, offset = data.offset verify_integrity = False else: - if data.dtype != _NS_DTYPE: + if data.dtype != _NS_DTYPE and data.size: subarr = tslib.cast_to_nanoseconds(data) else: subarr = data