Skip to content

Commit bc19114

Browse files
abmyiiKevin D Smith
authored and
Kevin D Smith
committed
BUG: GH36928 Allow dict_keys to be used as column names by read_csv (pandas-dev#36937)
1 parent deb10ac commit bc19114

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ including other versions of pandas.
1414

1515
Fixed regressions
1616
~~~~~~~~~~~~~~~~~
17+
- Fixed regression in :func:`read_csv` raising a ``ValueError`` when ``names`` was of type ``dict_keys`` (:issue:`36928`)
1718
- Fixed regression where attempting to mutate a :class:`DateOffset` object would no longer raise an ``AttributeError`` (:issue:`36940`)
1819
- Fixed regression where :meth:`DataFrame.agg` would fail with :exc:`TypeError` when passed positional arguments to be passed on to the aggregation function (:issue:`36948`).
1920
- Fixed regression in :class:`RollingGroupby` with ``sort=False`` not being respected (:issue:`36889`)

pandas/io/parsers.py

+3-1
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 (
424+
is_list_like(names, allow_sets=False) or isinstance(names, abc.KeysView)
425+
):
424426
raise ValueError("Names should be an ordered collection.")
425427

426428

pandas/tests/io/parser/test_common.py

+12
Original file line numberDiff line numberDiff line change
@@ -2241,3 +2241,15 @@ def test_read_table_delim_whitespace_non_default_sep(all_parsers, delimiter):
22412241

22422242
with pytest.raises(ValueError, match=msg):
22432243
parser.read_table(f, delim_whitespace=True, delimiter=delimiter)
2244+
2245+
2246+
def test_dict_keys_as_names(all_parsers):
2247+
# GH: 36928
2248+
data = "1,2"
2249+
2250+
keys = {"a": int, "b": int}.keys()
2251+
parser = all_parsers
2252+
2253+
result = parser.read_csv(StringIO(data), names=keys)
2254+
expected = DataFrame({"a": [1], "b": [2]})
2255+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)