diff --git a/doc/source/whatsnew/v0.15.1.txt b/doc/source/whatsnew/v0.15.1.txt index e4fc085d4b16a..e628829196e95 100644 --- a/doc/source/whatsnew/v0.15.1.txt +++ b/doc/source/whatsnew/v0.15.1.txt @@ -213,6 +213,7 @@ Bug Fixes - Bug in ``Categorical`` reflected comparison operator raising if the first argument was a numpy array scalar (e.g. np.int64) (:issue:`8658`) - Bug in Panel indexing with a list-like (:issue:`8710`) - Compat issue is ``DataFrame.dtypes`` when ``options.mode.use_inf_as_null`` is True (:issue:`8722`) +- Bug in ``read_csv``, ``dialect`` parameter would not take a string (:issue: `8703`) diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 2034d4fa899ce..8f8e3151d56e6 100644 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -526,6 +526,8 @@ def __init__(self, f, engine=None, **kwds): if kwds.get('dialect') is not None: dialect = kwds['dialect'] + if dialect in csv.list_dialects(): + dialect = csv.get_dialect(dialect) kwds['delimiter'] = dialect.delimiter kwds['doublequote'] = dialect.doublequote kwds['escapechar'] = dialect.escapechar diff --git a/pandas/io/tests/test_parsers.py b/pandas/io/tests/test_parsers.py index 44daeaca4ed32..8440d45ffb4be 100644 --- a/pandas/io/tests/test_parsers.py +++ b/pandas/io/tests/test_parsers.py @@ -191,6 +191,21 @@ def test_dialect(self): 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'] + }) + dia = csv.register_dialect('mydialect', delimiter=':') + df = self.read_csv(StringIO(data), dialect='mydialect') + tm.assert_frame_equal(df, exp) + csv.unregister_dialect('mydialect') + def test_1000_sep(self): data = """A|B|C 1|2,334|5