Skip to content

to_csv date format flag not behaving as expected with multiindex #6797

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
markrichardson opened this issue Apr 4, 2014 · 2 comments
Closed
Labels
Bug Enhancement MultiIndex Output-Formatting __repr__ of pandas objects, to_string
Milestone

Comments

@markrichardson
Copy link

Using pandas 0.13.1 ...

When df is a 2-level datetime indexed dataframe, to_csv prints 00:00 following the dates, even when you specify date_format = '%Y-%m-%d'.

df.to_csv(date_format = '%Y-%m-%d')

To get around this, I'm currently having to use reset_index; then to_csv works as expected.

In [4]: df = DataFrame(np.random.randn(6,2),index=MultiIndex.from_product([['A','B'],date_range('20130101',periods=3)]))

In [5]: df
Out[5]: 
                     0         1
A 2013-01-01 -0.180284 -0.632280
  2013-01-02  2.986923 -0.422540
  2013-01-03 -1.435938  1.700168
B 2013-01-01 -0.163560 -1.047000
  2013-01-02  1.852092 -0.552963
  2013-01-03  1.375277  0.737837

[6 rows x 2 columns]

In [6]: df.to_csv('test.csv',mode='w')

In [7]: !cat test.csv
,,0,1
A,2013-01-01 00:00:00,-0.1802842406155066,-0.6322803999738553
A,2013-01-02 00:00:00,2.9869225314020604,-0.42253974812704775
A,2013-01-03 00:00:00,-1.435937967237287,1.700167741194332
B,2013-01-01 00:00:00,-0.1635604685556706,-1.0469998404335645
B,2013-01-02 00:00:00,1.8520922142464138,-0.5529633735794578
B,2013-01-03 00:00:00,1.3752768989190265,0.7378371099828236

In [8]: df.to_csv('test.csv',mode='w',date_format='%Y-%m-%d')

In [9]: !cat test.csv
,,0,1
A,2013-01-01 00:00:00,-0.1802842406155066,-0.6322803999738553
A,2013-01-02 00:00:00,2.9869225314020604,-0.42253974812704775
A,2013-01-03 00:00:00,-1.435937967237287,1.700167741194332
B,2013-01-01 00:00:00,-0.1635604685556706,-1.0469998404335645
B,2013-01-02 00:00:00,1.8520922142464138,-0.5529633735794578
B,2013-01-03 00:00:00,1.3752768989190265,0.7378371099828236

In [10]: df.reset_index().to_csv('test.csv',mode='w',date_format='%Y-%m-%d')

In [11]: !cat test.csv
,level_0,level_1,0,1
0,A,2013-01-01,-0.1802842406155066,-0.6322803999738553
1,A,2013-01-02,2.9869225314020604,-0.42253974812704775
2,A,2013-01-03,-1.435937967237287,1.700167741194332
3,B,2013-01-01,-0.1635604685556706,-1.0469998404335645
4,B,2013-01-02,1.8520922142464138,-0.5529633735794578
5,B,2013-01-03,1.3752768989190265,0.7378371099828236

@jreback jreback added this to the 0.15.0 milestone Apr 4, 2014
@jreback
Copy link
Contributor

jreback commented Apr 4, 2014

needs a little custom logic to individually format the data in each level of the multi-index, in core/index.py/MultiIndex/to_native_types()

@jreback jreback modified the milestones: 0.16.0, Next Major Release Mar 3, 2015
@jreback
Copy link
Contributor

jreback commented Nov 8, 2015

dupe of #7791

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

No branches or pull requests

2 participants