Skip to content

Commit 6eafa07

Browse files
committed
ENH: add new izip-based row-iterator, update release and docs (close #818)
1 parent 0f98ad8 commit 6eafa07

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

RELEASE.rst

+5
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ pandas 0.7.1
3131

3232
- Add ``to_clipboard`` function to pandas namespace for writing objects to
3333
the system clipboard (#774)
34+
- Add ``itertuples`` method to Dataframe for iterating through the rows of a
35+
dataframe as tuples (#818)
3436

3537
**Bug fixes**
3638

3739
- Fix memory leak when inserting large number of columns into a single
3840
DataFrame (#790)
3941
- Appending length-0 DataFrame with new columns would not result in those new
4042
columns being part of the resulting concatenated DataFrame (#782)
43+
- Fixed groupby corner case when passing dictionary grouper and as_index is
44+
False (#819)
45+
- Fixed bug whereby bool array sometimes had object dtype (#820)
4146

4247
pandas 0.7.0
4348
============

doc/source/basics.rst

+13
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,19 @@ For instance, a contrived way to transpose the dataframe would be:
759759
df2_t = DataFrame(dict((idx,values) for idx, values in df2.iterrows()))
760760
print df2_t
761761
762+
itertuples
763+
~~~~~~~~~~
764+
765+
This method will return an iterator yielding a tuple for each row in the
766+
DataFrame. The first element of the tuple will be the row's corresponding index
767+
value, while the remaining values are the row values proper.
768+
769+
For instance,
770+
771+
.. ipython:: python
772+
773+
for r in df2.itertuples(): print r
774+
762775
.. _basics.sorting:
763776

764777
Sorting by index and value

pandas/core/frame.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import numpy.ma as ma
2626

2727
from pandas.core.common import (isnull, notnull, PandasError, _try_sort,
28-
_default_index, _stringify, csv_encode)
28+
_default_index, _stringify)
2929
from pandas.core.daterange import DateRange
3030
from pandas.core.generic import NDFrame
3131
from pandas.core.index import Index, MultiIndex, NULL_INDEX, _ensure_index
@@ -497,6 +497,15 @@ def iterrows(self):
497497
s.name = k
498498
yield k, s
499499

500+
def itertuples(self):
501+
"""
502+
Iterate over rows of DataFrame as tuples, with index value
503+
as first element of the tuple
504+
"""
505+
series = [self[col] for col in self.columns]
506+
series.insert(0, self.index)
507+
return izip(*series)
508+
500509
iterkv = iteritems
501510
if py3compat.PY3: # pragma: no cover
502511
items = iteritems

pandas/tests/test_frame.py

+7
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,13 @@ def test_iterrows(self):
19281928
exp = self.mixed_frame.xs(self.mixed_frame.index[i])
19291929
assert_series_equal(v, exp)
19301930

1931+
def test_itertuples(self):
1932+
for i, tup in enumerate(self.frame.itertuples()):
1933+
s = Series(tup[1:])
1934+
s.name = tup[0]
1935+
expected = self.frame.ix[i,:].reset_index(drop=True)
1936+
assert_series_equal(s, expected)
1937+
19311938
def test_len(self):
19321939
self.assertEqual(len(self.frame), len(self.frame.index))
19331940

0 commit comments

Comments
 (0)