Skip to content

(wip) BUG: itertuples was retuning empty list if frame had no columns #32252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Other
instead of ``TypeError: Can only append a Series if ignore_index=True or if the Series has a name`` (:issue:`30871`)
- Set operations on an object-dtype :class:`Index` now always return object-dtype results (:issue:`31401`)
- Bug in :meth:`AbstractHolidayCalendar.holidays` when no rules were defined (:issue:`31415`)
- Bug in :meth:`DataFrame.itertuples` was returning empty list when DataFrame has no columns (:issue:`25408`)
-

.. ---------------------------------------------------------------------------
Expand Down
6 changes: 5 additions & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,11 @@ def itertuples(self, index=True, name="Pandas"):
fields.insert(0, "Index")

# use integer indexing because of possible duplicate column names
arrays.extend(self.iloc[:, k] for k in range(len(self.columns)))
if len(self.columns) > 0:
arrays.extend(self.iloc[:, k] for k in range(len(self.columns)))
else:
arrays.extend([() for _ in range(len(self))])
return iter(arrays)

# Python versions before 3.7 support at most 255 arguments to constructors
can_return_named_tuples = PY37 or len(self.columns) + index < 255
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/methods/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,10 @@ def test_to_dict_orient_dtype(self):
"c": type(df_dict["c"]),
}
assert result == expected

def test_to_dict_no_rows_split(self):
# GH 25408
columns = ["A", "B"]
result = DataFrame(columns=columns).transpose().to_dict(orient="split")
expected = {"index": ["A", "B"], "columns": [], "data": [[], []]}
assert result == expected
6 changes: 6 additions & 0 deletions pandas/tests/frame/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ def test_itertuples(self, float_frame):
else:
assert not hasattr(result_255_columns, "_fields")

# GH 25408
df_0_columns = DataFrame(index=["A", "B"])
result = list(df_0_columns.itertuples())
tm.assert_index_equal(result[0], pd.Index(["A", "B"]))
assert result[1:] == [(), ()]

def test_sequence_like_with_categorical(self):

# GH 7839
Expand Down