Skip to content

Commit 9c9f4cb

Browse files
committed
ENH: index_label=False -> no leading comma in header in DataFrame.to_csv. close #1583
1 parent cfa70db commit 9c9f4cb

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

RELEASE.rst

+2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ pandas 0.9.0
5050
- DataFrame.dot will not do data alignment, and also work with Series (#1915)
5151
- Add ``na`` option for missing data handling in some vectorized string
5252
methods (#1689)
53+
- If index_label=False in DataFrame.to_csv, do not print fields/commas in the
54+
text output. Results in easier importing into R (#1583)
5355

5456
**API Changes**
5557

pandas/core/frame.py

+26-20
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,8 @@ def to_panel(self):
10411041
to_wide = deprecate('to_wide', to_panel)
10421042

10431043
def _helper_csvexcel(self, writer, na_rep=None, cols=None,
1044-
header=True, index=True, index_label=None,
1045-
float_format=None):
1044+
header=True, index=True,
1045+
index_label=None, float_format=None):
10461046
if cols is None:
10471047
cols = self.columns
10481048

@@ -1054,24 +1054,28 @@ def _helper_csvexcel(self, writer, na_rep=None, cols=None,
10541054
if has_aliases or header:
10551055
if index:
10561056
# should write something for index label
1057-
if index_label is None:
1058-
if isinstance(self.index, MultiIndex):
1059-
index_label = []
1060-
for i, name in enumerate(self.index.names):
1061-
if name is None:
1062-
name = ''
1063-
index_label.append(name)
1064-
else:
1065-
index_label = self.index.name
1066-
if index_label is None:
1067-
index_label = ['']
1057+
if index_label is not False:
1058+
if index_label is None:
1059+
if isinstance(self.index, MultiIndex):
1060+
index_label = []
1061+
for i, name in enumerate(self.index.names):
1062+
if name is None:
1063+
name = ''
1064+
index_label.append(name)
10681065
else:
1069-
index_label = [index_label]
1070-
elif not isinstance(index_label, (list, tuple, np.ndarray)):
1071-
# given a string for a DF with Index
1072-
index_label = [index_label]
1066+
index_label = self.index.name
1067+
if index_label is None:
1068+
index_label = ['']
1069+
else:
1070+
index_label = [index_label]
1071+
elif not isinstance(index_label, (list, tuple, np.ndarray)):
1072+
# given a string for a DF with Index
1073+
index_label = [index_label]
1074+
1075+
encoded_labels = list(index_label)
1076+
else:
1077+
encoded_labels = []
10731078

1074-
encoded_labels = list(index_label)
10751079
if has_aliases:
10761080
if len(header) != len(cols):
10771081
raise ValueError(('Writing %d cols but got %d aliases'
@@ -1127,10 +1131,12 @@ def to_csv(self, path_or_buf, sep=",", na_rep='', float_format=None,
11271131
assumed to be aliases for the column names
11281132
index : boolean, default True
11291133
Write row names (index)
1130-
index_label : string or sequence, default None
1134+
index_label : string or sequence, or False, default None
11311135
Column label for index column(s) if desired. If None is given, and
11321136
`header` and `index` are True, then the index names are used. A
1133-
sequence should be given if the DataFrame uses MultiIndex.
1137+
sequence should be given if the DataFrame uses MultiIndex. If
1138+
False do not print fields for index names. Use index_label=False
1139+
for easier importing in R
11341140
mode : Python write mode, default 'w'
11351141
sep : character, default ","
11361142
Field delimiter for the output file.

pandas/tests/test_frame.py

+11
Original file line numberDiff line numberDiff line change
@@ -3392,6 +3392,17 @@ def test_to_csv_quoting(self):
33923392

33933393
self.assertEqual(result, expected)
33943394

3395+
def test_to_csv_index_no_leading_comma(self):
3396+
df = DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]},
3397+
index=['one', 'two', 'three'])
3398+
3399+
buf = StringIO()
3400+
df.to_csv(buf, index_label=False)
3401+
expected = ('A,B\n'
3402+
'one,1,4\n'
3403+
'two,2,5\n'
3404+
'three,3,6\n')
3405+
self.assertEqual(buf.getvalue(), expected)
33953406

33963407
def test_to_excel_from_excel(self):
33973408
try:

0 commit comments

Comments
 (0)