Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,7 @@ Bug Fixes

- Bug where infer_freq infers timerule (WOM-5XXX) unsupported by to_offset (:issue:`9425`)


- Bug in ``to_csv`` where ``data_format`` is ignored if the ``datetime`` is not
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data_format -> date_format

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your eagle eye!

an integral date value (i.e. it is fractional) (:issue:`10209`)

2 changes: 1 addition & 1 deletion pandas/core/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2114,7 +2114,7 @@ def _get_format_datetime64_from_values(values, date_format):
is_dates_only = _is_dates_only(values)
if is_dates_only:
return date_format or "%Y-%m-%d"
return None
return date_format


class Timedelta64Formatter(GenericArrayFormatter):
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,27 @@ def test_to_csv_decimal(self):
expected_float_format = ';col1;col2;col3\n0;1;a;10,10\n'
self.assertEqual(df.to_csv(decimal=',',sep=';', float_format = '%.2f'), expected_float_format)

def test_to_csv_date_format(self):
# GH 10209
df_sec = DataFrame({'A': pd.date_range('20130101',periods=5,freq='s')})
df_day = DataFrame({'A': pd.date_range('20130101',periods=5,freq='d')})

expected_default_sec = ',A\n0,2013-01-01 00:00:00\n1,2013-01-01 00:00:01\n2,2013-01-01 00:00:02' + \
'\n3,2013-01-01 00:00:03\n4,2013-01-01 00:00:04\n'
self.assertEqual(df_sec.to_csv(), expected_default_sec)

expected_ymdhms_day = ',A\n0,2013-01-01 00:00:00\n1,2013-01-02 00:00:00\n2,2013-01-03 00:00:00' + \
'\n3,2013-01-04 00:00:00\n4,2013-01-05 00:00:00\n'
self.assertEqual(df_day.to_csv(date_format='%Y-%m-%d %H:%M:%S'), expected_ymdhms_day)

expected_ymd_sec = ',A\n0,2013-01-01\n1,2013-01-01\n2,2013-01-01\n3,2013-01-01\n4,2013-01-01\n'
self.assertEqual(df_sec.to_csv(date_format='%Y-%m-%d'), expected_ymd_sec)

expected_default_day = ',A\n0,2013-01-01\n1,2013-01-02\n2,2013-01-03\n3,2013-01-04\n4,2013-01-05\n'
self.assertEqual(df_day.to_csv(), expected_default_day)
self.assertEqual(df_day.to_csv(date_format='%Y-%m-%d'), expected_default_day)


class TestSeriesFormatting(tm.TestCase):
_multiprocess_can_split_ = True

Expand Down