forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUG, DOC: Improve dialect handling in read_csv
1) Update documentation about how the dialect parameter is handled. 2) Verify that the dialect parameter passed in is valid before accessing the dialect attributes. Closes pandas-devgh-14898.
- Loading branch information
Showing
5 changed files
with
110 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Tests that dialects are properly handled during parsing | ||
for all of the parsers defined in parsers.py | ||
""" | ||
|
||
import csv | ||
|
||
from pandas import DataFrame | ||
from pandas.compat import StringIO | ||
from pandas.io.common import ParserWarning | ||
|
||
import pandas.util.testing as tm | ||
|
||
|
||
class DialectTests(object): | ||
|
||
def test_dialect(self): | ||
data = """\ | ||
label1,label2,label3 | ||
index1,"a,c,e | ||
index2,b,d,f | ||
""" | ||
|
||
dia = csv.excel() | ||
dia.quoting = csv.QUOTE_NONE | ||
with tm.assert_produces_warning(ParserWarning): | ||
df = self.read_csv(StringIO(data), dialect=dia) | ||
|
||
data = '''\ | ||
label1,label2,label3 | ||
index1,a,c,e | ||
index2,b,d,f | ||
''' | ||
exp = self.read_csv(StringIO(data)) | ||
exp.replace('a', '"a', inplace=True) | ||
tm.assert_frame_equal(df, exp) | ||
|
||
def test_dialect_str(self): | ||
data = """\ | ||
fruit:vegetable | ||
apple:brocolli | ||
pear:tomato | ||
""" | ||
exp = DataFrame({ | ||
'fruit': ['apple', 'pear'], | ||
'vegetable': ['brocolli', 'tomato'] | ||
}) | ||
csv.register_dialect('mydialect', delimiter=':') | ||
with tm.assert_produces_warning(ParserWarning): | ||
df = self.read_csv(StringIO(data), dialect='mydialect') | ||
|
||
tm.assert_frame_equal(df, exp) | ||
csv.unregister_dialect('mydialect') | ||
|
||
def test_dialect_conflict(self): | ||
data = 'a,b\n1,2' | ||
dialect = 'excel' | ||
exp = DataFrame({'a': [1], 'b': [2]}) | ||
|
||
with tm.assert_produces_warning(None): | ||
df = self.read_csv(StringIO(data), delimiter=',', dialect=dialect) | ||
tm.assert_frame_equal(df, exp) | ||
|
||
with tm.assert_produces_warning(ParserWarning): | ||
df = self.read_csv(StringIO(data), delimiter='.', dialect=dialect) | ||
tm.assert_frame_equal(df, exp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters