Skip to content

Commit 808c30c

Browse files
committed
BUG: don't let dateutil do weird stuff with empty strings. close #2263
1 parent 6a7c11c commit 808c30c

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ pandas 0.10.0
7272
- Respect dtype=object in DataFrame constructor (#2291)
7373
- Fix DatetimeIndex.join bug with tz-aware indexes and how='outer' (#2317)
7474
- pop(...) and del works with DataFrame with duplicate columns (#2349)
75+
- Treat empty strings as NA in date parsing (rather than let dateutil do
76+
something weird) (#2263)
7577

7678
pandas 0.9.1
7779
============

pandas/io/tests/test_parsers.py

+6
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,12 @@ def test_compact_ints(self):
17631763
ex_dtype = np.dtype([(str(i), 'u1') for i in range(4)])
17641764
self.assertEqual(result.dtype, ex_dtype)
17651765

1766+
def test_parse_dates_empty_string(self):
1767+
# #2263
1768+
s = StringIO("Date, test\n2012-01-01, 1\n,2")
1769+
result = pd.read_csv(s, parse_dates=["Date"], na_filter=False)
1770+
self.assertTrue(result['Date'].isnull()[1])
1771+
17661772
class TestCParserLowMemory(ParserTests, unittest.TestCase):
17671773

17681774
def read_csv(self, *args, **kwds):

pandas/src/inference.pyx

+8-2
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,10 @@ def try_parse_dates(ndarray[object] values, parser=None,
544544
# EAFP here
545545
try:
546546
for i from 0 <= i < n:
547-
result[i] = parse_date(values[i])
547+
if values[i] == '':
548+
result[i] = np.nan
549+
else:
550+
result[i] = parse_date(values[i])
548551
except Exception:
549552
# failed
550553
return values
@@ -553,7 +556,10 @@ def try_parse_dates(ndarray[object] values, parser=None,
553556

554557
try:
555558
for i from 0 <= i < n:
556-
result[i] = parse_date(values[i])
559+
if values[i] == '':
560+
result[i] = np.nan
561+
else:
562+
result[i] = parse_date(values[i])
557563
except Exception:
558564
# raise if passed parser and it failed
559565
raise

0 commit comments

Comments
 (0)