to_csv() with a single level row MultiIndex does not produce the desired output · Issue #19589 · pandas-dev/pandas (original) (raw)
Code Sample, a copy-pastable example if possible
import pandas as pd; idx_1d = pd.MultiIndex(levels=[[1.0]], labels=[[0]], names=['x']) idx_2d = pd.MultiIndex(levels=[[1.], [2.]], labels=[[0], [0]], names=['x', 'y'])
DataFrame
print(pd.DataFrame({'data': [1]}, idx_2d).to_csv()) x,y,data 1.0,2.0,1
print(pd.DataFrame({'data': [1]}, idx_1d).to_csv()) x,data "('1.0',)",1
Series
print(pd.Series([1], idx_2d).to_csv()) 1.0,2.0,1
print(pd.Series([1], idx_1d).to_csv()) "('1.0',)",1
Problem description
The output for index.nlevels==1
should be the same as for index.nlevels==2
, with one column less. That matches the output from other index types and was the output produced by pandas up to 0.20.3.
Expected Output
print(pd.DataFrame({'data': [1]}, idx_1d).to_csv()) x,data 1.0,1
print(pd.Series([1], idx_1d).to_csv()) 1.0,1
Output of pd.show_versions()
INSTALLED VERSIONS ------------------ commit: None python: 3.6.0.final.0 python-bits: 64 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 58 Stepping 9, GenuineIntel byteorder: little LC_ALL: None LANG: None LOCALE: None.None
pandas: 0.22.0
pytest: 3.2.1
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.11.3
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 5.1.0
sphinx: 1.5.1
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2016.10
blosc: None
bottleneck: 1.2.0
tables: 3.4.2
numexpr: 2.6.1
feather: None
matplotlib: 2.0.0
openpyxl: 2.4.1
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 4.1.1
bs4: 4.5.3
html5lib: None
sqlalchemy: 1.1.5
pymysql: None
psycopg2: None
jinja2: 2.9.4
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None