From 66ec3ddcb465c11e2e318b72071bb06f360b272e Mon Sep 17 00:00:00 2001 From: sinhrks Date: Mon, 5 May 2014 12:08:43 +0900 Subject: [PATCH] BUG: DatetimeIndex cannot parse string ndarray with dayfirst --- doc/source/release.rst | 1 + pandas/tseries/index.py | 31 ++++++++++++++++--------- pandas/tseries/tests/test_timeseries.py | 19 +++++++++++++++ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/doc/source/release.rst b/doc/source/release.rst index 5eaa0aa469a3c..3e6f7bb232156 100644 --- a/doc/source/release.rst +++ b/doc/source/release.rst @@ -484,6 +484,7 @@ Bug Fixes or until the end of the line when ``colspec`` contains a ``None`` (previously raised a ``TypeError``) - Bug in cache coherence with chained indexing and slicing; add ``_is_view`` property to ``NDFrame`` to correctly predict views; mark ``is_copy`` on ``xs` only if its an actual copy (and not a view) (:issue:`7084`) +- Bug in DatetimeIndex creation from string ndarray with ``dayfirst=True`` (:issue:`5917`) pandas 0.13.1 ------------- diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py index a2e01c8110261..d9018ad92eb17 100644 --- a/pandas/tseries/index.py +++ b/pandas/tseries/index.py @@ -250,20 +250,29 @@ def __new__(cls, data=None, else: subarr = data.view(_NS_DTYPE) else: - try: - subarr = tools.to_datetime(data, box=False) + if isinstance(data, ABCSeries): + values = data.values + else: + values = data - # make sure that we have a index/ndarray like (and not a Series) - if isinstance(subarr, ABCSeries): - subarr = subarr.values + if lib.is_string_array(values): + subarr = _str_to_dt_array(values, freq, dayfirst=dayfirst, + yearfirst=yearfirst) + else: + try: + subarr = tools.to_datetime(data, box=False) - except ValueError: - # tz aware - subarr = tools.to_datetime(data, box=False, utc=True) + # make sure that we have a index/ndarray like (and not a Series) + if isinstance(subarr, ABCSeries): + subarr = subarr.values + + except ValueError: + # tz aware + subarr = tools.to_datetime(data, box=False, utc=True) - if not np.issubdtype(subarr.dtype, np.datetime64): - raise ValueError('Unable to convert %s to datetime dtype' - % str(data)) + if not np.issubdtype(subarr.dtype, np.datetime64): + raise ValueError('Unable to convert %s to datetime dtype' + % str(data)) if isinstance(subarr, DatetimeIndex): if tz is None: diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py index 319eaee6d14df..7690f118af482 100644 --- a/pandas/tseries/tests/test_timeseries.py +++ b/pandas/tseries/tests/test_timeseries.py @@ -2415,6 +2415,25 @@ def test_datetimeindex_constructor(self): self.assertEquals(len(idx1), len(idx2)) self.assertEquals(idx1.offset, idx2.offset) + def test_dayfirst(self): + # GH 5917 + arr = ['10/02/2014', '11/02/2014', '12/02/2014'] + expected = DatetimeIndex([datetime(2014, 2, 10), + datetime(2014, 2, 11), + datetime(2014, 2, 12)]) + idx1 = DatetimeIndex(arr, dayfirst=True) + idx2 = DatetimeIndex(np.array(arr), dayfirst=True) + idx3 = to_datetime(arr, dayfirst=True) + idx4 = to_datetime(np.array(arr), dayfirst=True) + idx5 = DatetimeIndex(Index(arr), dayfirst=True) + idx6 = DatetimeIndex(Series(arr), dayfirst=True) + self.assert_(expected.equals(idx1)) + self.assert_(expected.equals(idx2)) + self.assert_(expected.equals(idx3)) + self.assert_(expected.equals(idx4)) + self.assert_(expected.equals(idx5)) + self.assert_(expected.equals(idx6)) + def test_dti_snap(self): dti = DatetimeIndex(['1/1/2002', '1/2/2002', '1/3/2002', '1/4/2002', '1/5/2002', '1/6/2002', '1/7/2002'], freq='D')