Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Allow Series.to_csv to ignore the index. #684

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1969,18 +1972,20 @@ 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

Parameters
----------
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):
Expand Down