Skip to content

Commit 9aaac80

Browse files
committed
BUG: yield correct Series subclass in df.iterrows() (#13977)
1 parent f8f4230 commit 9aaac80

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

doc/source/whatsnew/v0.19.0.txt

+2
Original file line numberDiff line numberDiff line change
@@ -1097,3 +1097,5 @@ Bug Fixes
10971097
- Bug in ``.to_string()`` when called with an integer ``line_width`` and ``index=False`` raises an UnboundLocalError exception because ``idx`` referenced before assignment.
10981098

10991099
- Bug in ``read_csv()``, where aliases for utf-xx (e.g. UTF-xx, UTF_xx, utf_xx) raised UnicodeDecodeError (:issue:`13549`)
1100+
1101+
- Bug in ``DataFrame.iterrows()`` not yielding proper ``Series`` subclasses (:issue:`13898`)

pandas/core/frame.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -694,8 +694,9 @@ def iterrows(self):
694694
695695
"""
696696
columns = self.columns
697+
klass = self._constructor_sliced
697698
for k, v in zip(self.index, self.values):
698-
s = Series(v, index=columns, name=k)
699+
s = klass(v, index=columns, name=k)
699700
yield k, s
700701

701702
def itertuples(self, index=True, name="Pandas"):

pandas/tests/frame/test_subclass.py

+7
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ def test_subclass_align_combinations(self):
211211
tm.assertIsInstance(res2, tm.SubclassedDataFrame)
212212
tm.assert_frame_equal(res2, exp1)
213213

214+
def test_subclass_iterrows(self):
215+
# GH 13977
216+
df = tm.SubclassedDataFrame({'a': [1]})
217+
for i, row in df.iterrows():
218+
tm.assertIsInstance(row, tm.SubclassedSeries)
219+
tm.assert_series_equal(row, df.loc[i])
220+
214221
def test_subclass_sparse_slice(self):
215222
rows = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]
216223
ssdf = tm.SubclassedSparseDataFrame(rows)

0 commit comments

Comments
 (0)