BUG: dict_keys
cannot be used as pd.read_csv
's names
parameter · Issue #36928 · pandas-dev/pandas (original) (raw)
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of pandas.
- (optional) I have confirmed this bug exists on the master branch of pandas.
Code Sample, a copy-pastable example
test.csv
:
import pandas as pd data = {'a': 10, 'b': 20} pd.read_csv('test.csv', names=data.keys())
Error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-packages/pandas/io/parsers.py", line 691, in read_csv
return _read(filepath_or_buffer, kwds)
File "/usr/local/lib/python3.8/dist-packages/pandas/io/parsers.py", line 451, in _read
_validate_names(kwds.get("names", None))
File "/usr/local/lib/python3.8/dist-packages/pandas/io/parsers.py", line 424, in _validate_names
raise ValueError("Names should be an ordered collection.")
ValueError: Names should be an ordered collection.
Problem description
When a dict_keys
object is passed, the keys aren't used as names. This is because dict_keys
isn't list_like (
if not is_list_like(names, allow_sets=False): |
---|
) or indexable, so the issue is understandable, but it would be nice to be able to pass dict_keys
without issue.
Expected Output
Passing the dict keys to names shouldn't fail.
Output of pd.show_versions()
INSTALLED VERSIONS
commit : 4e55346
python : 3.8.3.final.0
python-bits : 64
OS : Linux
OS-release : 4.15.0-118-generic
Version : #119-Ubuntu SMP Tue Sep 8 12:30:01 UTC 2020
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : None
LOCALE : en_US.UTF-8
pandas : 1.2.0.dev0+632.g4e553464f
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.1.1
setuptools : 46.1.3
Cython : None
pytest : None
hypothesis : None
sphinx : 1.6.7
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : 0.999999999
pymysql : None
psycopg2 : None
jinja2 : 2.10
IPython : None
pandas_datareader: None
bs4 : 4.6.0
bottleneck : None
fsspec : None
fastparquet : None
gcsfs : None
matplotlib : 3.2.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pyxlsb : None
s3fs : None
scipy : 1.4.1
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
numba : None