Skip to content

Commit 7388990

Browse files
change index label default to True and remove None option
1 parent 0399c34 commit 7388990

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

pandas/core/generic.py

+6-9
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=None,
2874+
columns=None, header=True, index=True, index_label=True,
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,14 +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 : False or str or sequence, default None
2914-
Column label for index column(s) if desired. If None is given, and
2915-
`header` and `index` are True, then the index names are used. If
2916-
`header` is False or None, default index_label changes to False,
2917-
neither header nor index names will be printed. A
2918-
sequence should be given if the object uses MultiIndex. If
2919-
False do not print fields for index names. Use index_label=False
2920-
for easier importing in R.
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
2917+
for index names. Use index_label=False for easier importing in R.
29212918
mode : str
29222919
Python write mode, default 'w'.
29232920
encoding : str, optional

pandas/io/formats/csvs.py

+8-5
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=None, mode='w', nanRep=None, encoding=None,
32+
index_label=True, 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,
@@ -51,11 +51,14 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='',
5151
self.header = header
5252
self.index = index
5353
self.index_label = index_label
54-
# set defualt index label to False if header is False and
55-
# index_label is not None or False.
54+
# set defualt index label to False if header is False and if
55+
# index_label is boolean.
5656
if self.header is None or self.header is False:
57-
if self.index_label is None:
57+
if isinstance(self.index_label, bool):
5858
self.index_label = False
59+
# set index label to False if index is False.
60+
if not self.index:
61+
self.index_label = False
5962
self.mode = mode
6063
if encoding is None:
6164
encoding = 'ascii' if compat.PY2 else 'utf-8'
@@ -202,7 +205,7 @@ def _index_label_encoder(self):
202205
"""
203206
index_label = self.index_label
204207
obj = self.obj
205-
if index_label is None:
208+
if index_label:
206209
# append index label based on index type
207210
if isinstance(obj.index, ABCMultiIndex):
208211
index_label = []

pandas/tests/generic/test_frame.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ 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, None, '0,0,0\n1,0,0\n'),
278-
(True, None, 'index.name,0,1\n0,0,0\n1,0,0\n'),
277+
(False, True, '0,0,0\n1,0,0\n'),
278+
(True, True, 'index.name,0,1\n0,0,0\n1,0,0\n'),
279279
(False, False, '0,0,0\n1,0,0\n'),
280280
(True, False, ',0,1\n0,0,0\n1,0,0\n')
281281
])
@@ -290,8 +290,8 @@ def test_to_csv_header_single_index(self, header, index_label, expected):
290290
@pytest.mark.skipif(os.name == 'nt',
291291
reason="Windows use \r\n for newline")
292292
@pytest.mark.parametrize("header, index_label, expected", [
293-
(False, None, 'a,b,0,0\na,c,0,0\n'),
294-
(True, None, 'index.name.0,index.name.1,0,1\na,b,0,0\na,c,0,0\n'),
293+
(False, True, 'a,b,0,0\na,c,0,0\n'),
294+
(True, True, 'index.name.0,index.name.1,0,1\na,b,0,0\na,c,0,0\n'),
295295
(False, False, 'a,b,0,0\na,c,0,0\n'),
296296
(True, False, ',,0,1\na,b,0,0\na,c,0,0\n')
297297
])

0 commit comments

Comments
 (0)