Skip to content

Commit 9e99a5e

Browse files
committed
BUG: fix scalar date_parser issue in read_csv. close #3071
1 parent 6e7b37b commit 9e99a5e

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

RELEASE.rst

+1
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ pandas 0.11.0
152152
- Formatting of an index that has ``nan`` was inconsistent or wrong (would fill from
153153
other values), (GH2850_)
154154
- Unstack of a frame with no nans would always cause dtype upcasting (GH2929_)
155+
- Fix scalar datetime.datetime parsing bug in read_csv (GH3071_)
155156

156157
.. _GH622: https://github.com/pydata/pandas/issues/622
157158
.. _GH797: https://github.com/pydata/pandas/issues/797

pandas/io/parsers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,10 @@ def converter(*date_cols):
15531553
return lib.try_parse_dates(strs, dayfirst=dayfirst)
15541554
else:
15551555
try:
1556-
return date_parser(*date_cols)
1556+
result = date_parser(*date_cols)
1557+
if isinstance(result, datetime.datetime):
1558+
raise Exception('scalar parser')
1559+
return result
15571560
except Exception:
15581561
try:
15591562
return generic_parser(date_parser, *date_cols)

pandas/io/tests/test_parsers.py

+15
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,21 @@ def test_converter_index_col_bug(self):
365365
tm.assert_frame_equal(rs, xp)
366366
self.assert_(rs.index.name == xp.index.name)
367367

368+
def test_date_parser_int_bug(self):
369+
# #3071
370+
log_file = StringIO(
371+
'posix_timestamp,elapsed,sys,user,queries,query_time,rows,'
372+
'accountid,userid,contactid,level,silo,method\n'
373+
'1343103150,0.062353,0,4,6,0.01690,3,'
374+
'12345,1,-1,3,invoice_InvoiceResource,search\n'
375+
)
376+
377+
def f(posix_string):
378+
return datetime.utcfromtimestamp(int(posix_string))
379+
380+
# it works!
381+
read_csv(log_file, index_col=0, parse_dates=0, date_parser=f)
382+
368383
def test_multiple_skts_example(self):
369384
data = "year, month, a, b\n 2001, 01, 0.0, 10.\n 2001, 02, 1.1, 11."
370385
pass

0 commit comments

Comments
 (0)