Skip to content

Commit 721e503

Browse files
committed
ENH: Implemented lazy iteration.
Fixes GH20783.
1 parent 0ae7e90 commit 721e503

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

pandas/core/base.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,12 @@ def __iter__(self):
902902
(for str, int, float) or a pandas scalar
903903
(for Timestamp/Timedelta/Interval/Period)
904904
"""
905-
return iter(self.tolist())
905+
if is_datetimelike(self._values):
906+
return map(com._maybe_box_datetimelike, self._values)
907+
elif is_extension_array_dtype(self._values):
908+
return iter(self._values)
909+
else:
910+
return map(self._values.item, range(self._values.size))
906911

907912
@cache_readonly
908913
def hasnans(self):

pandas/core/frame.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -816,14 +816,14 @@ def itertuples(self, index=True, name="Pandas"):
816816
Pandas(Index='b', col1=2, col2=0.20000000000000001)
817817
818818
"""
819-
arrays = []
819+
iterators = []
820820
fields = []
821821
if index:
822-
arrays.append(self.index)
822+
iterators.append(self.index)
823823
fields.append("Index")
824824

825825
# use integer indexing because of possible duplicate column names
826-
arrays.extend(self.iloc[:, k] for k in range(len(self.columns)))
826+
iterators.extend(self.iloc[:, k] for k in range(len(self.columns)))
827827

828828
# Python 3 supports at most 255 arguments to constructor, and
829829
# things get slow with this many fields in Python 2
@@ -833,12 +833,12 @@ def itertuples(self, index=True, name="Pandas"):
833833
itertuple = collections.namedtuple(name,
834834
fields + list(self.columns),
835835
rename=True)
836-
return map(itertuple._make, zip(*arrays))
836+
return map(itertuple._make, zip(*iterators))
837837
except Exception:
838838
pass
839839

840840
# fallback to regular tuples
841-
return zip(*arrays)
841+
return zip(*iterators)
842842

843843
items = iteritems
844844

0 commit comments

Comments
 (0)