Skip to content

Commit 042b6f0

Browse files
kerncjreback
authored andcommitted
BUG: yield correct Series subclass in df.iterrows() (pandas-dev#13977)
closes pandas-dev#13977 Author: Kernc <[email protected]> Closes pandas-dev#13978 from kernc/iterrows-with-constructor-sliced and squashes the following commits: 9aaac80 [Kernc] BUG: yield correct Series subclass in df.iterrows() (pandas-dev#13977) f8f4230 [Kernc] DOC: tm.assert_series_equal() fix docstring default values
1 parent 185fcbe commit 042b6f0

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

doc/source/whatsnew/v0.19.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,7 @@ Bug Fixes
12091209
- Bug in ``groupby`` where ``apply`` returns different result depending on whether first result is ``None`` or not (:issue:`12824`)
12101210
- Bug in ``groupby(..).nth()`` where the group key is included inconsistently if called after ``.head()/.tail()`` (:issue:`12839`)
12111211
- Bug in ``.to_html``, ``.to_latex`` and ``.to_string`` silently ignore custom datetime formatter passed through the ``formatters`` key word (:issue:`10690`)
1212+
- Bug in ``DataFrame.iterrows()``, not yielding a ``Series`` subclasse if defined (:issue:`13977`)
12121213

12131214
- Bug in ``pd.to_numeric`` when ``errors='coerce'`` and input contains non-hashable objects (:issue:`13324`)
12141215
- Bug in invalid ``Timedelta`` arithmetic and comparison may raise ``ValueError`` rather than ``TypeError`` (:issue:`13624`)

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)

pandas/util/testing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1104,10 +1104,10 @@ def assert_series_equal(left, right, check_dtype=True,
11041104
right : Series
11051105
check_dtype : bool, default True
11061106
Whether to check the Series dtype is identical.
1107-
check_index_type : bool / string {'equiv'}, default False
1107+
check_index_type : bool / string {'equiv'}, default 'equiv'
11081108
Whether to check the Index class, dtype and inferred_type
11091109
are identical.
1110-
check_series_type : bool, default False
1110+
check_series_type : bool, default True
11111111
Whether to check the Series class is identical.
11121112
check_less_precise : bool or int, default False
11131113
Specify comparison precision. Only used when check_exact is False.

0 commit comments

Comments
 (0)