Skip to content

Commit 0f72d3d

Browse files
thrasibulejreback
authored andcommitted
ENH: read_csv dialect parameter can take a string (GH8703)
1 parent a0ac41d commit 0f72d3d

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

doc/source/whatsnew/v0.15.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ Bug Fixes
256256
- Bug in ``Categorical`` reflected comparison operator raising if the first argument was a numpy array scalar (e.g. np.int64) (:issue:`8658`)
257257
- Bug in Panel indexing with a list-like (:issue:`8710`)
258258
- Compat issue is ``DataFrame.dtypes`` when ``options.mode.use_inf_as_null`` is True (:issue:`8722`)
259+
- Bug in ``read_csv``, ``dialect`` parameter would not take a string (:issue: `8703`)
259260

260261

261262

pandas/io/parsers.py

+2
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ def __init__(self, f, engine=None, **kwds):
526526

527527
if kwds.get('dialect') is not None:
528528
dialect = kwds['dialect']
529+
if dialect in csv.list_dialects():
530+
dialect = csv.get_dialect(dialect)
529531
kwds['delimiter'] = dialect.delimiter
530532
kwds['doublequote'] = dialect.doublequote
531533
kwds['escapechar'] = dialect.escapechar

pandas/io/tests/test_parsers.py

+15
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,21 @@ def test_dialect(self):
191191
exp.replace('a', '"a', inplace=True)
192192
tm.assert_frame_equal(df, exp)
193193

194+
def test_dialect_str(self):
195+
data = """\
196+
fruit:vegetable
197+
apple:brocolli
198+
pear:tomato
199+
"""
200+
exp = DataFrame({
201+
'fruit': ['apple', 'pear'],
202+
'vegetable': ['brocolli', 'tomato']
203+
})
204+
dia = csv.register_dialect('mydialect', delimiter=':')
205+
df = self.read_csv(StringIO(data), dialect='mydialect')
206+
tm.assert_frame_equal(df, exp)
207+
csv.unregister_dialect('mydialect')
208+
194209
def test_1000_sep(self):
195210
data = """A|B|C
196211
1|2,334|5

0 commit comments

Comments
 (0)