Skip to content

BUG: DataFrame.to_excel(index=False) misplaces data/columns in MultiIndex case #5616

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
aldanor opened this issue Nov 29, 2013 · 8 comments · Fixed by #5659
Closed

BUG: DataFrame.to_excel(index=False) misplaces data/columns in MultiIndex case #5616

aldanor opened this issue Nov 29, 2013 · 8 comments · Fixed by #5659
Labels
Bug IO Excel read_excel, to_excel Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@aldanor
Copy link
Contributor

aldanor commented Nov 29, 2013

Example:

>>> df = pd.DataFrame({'a': [10, 20], 'b': [30, 40], 'c': [50, 60]}, 
...                   index=pd.MultiIndex.from_tuples([(70, 80), (90, 100)]))
>>> writer = pd.ExcelWriter('test.xls')
>>> df.to_excel(writer, sheet_name='1', index=False)
>>> writer.save()
>>> pd.read_excel('test.xls', '1')
        a   b   c
10 30  50 NaN NaN
20 40  60 NaN NaN

The column names are obviously misplaced (erroneously shifted to the right by the number of levels in the MultiIndex). Or is it the data that is misplaced? Wonder why no one noticed this before.

@jtratner
Copy link
Contributor

Thanks for the bug report. Can you post what version of pandas and xlwt you
are using?
On Nov 29, 2013 11:50 AM, "Ivan Smirnov" [email protected] wrote:

Example:

df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]}, ... index=pd.MultiIndex.from_tuples([(7, 8), (9, 10)]))>>> writer = pd.ExcelWriter('test.xls')>>> df.to_excel(writer, sheet_name='1', index=False)>>> writer.save()>>> pd.read_excel('test.xls', '1')
a b c1 3 5 NaN NaN2 4 6 NaN NaN

The column names are obviously misplaced (erroneously shifted to the right
by the number of levels in the MultiIndex). Or is it the data that is
misplaced?


Reply to this email directly or view it on GitHubhttps://github.com//issues/5616
.

@aldanor
Copy link
Contributor Author

aldanor commented Nov 29, 2013

@jtratner it's xlwt==0.7.5 and pandas is fetched from git head about a week ago (btw it registers as pandas==0.0.0 on my machine since it's behind a corporate proxy/firewall and git fails to retrieve a proper version... oh well, guess that's another bug).

@jtratner
Copy link
Contributor

Can you run git describe in the pandas directory and post what you see?
Doesn't really make sense to me that it comes out as 0.0.0 - proxy
shouldn't have anything to do with it, but not a big deal.

Regardless, definitely a bug that needs to be fixed, but I'm guessing it
won't be that difficult.

@aldanor
Copy link
Contributor Author

aldanor commented Dec 2, 2013

@jtratner Well, that's the thing - I cannot access git due to SSL restrictions, proxies etc on a remote box at work, hence I had to download a zip of the master branch. Obviously then, git describe returns a fatal error since it's not a git repo in the first place, and pandas ended up installing itself to /site-packages/pandas-0.0.0-py2.7-linux-x86_64.egg/pandas. Bottom line is, is pandas (dev) versioning supposed to be so tightly coupled to git?

@jtratner
Copy link
Contributor

jtratner commented Dec 2, 2013

it's supposed to fall back on using the hard-coded major/minor version.
Anyways, not a big deal, going to take a look when I can.

On Mon, Dec 2, 2013 at 12:09 PM, Ivan Smirnov [email protected]:

@jtratner https://github.com/jtratner Well, that's the thing - I cannot
access git due to SSL restrictions, proxies etc on a remote box at work,
hence I had to download a zip of the master branch. Obviously then, git
describe returns a fatal error since it's not a git repo in the first
place, and pandas ended up installing itself to
/site-packages/pandas-0.0.0-py2.7-linux-x86_64.egg/pandas. Bottom line
is, is pandas versioning supposed to be so tightly coupled to git?


Reply to this email directly or view it on GitHubhttps://github.com//issues/5616#issuecomment-29636624
.

@jmcnamara
Copy link
Contributor

Adding a comment to track this issue.

@jmcnamara
Copy link
Contributor

Mea culpa. My Excel multi-index changes introduced this bug.

I've pushed a fix and test.

@jmcnamara
Copy link
Contributor

Output after fix:

>>> import pandas as pd
>>>
>>> df = pd.DataFrame({'a': [10, 20], 'b': [30, 40], 'c': [50, 60]},
...                   index=pd.MultiIndex.from_tuples([(70, 80), (90, 100)]))
>>> writer = pd.ExcelWriter('test.xls')
>>> df.to_excel(writer, sheet_name='1', index=False)
>>> writer.save()
>>>
>>> pd.read_excel('test.xls', '1')
    a   b   c
0  10  30  50
1  20  40  60

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants