Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ including other versions of pandas.

Fixed regressions
~~~~~~~~~~~~~~~~~
-
- `dict_keys` not being accepted as valid column names by `pandas.io.parsers._validate_names`

.. ---------------------------------------------------------------------------

Expand Down
2 changes: 0 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,6 @@ 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
4 changes: 3 additions & 1 deletion pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ def _validate_names(names):
if names is not None:
if len(names) != len(set(names)):
raise ValueError("Duplicate names are not allowed.")
if not is_list_like(names, allow_sets=False):
if not is_list_like(names, allow_sets=False) and not isinstance(
names, abc.KeysView
):
raise ValueError("Names should be an ordered collection.")


Expand Down
7 changes: 5 additions & 2 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2224,9 +2224,12 @@ def test_read_table_delim_whitespace_non_default_sep(all_parsers):


def test_dict_keys_as_names(all_parsers):
data = "a,b\n1,2"
# GH: 36928
data = "1,2"

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

parser.read_csv(StringIO(data), names=keys)
result = parser.read_csv(StringIO(data), names=keys)
expected = DataFrame({"a": [1], "b": [2]})
tm.assert_frame_equal(result, expected)