Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Contributor

Choose a reason for hiding this comment

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

use backticks around date_format

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
11 changes: 11 additions & 0 deletions pandas/tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,17 @@ 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 = DataFrame({'A' : pd.date_range('20130101',periods=5,freq='s')})
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a test with freq='D' as well (should be same results, so can do in a loop)


expected_default = ',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.to_csv(), expected_default)

expected_ymd = ',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.to_csv(date_format='%Y-%m-%d'), expected_ymd)

class TestSeriesFormatting(tm.TestCase):
_multiprocess_can_split_ = True

Expand Down