Skip to content

Commit 66ec3dd

Browse files
committed
BUG: DatetimeIndex cannot parse string ndarray with dayfirst
1 parent ecaa39c commit 66ec3dd

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

doc/source/release.rst

+1
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ Bug Fixes
484484
or until the end of the line when ``colspec`` contains a ``None`` (previously raised a ``TypeError``)
485485
- Bug in cache coherence with chained indexing and slicing; add ``_is_view`` property to ``NDFrame`` to correctly predict
486486
views; mark ``is_copy`` on ``xs` only if its an actual copy (and not a view) (:issue:`7084`)
487+
- Bug in DatetimeIndex creation from string ndarray with ``dayfirst=True`` (:issue:`5917`)
487488

488489
pandas 0.13.1
489490
-------------

pandas/tseries/index.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,29 @@ def __new__(cls, data=None,
250250
else:
251251
subarr = data.view(_NS_DTYPE)
252252
else:
253-
try:
254-
subarr = tools.to_datetime(data, box=False)
253+
if isinstance(data, ABCSeries):
254+
values = data.values
255+
else:
256+
values = data
255257

256-
# make sure that we have a index/ndarray like (and not a Series)
257-
if isinstance(subarr, ABCSeries):
258-
subarr = subarr.values
258+
if lib.is_string_array(values):
259+
subarr = _str_to_dt_array(values, freq, dayfirst=dayfirst,
260+
yearfirst=yearfirst)
261+
else:
262+
try:
263+
subarr = tools.to_datetime(data, box=False)
259264

260-
except ValueError:
261-
# tz aware
262-
subarr = tools.to_datetime(data, box=False, utc=True)
265+
# make sure that we have a index/ndarray like (and not a Series)
266+
if isinstance(subarr, ABCSeries):
267+
subarr = subarr.values
268+
269+
except ValueError:
270+
# tz aware
271+
subarr = tools.to_datetime(data, box=False, utc=True)
263272

264-
if not np.issubdtype(subarr.dtype, np.datetime64):
265-
raise ValueError('Unable to convert %s to datetime dtype'
266-
% str(data))
273+
if not np.issubdtype(subarr.dtype, np.datetime64):
274+
raise ValueError('Unable to convert %s to datetime dtype'
275+
% str(data))
267276

268277
if isinstance(subarr, DatetimeIndex):
269278
if tz is None:

pandas/tseries/tests/test_timeseries.py

+19
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,25 @@ def test_datetimeindex_constructor(self):
24152415
self.assertEquals(len(idx1), len(idx2))
24162416
self.assertEquals(idx1.offset, idx2.offset)
24172417

2418+
def test_dayfirst(self):
2419+
# GH 5917
2420+
arr = ['10/02/2014', '11/02/2014', '12/02/2014']
2421+
expected = DatetimeIndex([datetime(2014, 2, 10),
2422+
datetime(2014, 2, 11),
2423+
datetime(2014, 2, 12)])
2424+
idx1 = DatetimeIndex(arr, dayfirst=True)
2425+
idx2 = DatetimeIndex(np.array(arr), dayfirst=True)
2426+
idx3 = to_datetime(arr, dayfirst=True)
2427+
idx4 = to_datetime(np.array(arr), dayfirst=True)
2428+
idx5 = DatetimeIndex(Index(arr), dayfirst=True)
2429+
idx6 = DatetimeIndex(Series(arr), dayfirst=True)
2430+
self.assert_(expected.equals(idx1))
2431+
self.assert_(expected.equals(idx2))
2432+
self.assert_(expected.equals(idx3))
2433+
self.assert_(expected.equals(idx4))
2434+
self.assert_(expected.equals(idx5))
2435+
self.assert_(expected.equals(idx6))
2436+
24182437
def test_dti_snap(self):
24192438
dti = DatetimeIndex(['1/1/2002', '1/2/2002', '1/3/2002', '1/4/2002',
24202439
'1/5/2002', '1/6/2002', '1/7/2002'], freq='D')

0 commit comments

Comments
 (0)