Skip to content

Commit 45e10ff

Browse files
committed
BUG: GH36928 Add whatsnew entry and move fix to _validate_names function instead of is_list_like
1 parent 5289000 commit 45e10ff

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

doc/source/whatsnew/v1.1.4.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17-
-
17+
- `dict_keys` not being accepted as valid column names by `pandas.io.parsers._validate_names`
1818

1919
.. ---------------------------------------------------------------------------
2020

pandas/_libs/lib.pyx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,8 +1019,6 @@ cdef inline bint c_is_list_like(object obj, bint allow_sets) except -1:
10191019
and not (util.is_array(obj) and obj.ndim == 0)
10201020
# exclude sets if allow_sets is False
10211021
and not (allow_sets is False and isinstance(obj, abc.Set))
1022-
# allow dict_keys objects
1023-
or isinstance(obj, abc.KeysView)
10241022
)
10251023

10261024

pandas/io/parsers.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,9 @@ def _validate_names(names):
420420
if names is not None:
421421
if len(names) != len(set(names)):
422422
raise ValueError("Duplicate names are not allowed.")
423-
if not is_list_like(names, allow_sets=False):
423+
if not is_list_like(names, allow_sets=False) and not isinstance(
424+
names, abc.KeysView
425+
):
424426
raise ValueError("Names should be an ordered collection.")
425427

426428

pandas/tests/io/parser/test_common.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,9 +2224,12 @@ def test_read_table_delim_whitespace_non_default_sep(all_parsers):
22242224

22252225

22262226
def test_dict_keys_as_names(all_parsers):
2227-
data = "a,b\n1,2"
2227+
# GH: 36928
2228+
data = "1,2"
22282229

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

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

0 commit comments

Comments
 (0)