Skip to content

Commit 820da18

Browse files
changed based on wil review
1 parent 92db032 commit 820da18

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

pandas/io/formats/csvs.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,24 @@ def save(self):
199199
_fh.close()
200200

201201
def _index_label_encoder(self):
202-
"""Encode index label if it is not False.
203-
204-
Returns:
205-
index_label: new index_label given index types
206-
encode_labels: list of index labels
202+
"""
203+
Encode index label if it is not False.
204+
205+
Returns
206+
-------
207+
index_label: list
208+
New index_label given index types
209+
encode_labels: list
210+
List of index labels
207211
"""
208212
index_label = self.index_label
209213
obj = self.obj
210-
if index_label:
214+
215+
if index_label is True:
211216
# append index label based on index type
212217
if isinstance(obj.index, ABCMultiIndex):
213218
index_label = []
214-
for i, name in enumerate(obj.index.names):
219+
for name in obj.index.names:
215220
# add empty string is name is None
216221
if name is None:
217222
name = ''
@@ -225,7 +230,6 @@ def _index_label_encoder(self):
225230
index_label = [index_label]
226231
elif not isinstance(index_label,
227232
(list, tuple, np.ndarray, ABCIndexClass)):
228-
# given a string for a DF with Index
229233
index_label = [index_label]
230234

231235
encoded_labels = list(index_label)

pandas/tests/io/formats/test_to_csv.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,9 @@ def test_to_csv_compression(self, compression_only,
568568
(False, False, ['0,0,0', '1,0,0']),
569569
(True, False, [',0,1', '0,0,0', '1,0,0']),
570570
(False, None, ['0,0,0', '1,0,0']),
571-
(True, None, ['index.name,0,1', '0,0,0', '1,0,0'])
571+
(True, None, ['index.name,0,1', '0,0,0', '1,0,0']),
572+
(True, "new_index", ['new_index,0,1', '0,0,0', '1,0,0']),
573+
(True, ["new_index"], ['new_index,0,1', '0,0,0', '1,0,0'])
572574
])
573575
def test_to_csv_header_single_index(self, header, index_label,
574576
expected_rows):
@@ -587,7 +589,11 @@ def test_to_csv_header_single_index(self, header, index_label,
587589
(False, False, ['a,b,0,0', 'a,c,0,0']),
588590
(True, False, [',,0,1', 'a,b,0,0', 'a,c,0,0']),
589591
(False, None, ['a,b,0,0', 'a,c,0,0']),
590-
(True, None, ['index.name.0,index.name.1,0,1', 'a,b,0,0', 'a,c,0,0'])
592+
(True, None, ['index.name.0,index.name.1,0,1', 'a,b,0,0', 'a,c,0,0']),
593+
(True, ("index1", "index2"),
594+
['index1,index2,0,1', 'a,b,0,0', 'a,c,0,0']),
595+
(True, ["index1", "index2"],
596+
['index1,index2,0,1', 'a,b,0,0', 'a,c,0,0'])
591597
])
592598
def test_to_csv_header_multi_index(self, header, index_label,
593599
expected_rows):

0 commit comments

Comments
 (0)