Skip to content

Commit b8df975

Browse files
author
Chang She
committed
BUG: parser bug when parse_dates is string #1544
1 parent 6078fba commit b8df975

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

pandas/io/parsers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -864,14 +864,15 @@ def _should_parse_dates(self, i):
864864
if isinstance(self.parse_dates, bool):
865865
return self.parse_dates
866866
else:
867-
to_parse = self.parse_dates # int/string or list of int or string
868-
869867
if np.isscalar(self.index_col):
870868
name = self.index_name
871869
else:
872870
name = self.index_name[i]
873871

874-
return i in to_parse or name in to_parse
872+
if np.isscalar(self.parse_dates):
873+
return (i == self.parse_dates) or (name == self.parse_dates)
874+
else:
875+
return (i in self.parse_dates) or (name in self.parse_dates)
875876

876877
def _conv_date(self, *date_cols):
877878
if self.date_parser is None:

pandas/io/tests/test_parsers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import pandas.lib as lib
2121
from pandas.util import py3compat
2222
from pandas.lib import Timestamp
23+
from pandas.tseries.index import date_range
2324

2425
from numpy.testing.decorators import slow
2526
from pandas.io.date_converters import (
@@ -586,6 +587,21 @@ def test_parse_dates_implicit_first_col(self):
586587
self.assert_(isinstance(df.index[0], (datetime, np.datetime64, Timestamp)))
587588
assert_frame_equal(df, expected)
588589

590+
def test_parse_dates_string(self):
591+
data = """date,A,B,C
592+
20090101,a,1,2
593+
20090102,b,3,4
594+
20090103,c,4,5
595+
"""
596+
rs = read_csv(StringIO(data), index_col='date', parse_dates='date')
597+
idx = date_range('1/1/2009', periods=3).asobject
598+
idx.name = 'date'
599+
xp = DataFrame({'A': ['a', 'b', 'c'],
600+
'B': [1, 3, 4],
601+
'C': [2, 4, 5]}, idx)
602+
assert_frame_equal(rs, xp)
603+
604+
589605
def test_parse_dates_column_list(self):
590606
from pandas.core.datetools import to_datetime
591607

0 commit comments

Comments
 (0)