Skip to content

read_csv not respecting MultiIndex names #12518

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

Closed
toobaz opened this issue Mar 3, 2016 · 6 comments
Closed

read_csv not respecting MultiIndex names #12518

toobaz opened this issue Mar 3, 2016 · 6 comments

Comments

@toobaz
Copy link
Member

toobaz commented Mar 3, 2016

Code Sample, a copy-pastable example if possible

In [2]: pd.read_csv('/tmp/csv.csv', header=None)
Out[2]: 
   0  1  2
0  0  2  2
1  1  2  2
2  2  2  2

In [3]: mi = pd.MultiIndex.from_tuples([(1,2), (3,4)], names=['l1', 'l2'])

In [4]: pd.read_csv('/tmp/csv.csv', header=None, names=mi)
Out[4]: 
   1  3
   2  4
0  2  2
1  2  2
2  2  2

Expected Output

In [4]: pd.read_csv('/tmp/csv.csv', header=None, names=mi)
Out[4]: 
l1  1  3
l2  2  4
0   2  2
1   2  2
2   2  2

Might be worth fixing together with #7589 (just a rough guess, didn't look at the code)

output of pd.show_versions()

In [8]: pd.show_versions()

INSTALLED VERSIONS
------------------
commit: eba78032e927d2680850c88190f3bafe18cac6ba
python: 3.4.3.final.0
python-bits: 64
OS: Linux
OS-release: 4.3.0-1-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: it_IT.utf8

pandas: 0.18.0rc1+66.geba7803
nose: 1.3.6
pip: 1.5.6
setuptools: 20.1.1
Cython: 0.23.2
numpy: 1.10.0.post2
scipy: 0.16.0
statsmodels: 0.8.0.dev0+755fa81
xarray: None
IPython: 4.1.1
sphinx: 1.3.1
patsy: 0.3.0-dev
dateutil: 2.4.2
pytz: 2015.6
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.4.3
matplotlib: 1.5.dev1
openpyxl: None
xlrd: 0.9.4
xlwt: None
xlsxwriter: 0.7.3
lxml: None
bs4: 4.4.0
html5lib: 0.999
httplib2: 0.9.1
apiclient: None
sqlalchemy: 1.0.11
pymysql: None
psycopg2: None
jinja2: 2.8
@jreback
Copy link
Contributor

jreback commented Mar 3, 2016

you need to specify a list for the header, see http://pandas.pydata.org/pandas-docs/stable/io.html#reading-columns-with-a-multiindex

@jreback
Copy link
Contributor

jreback commented Mar 3, 2016

in the future pls supply examples as copy-pastable (use StringIO)

@toobaz
Copy link
Member Author

toobaz commented Mar 3, 2016

Sorry, the example could have also been simpler:

In [3]: mi = pd.MultiIndex.from_tuples([(1,2), (3,4)], names=['l1', 'l2'])

In [4]: pd.read_csv(StringIO('0,2,2\n1,2,2\n2,2,2'), names=mi)
Out[4]: 
   1  3
   2  4
0  2  2
1  2  2
2  2  2

(the problem did not lie in header)

@toobaz
Copy link
Member Author

toobaz commented Mar 3, 2016

(... which means that this is a bug, I think)

@jreback
Copy link
Contributor

jreback commented Mar 3, 2016

@toobaz that does't make any sense

names : array-like, default None
    List of column names to use. If file contains no header row, then you
    should explicitly pass header=None

what you are writing doesn't match the doc-string, nor the expected behavior.

@toobaz
Copy link
Member Author

toobaz commented Mar 3, 2016

Ooops, right, sorry. Too bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants