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

[BUG] to_csv() cols parameter not working properly. #3454

Closed
abudis opened this issue Apr 25, 2013 · 4 comments
Closed

[BUG] to_csv() cols parameter not working properly. #3454

abudis opened this issue Apr 25, 2013 · 4 comments
Labels
Bug IO Data IO issues that don't fit into a more specific label Regression Functionality that used to work in a prior pandas version
Milestone

Comments

@abudis
Copy link

abudis commented Apr 25, 2013

pandas version: 0.11
python version: 2.7.4
platforms: linux & windows

When using cols parameter of to_csv() method only the order of the header is changed, while actual values are not.

import pandas as pd
from cStringIO import StringIO

data = """\
date,time,X1,X2
2013-1-1,0030,0.1,0.4
2013-1-1,0100,0.2,0.3
2013-1-1,0130,0.3,0.2
2013-1-1,0200,0.4,0.1
"""

df = pd.read_csv(StringIO(data), parse_dates=[['date', 'time']], index_col=0)
df.to_csv('data.csv', index_label='date_time', cols=['X2', 'X1'])

Expected output:
date_time,X2,X1
2013-01-01 00:30:00,0.4,0.1
2013-01-01 01:00:00,0.3,0.2
2013-01-01 01:30:00,0.2,0.3
2013-01-01 02:00:00,0.1,0.4

Actual output:
date_time,X2,X1
2013-01-01 00:30:00,0.1,0.4
2013-01-01 01:00:00,0.2,0.3
2013-01-01 01:30:00,0.3,0.2
2013-01-01 02:00:00,0.4,0.1

df.to_csv('data.csv', index_label='date_time', cols=['X2'])

date_time,X2
2013-01-01 00:30:00,0.1
2013-01-01 01:00:00,0.2
2013-01-01 01:30:00,0.3
2013-01-01 02:00:00,0.4

print df['X1']

date_time
2013-01-01 00:30:00 0.1
2013-01-01 01:00:00 0.2
2013-01-01 01:30:00 0.3
2013-01-01 02:00:00 0.4
Name: X1, dtype: float64

print df['X2']

date_time
2013-01-01 00:30:00 0.4
2013-01-01 01:00:00 0.3
2013-01-01 01:30:00 0.2
2013-01-01 02:00:00 0.1
Name: X2, dtype: float64

@ghost
Copy link

ghost commented Apr 25, 2013

Thanks for reporting this, it should be fixed soon.
In the meantime, you can use df.to_csv(engine='python') to recover the
original behavior from 0.10.1.

@abudis
Copy link
Author

abudis commented Apr 25, 2013

Thanks!

df[['date', 'time', 'X2', 'X1']].to_csv('data.csv', index=False)

This also works for me at the moment.

@ghost
Copy link

ghost commented Apr 25, 2013

yep, that would work as well, and uses the new faster code path.

@ghost
Copy link

ghost commented Apr 25, 2013

fixed in master via #3458.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Data IO issues that don't fit into a more specific label Regression Functionality that used to work in a prior pandas version
Projects
None yet
Development

No branches or pull requests

1 participant