Repository: pandas-dev/pandas

Issue:
dayfirst non working when constructing a pd.DatetimeIndex with an array
assert pd.DatetimeIndex(['10/02/2014'], dayfirst=True) == pd.DatetimeIndex(np.asarray(['10/02/2014'], dtype=object), dayfirst=True)



Discussion:
this is a known bug, see here: https://github.com/pydata/pandas/issues/3341

@jreback I think this is another issue:

```
In [5]: pd.to_datetime(['10/02/2014'], dayfirst=True)[0]
Out[5]: Timestamp('2014-02-10 00:00:00', tz=None)

In [6]: pd.to_datetime(np.asarray(['10/02/2014'], dtype='object'), dayfirst=True)[0]
Out[6]: Timestamp('2014-02-10 00:00:00', tz=None)

In [8]: pd.DatetimeIndex(np.asarray(['10/02/2014'], dtype='object'), dayfirst=True)[0]
Out[8]: Timestamp('2014-10-02 00:00:00', tz=None)
```

There seems to be a difference between `to_datetime` and `DatetimeIndex`. 
Also, the other issue is about `dayfirst` not being strict, so no error for eg "10/14/2014". While here, it is just wrong even in a case that is perfectly possible for dayfirst.

@jorisvandenbossche I agree dayfirst is non-strict / wrong because dateutil is broken IIRC

I had closed this because it is a dupe of #3341 but not prob


Resolving patch:
diff --git a/pandas/tseries/index.py b/pandas/tseries/index.py
--- 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:


Test patch:
diff --git a/pandas/tseries/tests/test_timeseries.py b/pandas/tseries/tests/test_timeseries.py
--- 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')