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

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

Comments

Projects
None yet
2 participants

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

Contributor

y-p 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 commented Apr 25, 2013

Thanks!

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

This also works for me at the moment.

Contributor

y-p commented Apr 25, 2013

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

Contributor

y-p commented Apr 25, 2013

fixed in master via #3458.

y-p closed this Apr 25, 2013

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