Skip to content

Commit 282746a

Browse files
kawochenjreback
authored andcommitted
BUG: fix bug where date_format in to_csv is sometimes ignored #10209
1 parent e26b33c commit 282746a

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

doc/source/whatsnew/v0.16.2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Bug Fixes
5959
- Bug in ``Categorical`` repr with ``display.width`` of ``None`` in Python 3 (:issue:`10087`)
6060
- Bug where some of the nan funcs do not have consistent return dtypes (:issue:`10251`)
6161
- Bug in groupby.apply aggregation for Categorical not preserving categories (:issue:`10138`)
62+
- Bug in ``to_csv`` where ``date_format`` is ignored if the ``datetime`` is fractional (:issue:`10209`)
6263

6364
- Bug in cache updating when consolidating (:issue:`10264`)
6465

@@ -92,4 +93,3 @@ Bug Fixes
9293

9394
- Bug to handle masking empty ``DataFrame``(:issue:`10126`)
9495
- Bug where MySQL interface could not handle numeric table/column names (:issue:`10255`)
95-

pandas/core/format.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2114,7 +2114,7 @@ def _get_format_datetime64_from_values(values, date_format):
21142114
is_dates_only = _is_dates_only(values)
21152115
if is_dates_only:
21162116
return date_format or "%Y-%m-%d"
2117-
return None
2117+
return date_format
21182118

21192119

21202120
class Timedelta64Formatter(GenericArrayFormatter):

pandas/tests/test_format.py

+21
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,27 @@ def test_to_csv_decimal(self):
24492449
expected_float_format = ';col1;col2;col3\n0;1;a;10,10\n'
24502450
self.assertEqual(df.to_csv(decimal=',',sep=';', float_format = '%.2f'), expected_float_format)
24512451

2452+
def test_to_csv_date_format(self):
2453+
# GH 10209
2454+
df_sec = DataFrame({'A': pd.date_range('20130101',periods=5,freq='s')})
2455+
df_day = DataFrame({'A': pd.date_range('20130101',periods=5,freq='d')})
2456+
2457+
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' + \
2458+
'\n3,2013-01-01 00:00:03\n4,2013-01-01 00:00:04\n'
2459+
self.assertEqual(df_sec.to_csv(), expected_default_sec)
2460+
2461+
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' + \
2462+
'\n3,2013-01-04 00:00:00\n4,2013-01-05 00:00:00\n'
2463+
self.assertEqual(df_day.to_csv(date_format='%Y-%m-%d %H:%M:%S'), expected_ymdhms_day)
2464+
2465+
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'
2466+
self.assertEqual(df_sec.to_csv(date_format='%Y-%m-%d'), expected_ymd_sec)
2467+
2468+
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'
2469+
self.assertEqual(df_day.to_csv(), expected_default_day)
2470+
self.assertEqual(df_day.to_csv(date_format='%Y-%m-%d'), expected_default_day)
2471+
2472+
24522473
class TestSeriesFormatting(tm.TestCase):
24532474
_multiprocess_can_split_ = True
24542475

0 commit comments

Comments
 (0)