Example #1
0
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.
            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)
Example #2
0
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.
            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)
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]})
    data = "a:b\n1:2"

    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)
Example #4
0
def test_dialect_str(all_parsers):
    dialect_name = "mydialect"
    parser = all_parsers
    data = """\
fruit:vegetable
apple:broccoli
pear:tomato
"""
    exp = DataFrame({
        "fruit": ["apple", "pear"],
        "vegetable": ["broccoli", "tomato"]
    })

    with tm.with_csv_dialect(dialect_name, delimiter=":"):
        df = parser.read_csv(StringIO(data), dialect=dialect_name)
        tm.assert_frame_equal(df, exp)
Example #5
0
def test_dialect_str(all_parsers):
    dialect_name = "mydialect"
    parser = all_parsers
    data = """\
fruit:vegetable
apple:broccoli
pear:tomato
"""
    exp = DataFrame({
        "fruit": ["apple", "pear"],
        "vegetable": ["broccoli", "tomato"]
    })

    with tm.with_csv_dialect(dialect_name, delimiter=":"):
        df = parser.read_csv(StringIO(data), dialect=dialect_name)
        tm.assert_frame_equal(df, exp)