Skip to content

Commit 68b18c5

Browse files
committed
REVIEWME: custom __iter__, iterates over rows, breaks pandas contract.
Only pandas' __str__ and __repr__ used iteration over rows, our tests pass either way. This is a very high-level decision which needs the concensus of the whole group.
1 parent 686a68b commit 68b18c5

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Orange/data/table/base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,28 @@ def __setitem__(self, key, value):
871871
# super call because we'd otherwise recurse back into this
872872
super().__setitem__(self._WEIGHTS_COLUMN, 1)
873873

874+
def __iter__(self):
875+
"""Iterate over the rows of this TableBase as SeriesBase, breaking the pandas contract.
876+
877+
Returns
878+
-------
879+
generator
880+
A generator of rows as SeriesBase objects.
881+
882+
Examples
883+
--------
884+
>>> iris = Table('iris')
885+
>>> for row in iris.iloc[:5]:
886+
>>> print(row)
887+
888+
Notes
889+
-----
890+
This breaks the pandas contract! Pandas iterates over column names by default.
891+
However, this only breaks __str__ and __repr__, which are reimplemented anyway.
892+
"""
893+
for _, row in self.iterrows():
894+
yield row
895+
874896
def __str__(self):
875897
"""Override the pandas representation to provide a more Orange-friendly one."""
876898
max_displayed_rows = 30

0 commit comments

Comments
 (0)