From 6e4b093e44cc7dd4434e50b4cca8fa3d03c4fcf4 Mon Sep 17 00:00:00 2001 From: Skipper Seabold Date: Wed, 25 Jan 2012 12:11:39 -0500 Subject: [PATCH] ENH: Allow Series.to_csv to ignore the index. --- pandas/core/series.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 68464c9bdbad0..30702f6897188 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -601,11 +601,14 @@ def __str__(self): def __iter__(self): return iter(self.values) - def iteritems(self): + def iteritems(self, index=True): """ Lazily iterate over (index, value) tuples """ - return izip(iter(self.index), iter(self)) + if index: + return izip(iter(self.index), iter(self)) + else: + return izip(iter(self)) iterkv = iteritems if py3compat.PY3: # pragma: no cover @@ -1969,7 +1972,7 @@ def from_csv(cls, path, sep=',', parse_dates=True): df = DataFrame.from_csv(path, header=None, sep=sep, parse_dates=parse_dates) return df[df.columns[0]] - def to_csv(self, path): + def to_csv(self, path, index=True): """ Write the Series to a CSV file @@ -1977,10 +1980,12 @@ def to_csv(self, path): ---------- path : string or None Output filepath. If None, write to stdout + index : bool, optional + Include the index as row names or not """ f = open(path, 'w') csvout = csv.writer(f, lineterminator='\n') - csvout.writerows(self.iteritems()) + csvout.writerows(self.iteritems(index)) f.close() def dropna(self):