-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Don't warn if default conflicts with dialect #23775
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
jreback
merged 1 commit into
pandas-dev:master
from
forking-repos:dialect-conflict-warning
Nov 19, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -16,6 +16,14 @@ | |
import pandas.util.testing as tm | ||
|
||
|
||
@pytest.fixture | ||
def custom_dialect(): | ||
dialect_name = "weird" | ||
dialect_kwargs = dict(doublequote=False, escapechar="~", delimiter=":", | ||
skipinitialspace=False, quotechar="~", quoting=3) | ||
return dialect_name, dialect_kwargs | ||
|
||
|
||
def test_dialect(all_parsers): | ||
parser = all_parsers | ||
data = """\ | ||
|
@@ -26,10 +34,7 @@ def test_dialect(all_parsers): | |
|
||
dia = csv.excel() | ||
dia.quoting = csv.QUOTE_NONE | ||
|
||
# Conflicting dialect quoting. | ||
with tm.assert_produces_warning(ParserWarning): | ||
df = parser.read_csv(StringIO(data), dialect=dia) | ||
df = parser.read_csv(StringIO(data), dialect=dia) | ||
|
||
data = """\ | ||
label1,label2,label3 | ||
|
@@ -53,14 +58,10 @@ def test_dialect_str(all_parsers): | |
"fruit": ["apple", "pear"], | ||
"vegetable": ["broccoli", "tomato"] | ||
}) | ||
csv.register_dialect(dialect_name, delimiter=":") | ||
|
||
# Conflicting dialect delimiter. | ||
with tm.assert_produces_warning(ParserWarning): | ||
with tm.with_csv_dialect(dialect_name, delimiter=":"): | ||
df = parser.read_csv(StringIO(data), dialect=dialect_name) | ||
|
||
tm.assert_frame_equal(df, exp) | ||
csv.unregister_dialect(dialect_name) | ||
tm.assert_frame_equal(df, exp) | ||
|
||
|
||
def test_invalid_dialect(all_parsers): | ||
|
@@ -75,17 +76,60 @@ class InvalidDialect(object): | |
parser.read_csv(StringIO(data), dialect=InvalidDialect) | ||
|
||
|
||
@pytest.mark.parametrize("delimiter", [",", "."]) | ||
def test_dialect_conflict(all_parsers, delimiter): | ||
data = "a,b\n1,2" | ||
dialect = "excel" | ||
@pytest.mark.parametrize("arg", [None, "doublequote", "escapechar", | ||
"skipinitialspace", "quotechar", "quoting"]) | ||
@pytest.mark.parametrize("value", ["dialect", "default", "other"]) | ||
def test_dialect_conflict_except_delimiter(all_parsers, custom_dialect, | ||
arg, value): | ||
# see gh-23761. | ||
dialect_name, dialect_kwargs = custom_dialect | ||
parser = all_parsers | ||
|
||
expected = DataFrame({"a": [1], "b": [2]}) | ||
data = "a:b\n1:2" | ||
|
||
warning_klass = None | ||
kwds = dict() | ||
|
||
# arg=None tests when we pass in the dialect without any other arguments. | ||
if arg is not None: | ||
if "value" == "dialect": # No conflict --> no warning. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. presumably this was mean to be |
||
kwds[arg] = dialect_kwargs[arg] | ||
elif "value" == "default": # Default --> no warning. | ||
from pandas.io.parsers import _parser_defaults | ||
kwds[arg] = _parser_defaults[arg] | ||
else: # Non-default + conflict with dialect --> warning. | ||
warning_klass = ParserWarning | ||
kwds[arg] = "blah" | ||
|
||
with tm.with_csv_dialect(dialect_name, **dialect_kwargs): | ||
with tm.assert_produces_warning(warning_klass): | ||
result = parser.read_csv(StringIO(data), | ||
dialect=dialect_name, **kwds) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("kwargs,warning_klass", [ | ||
(dict(sep=","), None), # sep is default --> sep_override=True | ||
(dict(sep="."), ParserWarning), # sep isn't default --> sep_override=False | ||
(dict(delimiter=":"), None), # No conflict | ||
(dict(delimiter=None), None), # Default arguments --> sep_override=True | ||
(dict(delimiter=","), ParserWarning), # Conflict | ||
(dict(delimiter="."), ParserWarning), # Conflict | ||
], ids=["sep-override-true", "sep-override-false", | ||
"delimiter-no-conflict", "delimiter-default-arg", | ||
"delimiter-conflict", "delimiter-conflict2"]) | ||
def test_dialect_conflict_delimiter(all_parsers, custom_dialect, | ||
kwargs, warning_klass): | ||
# see gh-23761. | ||
dialect_name, dialect_kwargs = custom_dialect | ||
parser = all_parsers | ||
|
||
expected = DataFrame({"a": [1], "b": [2]}) | ||
warning_klass = None if delimiter == "," else ParserWarning | ||
data = "a:b\n1:2" | ||
|
||
with tm.assert_produces_warning(warning_klass): | ||
result = parser.read_csv(StringIO(data), | ||
delimiter=delimiter, | ||
dialect=dialect) | ||
tm.assert_frame_equal(result, expected) | ||
with tm.with_csv_dialect(dialect_name, **dialect_kwargs): | ||
with tm.assert_produces_warning(warning_klass): | ||
result = parser.read_csv(StringIO(data), | ||
dialect=dialect_name, **kwargs) | ||
tm.assert_frame_equal(result, expected) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.