Skip to content

Commit 425c844

Browse files
jseaboldwesm
authored andcommitted
ENH: Allow Series.to_csv to ignore the index.
1 parent daf0c67 commit 425c844

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

pandas/core/series.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,14 @@ def __str__(self):
601601
def __iter__(self):
602602
return iter(self.values)
603603

604-
def iteritems(self):
604+
def iteritems(self, index=True):
605605
"""
606606
Lazily iterate over (index, value) tuples
607607
"""
608-
return izip(iter(self.index), iter(self))
608+
if index:
609+
return izip(iter(self.index), iter(self))
610+
else:
611+
return izip(iter(self))
609612

610613
iterkv = iteritems
611614
if py3compat.PY3: # pragma: no cover
@@ -1969,18 +1972,20 @@ def from_csv(cls, path, sep=',', parse_dates=True):
19691972
df = DataFrame.from_csv(path, header=None, sep=sep, parse_dates=parse_dates)
19701973
return df[df.columns[0]]
19711974

1972-
def to_csv(self, path):
1975+
def to_csv(self, path, index=True):
19731976
"""
19741977
Write the Series to a CSV file
19751978
19761979
Parameters
19771980
----------
19781981
path : string or None
19791982
Output filepath. If None, write to stdout
1983+
index : bool, optional
1984+
Include the index as row names or not
19801985
"""
19811986
f = open(path, 'w')
19821987
csvout = csv.writer(f, lineterminator='\n')
1983-
csvout.writerows(self.iteritems())
1988+
csvout.writerows(self.iteritems(index))
19841989
f.close()
19851990

19861991
def dropna(self):

0 commit comments

Comments
 (0)