Skip to content
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

to_csv() with a single level row MultiIndex does not produce the desired output #19589

Closed
joe1gi opened this issue Feb 8, 2018 · 2 comments · Fixed by #19831
Closed

to_csv() with a single level row MultiIndex does not produce the desired output #19589

joe1gi opened this issue Feb 8, 2018 · 2 comments · Fixed by #19831
Labels
API Design IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@joe1gi
Copy link

joe1gi commented Feb 8, 2018

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

@joe1gi joe1gi changed the title to_csv() with a single level MultiIndex does not produce the desired output to_csv() with a single level (row) MultiIndex does not produce the desired output Feb 8, 2018
@joe1gi joe1gi changed the title to_csv() with a single level (row) MultiIndex does not produce the desired output to_csv() with a single level row MultiIndex does not produce the desired output Feb 8, 2018
@jreback
Copy link
Contributor

jreback commented Feb 10, 2018

cc @gfyoung

@gfyoung gfyoung added API Design IO CSV read_csv, to_csv Regression Functionality that used to work in a prior pandas version labels Feb 10, 2018
@gfyoung
Copy link
Member

gfyoung commented Feb 10, 2018

@joe1gi : Thanks for the report! I agree: the outputs should match. PR to patch is welcome!

@jreback jreback added this to the Next Major Release milestone Feb 11, 2018
@jreback jreback added Output-Formatting __repr__ of pandas objects, to_string Difficulty Intermediate labels Feb 11, 2018
@jreback jreback modified the milestones: Next Major Release, 0.23.0 Feb 22, 2018
@jreback jreback modified the milestones: 0.23.0, Next Major Release Apr 14, 2018
@jreback jreback modified the milestones: Contributions Welcome, 0.24.0 Nov 1, 2018
jreback pushed a commit that referenced this issue Nov 1, 2018
Display the level by itself instead of as a tuple
(i.e. "squash" the MultiIndex into a single level)

Closes gh-19589.
tm9k1 pushed a commit to tm9k1/pandas that referenced this issue Nov 19, 2018
Display the level by itself instead of as a tuple
(i.e. "squash" the MultiIndex into a single level)

Closes pandas-devgh-19589.
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this issue Feb 28, 2019
Display the level by itself instead of as a tuple
(i.e. "squash" the MultiIndex into a single level)

Closes pandas-devgh-19589.
Pingviinituutti pushed a commit to Pingviinituutti/pandas that referenced this issue Feb 28, 2019
Display the level by itself instead of as a tuple
(i.e. "squash" the MultiIndex into a single level)

Closes pandas-devgh-19589.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design IO CSV read_csv, to_csv Output-Formatting __repr__ of pandas objects, to_string Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants