From 2c5fac75685a1446dc6000e34ae9f65e4b4767b1 Mon Sep 17 00:00:00 2001 From: Nicolas Bonnotte Date: Sun, 8 Nov 2015 17:10:46 +0100 Subject: [PATCH] BUG: multi-index to_native_types is not passing thru parameters --- doc/source/whatsnew/v0.17.1.txt | 3 +++ pandas/core/index.py | 7 ++++++- pandas/tests/test_format.py | 11 +++++++++++ pandas/tests/test_frame.py | 7 +++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v0.17.1.txt b/doc/source/whatsnew/v0.17.1.txt index 0d0f4c66c1fec..48eca9fd6f96d 100755 --- a/doc/source/whatsnew/v0.17.1.txt +++ b/doc/source/whatsnew/v0.17.1.txt @@ -120,3 +120,6 @@ Bug Fixes - Bug in ``to_excel`` with openpyxl 2.2+ and merging (:issue:`11408`) - Bug in ``DataFrame.to_dict()`` produces a ``np.datetime64`` object instead of ``Timestamp`` when only datetime is present in data (:issue:`11327`) + + +- Bug in ``DataFrame.to_csv`` in passing through arguments for formatting ``MultiIndexes``, including ``date_format`` (:issue:`7791`) diff --git a/pandas/core/index.py b/pandas/core/index.py index 855e3f013bfd3..edca7893c12d8 100644 --- a/pandas/core/index.py +++ b/pandas/core/index.py @@ -4327,7 +4327,12 @@ def _reference_duplicate_name(self, name): return sum(name == n for n in self.names) > 1 def _format_native_types(self, **kwargs): - return self.values + # we go through the levels and format them + levels = [level._format_native_types(**kwargs) + for level in self.levels] + mi = MultiIndex(levels=levels, labels=self.labels, names=self.names, + sortorder=self.sortorder, verify_integrity=False) + return mi.values @property def _constructor(self): diff --git a/pandas/tests/test_format.py b/pandas/tests/test_format.py index 140b54225b8e8..22555a84c55de 100644 --- a/pandas/tests/test_format.py +++ b/pandas/tests/test_format.py @@ -2952,6 +2952,17 @@ def test_to_csv_date_format(self): self.assertEqual(df_day.to_csv(), expected_default_day) self.assertEqual(df_day.to_csv(date_format='%Y-%m-%d'), expected_default_day) + # testing if date_format parameter is taken into account for + # multi-indexed dataframes (GH 7791) + df_sec['B'] = 0 + df_sec['C'] = 1 + expected_ymd_sec = 'A,B,C\n2013-01-01,0,1\n' + df_sec_grouped = df_sec.groupby([pd.Grouper(key='A', freq='1h'), 'B']) + self.assertEqual( + df_sec_grouped.mean().to_csv(date_format='%Y-%m-%d'), + expected_ymd_sec + ) + # deprecation GH11274 def test_to_csv_engine_kw_deprecation(self): with tm.assert_produces_warning(FutureWarning): diff --git a/pandas/tests/test_frame.py b/pandas/tests/test_frame.py index 1b57d53a548f3..8a01b1ca17373 100644 --- a/pandas/tests/test_frame.py +++ b/pandas/tests/test_frame.py @@ -7341,6 +7341,13 @@ def test_to_csv_quoting(self): df.to_csv(buf, encoding='utf-8', index=False) self.assertEqual(buf.getvalue(), text) + # testing if quoting parameter is passed through with multi-indexes + # related to issue #7791 + df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6]}) + df = df.set_index(['a', 'b']) + expected = '"a","b","c"\n"1","3","5"\n"2","4","6"\n' + self.assertEqual(df.to_csv(quoting=csv.QUOTE_ALL), expected) + def test_to_csv_unicodewriter_quoting(self): df = DataFrame({'A': [1, 2, 3], 'B': ['foo', 'bar', 'baz']})