Skip to content

Commit 2bbcf34

Browse files
refactoring and adding docstring and test cases
1 parent 7388990 commit 2bbcf34

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

pandas/core/generic.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -2871,7 +2871,7 @@ def to_latex(self, buf=None, columns=None, col_space=None, header=True,
28712871
return formatter.buf.getvalue()
28722872

28732873
def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
2874-
columns=None, header=True, index=True, index_label=True,
2874+
columns=None, header=True, index=True, index_label=None,
28752875
mode='w', encoding=None, compression='infer', quoting=None,
28762876
quotechar='"', line_terminator=None, chunksize=None,
28772877
tupleize_cols=None, date_format=None, doublequote=True,
@@ -2910,10 +2910,11 @@ def to_csv(self, path_or_buf=None, sep=",", na_rep='', float_format=None,
29102910
29112911
index : bool, default True
29122912
Write row names (index).
2913-
index_label : bool or str or sequence, default True.
2914-
Column label for index column(s) if desired. If header or index
2915-
is False, index_label will be set to False. A sequence should be
2916-
given if the object uses MultiIndex. If False do not print fields
2913+
index_label : bool or str or sequence, default None.
2914+
If index_label is not explicitly called, False if either header
2915+
or index is set to False; otherwise, True. If index_label is
2916+
explicitly called by allowed types of input, then input will be
2917+
given to index_label. If False, do not print fields
29172918
for index names. Use index_label=False for easier importing in R.
29182919
mode : str
29192920
Python write mode, default 'w'.

pandas/io/formats/csvs.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class CSVFormatter(object):
2929

3030
def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
3131
float_format=None, cols=None, header=True, index=True,
32-
index_label=True, mode='w', nanRep=None, encoding=None,
32+
index_label=None, mode='w', nanRep=None, encoding=None,
3333
compression='infer', quoting=None, line_terminator='\n',
3434
chunksize=None, tupleize_cols=False, quotechar='"',
3535
date_format=None, doublequote=True, escapechar=None,
@@ -50,15 +50,17 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
5050

5151
self.header = header
5252
self.index = index
53-
self.index_label = index_label
54-
# set defualt index label to False if header is False and if
55-
# index_label is boolean.
56-
if self.header is None or self.header is False:
57-
if isinstance(self.index_label, bool):
53+
# if index label is not explicitly called, index label is True if header
54+
# or index is not False; otherwise, index label is set to False
55+
if index_label is None:
56+
if self.header is False or self.header is None or not self.index:
5857
self.index_label = False
59-
# set index label to False if index is False.
60-
if not self.index:
61-
self.index_label = False
58+
else:
59+
self.index_label = True
60+
else:
61+
# if index label is explicitly called, then use the caller.
62+
self.index_label = index_label
63+
6264
self.mode = mode
6365
if encoding is None:
6466
encoding = 'ascii' if compat.PY2 else 'utf-8'

pandas/tests/generic/test_frame.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,12 @@ def test_deepcopy_empty(self):
274274
@pytest.mark.skipif(os.name == 'nt',
275275
reason="Windows use \r\n for newline")
276276
@pytest.mark.parametrize("header, index_label, expected", [
277-
(False, True, '0,0,0\n1,0,0\n'),
277+
(False, True, 'index.name,,\n0,0,0\n1,0,0\n'),
278278
(True, True, 'index.name,0,1\n0,0,0\n1,0,0\n'),
279279
(False, False, '0,0,0\n1,0,0\n'),
280-
(True, False, ',0,1\n0,0,0\n1,0,0\n')
280+
(True, False, ',0,1\n0,0,0\n1,0,0\n'),
281+
(False, None, '0,0,0\n1,0,0\n'),
282+
(True, None, 'index.name,0,1\n0,0,0\n1,0,0\n')
281283
])
282284
def test_to_csv_header_single_index(self, header, index_label, expected):
283285
# issue 24546
@@ -290,10 +292,12 @@ def test_to_csv_header_single_index(self, header, index_label, expected):
290292
@pytest.mark.skipif(os.name == 'nt',
291293
reason="Windows use \r\n for newline")
292294
@pytest.mark.parametrize("header, index_label, expected", [
293-
(False, True, 'a,b,0,0\na,c,0,0\n'),
295+
(False, True, 'index.name.0,index.name.1,,\na,b,0,0\na,c,0,0\n'),
294296
(True, True, 'index.name.0,index.name.1,0,1\na,b,0,0\na,c,0,0\n'),
295297
(False, False, 'a,b,0,0\na,c,0,0\n'),
296-
(True, False, ',,0,1\na,b,0,0\na,c,0,0\n')
298+
(True, False, ',,0,1\na,b,0,0\na,c,0,0\n'),
299+
(False, None, 'a,b,0,0\na,c,0,0\n'),
300+
(True, None, 'index.name.0,index.name.1,0,1\na,b,0,0\na,c,0,0\n')
297301
])
298302
def test_to_csv_header_multi_index(self, header, index_label, expected):
299303
# issue 24546

0 commit comments

Comments
 (0)