Skip to content

Commit 6876725

Browse files
committed
BUG: dayfirst and yearfirst in DatetimeIndex constructor #1769
1 parent 67ca18d commit 6876725

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

pandas/tseries/index.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ def __new__(cls, data=None,
138138
copy=False, name=None, tz=None,
139139
verify_integrity=True, normalize=False, **kwds):
140140

141+
dayfirst = kwds.pop('dayfirst', None)
142+
yearfirst = kwds.pop('yearfirst', None)
141143
warn = False
142144
if 'offset' in kwds and kwds['offset']:
143145
freq = kwds['offset']
@@ -188,13 +190,15 @@ def __new__(cls, data=None,
188190

189191
# try a few ways to make it datetime64
190192
if lib.is_string_array(data):
191-
data = _str_to_dt_array(data, offset)
193+
data = _str_to_dt_array(data, offset, dayfirst=dayfirst,
194+
yearfirst=yearfirst)
192195
else:
193196
data = tools.to_datetime(data)
194197
data.offset = offset
195198

196199
if issubclass(data.dtype.type, basestring):
197-
subarr = _str_to_dt_array(data, offset)
200+
subarr = _str_to_dt_array(data, offset, dayfirst=dayfirst,
201+
yearfirst=yearfirst)
198202
elif issubclass(data.dtype.type, np.datetime64):
199203
if isinstance(data, DatetimeIndex):
200204
if tz is None:
@@ -1481,9 +1485,9 @@ def _to_m8(key):
14811485

14821486

14831487

1484-
def _str_to_dt_array(arr, offset=None):
1488+
def _str_to_dt_array(arr, offset=None, dayfirst=None, yearfirst=None):
14851489
def parser(x):
1486-
result = parse_time_string(x, offset)
1490+
result = parse_time_string(x, offset, dayfirst=dayfirst, yearfirst=None)
14871491
return result[0]
14881492

14891493
arr = np.asarray(arr, dtype=object)
@@ -1520,4 +1524,3 @@ def _utc_naive(dt):
15201524
dt = dt.tz_convert('utc').replace(tzinfo=None)
15211525

15221526
return dt
1523-

pandas/tseries/tests/test_timeseries.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,6 +1789,11 @@ def test_datetimeindex_constructor(self):
17891789
arr = to_datetime(['1/1/2005', '1/2/2005', 'Jan 3, 2005', '2005-01-04'])
17901790
idx6 = DatetimeIndex(arr)
17911791

1792+
idx7 = DatetimeIndex(['12/05/2007', '25/01/2008'], dayfirst=True)
1793+
idx8 = DatetimeIndex(['2007/05/12', '2008/01/25'], dayfirst=False,
1794+
yearfirst=True)
1795+
self.assert_(idx7.equals(idx8))
1796+
17921797
for other in [idx2, idx3, idx4, idx5, idx6]:
17931798
self.assert_( (idx1.values == other.values).all() )
17941799

pandas/tseries/tools.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class DateParseError(ValueError):
113113
qpat2 = re.compile(r'(\d\d)Q(\d)')
114114
ypat = re.compile(r'(\d\d\d\d)$')
115115

116-
def parse_time_string(arg, freq=None):
116+
def parse_time_string(arg, freq=None, dayfirst=None, yearfirst=None):
117117
"""
118118
Try hard to parse datetime string, leveraging dateutil plus some extra
119119
goodies like quarter recognition.
@@ -123,6 +123,10 @@ def parse_time_string(arg, freq=None):
123123
arg : basestring
124124
freq : str or DateOffset, default None
125125
Helps with interpreting time string if supplied
126+
dayfirst : bool, default None
127+
If None uses default from print_config
128+
yearfirst : bool, default None
129+
If None uses default from print_config
126130
127131
Returns
128132
-------
@@ -196,8 +200,10 @@ def parse_time_string(arg, freq=None):
196200
if mresult:
197201
return mresult
198202

199-
dayfirst = print_config.date_dayfirst
200-
yearfirst = print_config.date_yearfirst
203+
if dayfirst is None:
204+
dayfirst = print_config.date_dayfirst
205+
if yearfirst is None:
206+
yearfirst = print_config.date_yearfirst
201207

202208
try:
203209
parsed = parse(arg, dayfirst=dayfirst, yearfirst=yearfirst)
@@ -272,4 +278,3 @@ def ole2datetime(oledt):
272278
raise Exception("Value is outside of acceptable range: %s " % val)
273279

274280
return OLE_TIME_ZERO + timedelta(days=val)
275-

0 commit comments

Comments
 (0)