Skip to content

Commit 4a3da96

Browse files
y-pwesm
y-p
authored andcommitted
BUG: when parsing partial date, use sane defaults for missing fields GH2618
dateutil uses the current date to fill in missing fields, this is desirable for year and month, but will cause an exception when code is run on the 31st day of a month, and trying to parse "2000 2" as feb 2000, because feb doesn't have 31 days. So we default to current year and month, but always the first of the month.
1 parent e68e7b8 commit 4a3da96

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

RELEASE.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pandas 0.10.1
103103
- Upconvert datetime + datetime64 values when concatenating frames (GH2624_)
104104
- Raise a more helpful error message in merge operations when one DataFrame
105105
has duplicate columns (GH2649_)
106+
- Fix partial date parsing issue occuring only when code is run at EOM (GH2618_)
106107

107108
**API Changes**
108109

@@ -140,6 +141,7 @@ pandas 0.10.1
140141
.. _GH2700: https://github.com/pydata/pandas/issues/2700
141142
.. _GH2694: https://github.com/pydata/pandas/issues/2694
142143
.. _GH2686: https://github.com/pydata/pandas/issues/2686
144+
.. _GH2618: https://github.com/pydata/pandas/issues/2618
143145

144146
pandas 0.10.0
145147
=============

pandas/src/inference.pyx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,20 +513,24 @@ def convert_sql_column(x):
513513
return maybe_convert_objects(x, try_float=1)
514514

515515
def try_parse_dates(ndarray[object] values, parser=None,
516-
dayfirst=False):
516+
dayfirst=False,default=None):
517517
cdef:
518518
Py_ssize_t i, n
519519
ndarray[object] result
520520

521-
from datetime import datetime
521+
from datetime import datetime, timedelta
522522

523523
n = len(values)
524524
result = np.empty(n, dtype='O')
525525

526526
if parser is None:
527+
if default is None: # GH2618
528+
date=datetime.now()
529+
default=datetime(date.year,date.month,1)
530+
527531
try:
528532
from dateutil.parser import parse
529-
parse_date = lambda x: parse(x, dayfirst=dayfirst)
533+
parse_date = lambda x: parse(x, dayfirst=dayfirst,default=default)
530534
except ImportError: # pragma: no cover
531535
def parse_date(s):
532536
try:
@@ -560,22 +564,26 @@ def try_parse_dates(ndarray[object] values, parser=None,
560564

561565
def try_parse_date_and_time(ndarray[object] dates, ndarray[object] times,
562566
date_parser=None, time_parser=None,
563-
dayfirst=False):
567+
dayfirst=False,default=None):
564568
cdef:
565569
Py_ssize_t i, n
566570
ndarray[object] result
567571

568-
from datetime import date, time, datetime
572+
from datetime import date, time, datetime, timedelta
569573

570574
n = len(dates)
571575
if len(times) != n:
572576
raise ValueError('Length of dates and times must be equal')
573577
result = np.empty(n, dtype='O')
574578

575579
if date_parser is None:
580+
if default is None: # GH2618
581+
date=datetime.now()
582+
default=datetime(date.year,date.month,1)
583+
576584
try:
577585
from dateutil.parser import parse
578-
parse_date = lambda x: parse(x, dayfirst=dayfirst)
586+
parse_date = lambda x: parse(x, dayfirst=dayfirst, default=default)
579587
except ImportError: # pragma: no cover
580588
def parse_date(s):
581589
try:

0 commit comments

Comments
 (0)