Skip to content

BUG: GH36928 Allow dict_keys to be used as column names by read_csv #36937

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
merged 13 commits into from
Oct 10, 2020
2 changes: 2 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,8 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
and not (util.is_array(obj) and obj.ndim == 0)
# exclude sets if allow_sets is False
and not (allow_sets is False and isinstance(obj, abc.Set))
# allow dict_keys objects
or isinstance(obj, abc.KeysView)
)


Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2221,3 +2221,12 @@ def test_read_table_delim_whitespace_non_default_sep(all_parsers):
)
with pytest.raises(ValueError, match=msg):
parser.read_table(f, delim_whitespace=True, sep=",")


def test_dict_keys_as_names(all_parsers):
data = "a,b\n1,2"

keys = {"a": int, "b": int}.keys()
parser = all_parsers

parser.read_csv(StringIO(data), names=keys)