Skip to content

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

Closed
@markrichardson

Description

@markrichardson

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions