Skip to content

BUG: Don't parse index column as numeric when parse_dates=True #14077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.19.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,7 @@ Bug Fixes
- Bug in ``Categorical.from_codes()`` where an unhelpful error was raised when an invalid ``ordered`` parameter was passed in (:issue:`14058`)
- Bug in ``Series`` construction from a tuple of integers on windows not returning default dtype (int64) (:issue:`13646`)

- Bug in ``pd.read_csv()`` where the index columns were being incorrectly parsed when parsed as dates with a ``thousands`` parameter (:issue:`14066`)
- Bug in ``.groupby(..).resample(..)`` when the same object is called multiple times (:issue:`13174`)
- Bug in ``.to_records()`` when index name is a unicode string (:issue:`13172`)

Expand Down
15 changes: 15 additions & 0 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,13 @@ def _set(x):
else:
_set(val)

elif self.parse_dates:
if isinstance(self.index_col, list):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_list_like? (I don't know if its coerced to a list before this), same below

Copy link
Member Author

@gfyoung gfyoung Aug 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We expect parse_dates to a be bool, list, or dict per the docs. This is explicitly validated as well (see here), so is_list_like is unnecessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to handle dict at this point? (or is that already transformed)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index_col can never be a dict per the docs.

If you're referring to parse_dates, parse_dates being a dict has a completely different meaning that is independent of the index_col.

for k in self.index_col:
_set(k)
elif self.index_col is not None:
_set(self.index_col)

def set_error_bad_lines(self, status):
self._reader.set_error_bad_lines(int(status))

Expand Down Expand Up @@ -1856,6 +1863,14 @@ def _set(x):
_set(k)
else:
_set(val)

elif self.parse_dates:
if isinstance(self.index_col, list):
for k in self.index_col:
_set(k)
elif self.index_col is not None:
_set(self.index_col)

return noconvert_columns

def _make_reader(self, f):
Expand Down
32 changes: 32 additions & 0 deletions pandas/io/tests/parser/parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,3 +474,35 @@ def test_parse_dates_empty_string(self):
result = self.read_csv(StringIO(data), parse_dates=["Date"],
na_filter=False)
self.assertTrue(result['Date'].isnull()[1])

def test_parse_dates_noconvert_thousands(self):
# see gh-14066
data = 'a\n04.15.2016'

expected = DataFrame([datetime(2016, 4, 15)], columns=['a'])
result = self.read_csv(StringIO(data), parse_dates=['a'],
thousands='.')
tm.assert_frame_equal(result, expected)

exp_index = DatetimeIndex(['2016-04-15'], name='a')
expected = DataFrame(index=exp_index)
result = self.read_csv(StringIO(data), index_col=0,
parse_dates=True, thousands='.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test index_col is a list-like

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

tm.assert_frame_equal(result, expected)

data = 'a,b\n04.15.2016,09.16.2013'

expected = DataFrame([[datetime(2016, 4, 15),
datetime(2013, 9, 16)]],
columns=['a', 'b'])
result = self.read_csv(StringIO(data), parse_dates=['a', 'b'],
thousands='.')
tm.assert_frame_equal(result, expected)

expected = DataFrame([[datetime(2016, 4, 15),
datetime(2013, 9, 16)]],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a dict for parse_dates as well

columns=['a', 'b'])
expected = expected.set_index(['a', 'b'])
result = self.read_csv(StringIO(data), index_col=[0, 1],
parse_dates=True, thousands='.')
tm.assert_frame_equal(result, expected)
37 changes: 16 additions & 21 deletions pandas/io/tests/parser/usecols.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
for all of the parsers defined in parsers.py
"""

from datetime import datetime
import nose

import numpy as np
import pandas.util.testing as tm

from pandas import DataFrame
from pandas import DataFrame, Index
from pandas.lib import Timestamp
from pandas.compat import StringIO

Expand Down Expand Up @@ -99,35 +98,31 @@ def test_usecols_index_col_False(self):

def test_usecols_index_col_conflict(self):
# see gh-4201: test that index_col as integer reflects usecols
data = """SecId,Time,Price,P2,P3
10000,2013-5-11,100,10,1
500,2013-5-12,101,11,1
"""
expected = DataFrame({'Price': [100, 101]}, index=[
datetime(2013, 5, 11), datetime(2013, 5, 12)])
expected.index.name = 'Time'
data = 'a,b,c,d\nA,a,1,one\nB,b,2,two'
expected = DataFrame({'c': [1, 2]}, index=Index(
['a', 'b'], name='b'))

df = self.read_csv(StringIO(data), usecols=[
'Time', 'Price'], parse_dates=True, index_col=0)
df = self.read_csv(StringIO(data), usecols=['b', 'c'],
index_col=0)
tm.assert_frame_equal(expected, df)

df = self.read_csv(StringIO(data), usecols=[
'Time', 'Price'], parse_dates=True, index_col='Time')
df = self.read_csv(StringIO(data), usecols=['b', 'c'],
index_col='b')
tm.assert_frame_equal(expected, df)

df = self.read_csv(StringIO(data), usecols=[
1, 2], parse_dates=True, index_col='Time')
df = self.read_csv(StringIO(data), usecols=[1, 2],
index_col='b')
tm.assert_frame_equal(expected, df)

df = self.read_csv(StringIO(data), usecols=[
1, 2], parse_dates=True, index_col=0)
df = self.read_csv(StringIO(data), usecols=[1, 2],
index_col=0)
tm.assert_frame_equal(expected, df)

expected = DataFrame(
{'P3': [1, 1], 'Price': (100, 101), 'P2': (10, 11)})
expected = expected.set_index(['Price', 'P2'])
df = self.read_csv(StringIO(data), usecols=[
'Price', 'P2', 'P3'], parse_dates=True, index_col=['Price', 'P2'])
{'b': ['a', 'b'], 'c': [1, 2], 'd': ('one', 'two')})
expected = expected.set_index(['b', 'c'])
df = self.read_csv(StringIO(data), usecols=['b', 'c', 'd'],
index_col=['b', 'c'])
tm.assert_frame_equal(expected, df)

def test_usecols_implicit_index_col(self):
Expand Down