Skip to content

BUG: DatetimeIndex cannot parse string ndarray with dayfirst #7038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
31 changes: 20 additions & 11 deletions pandas/tseries/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 19 additions & 0 deletions pandas/tseries/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down