Skip to content
Prev Previous commit
Next Next commit
Return quickly if series
  • Loading branch information
Roger Thomas committed Jun 27, 2022
commit f66340d450c3ddda65e63f7b57de6ec998c8e85b
7 changes: 4 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1971,6 +1971,10 @@ def to_dict(self, orient: str = "dict", into=dict):
elif orient.startswith("i"):
orient = "index"

if orient == "series":
# GH46470 Return quickly if orient series to avoid creating dtype objects
return into_c((k, v) for k, v in self.items())

object_dtype_indices = [
i
for i, col_dtype in enumerate(self.dtypes.values)
Expand Down Expand Up @@ -2021,9 +2025,6 @@ def to_dict(self, orient: str = "dict", into=dict):
)
)

elif orient == "series":
return into_c((k, v) for k, v in self.items())

elif orient == "records":
columns = self.columns.tolist()
if are_all_object_dtype_cols:
Expand Down