-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
PERF: Slow performance of to_dict (#46470) #46487
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
Changes from 5 commits
8d93fec
d9f9786
96ac6fa
3c596f7
57c95eb
e07f02c
d2da86b
0c0481d
1d68f83
b413f99
f66340d
03978aa
e1aee52
aa9863d
ecfffa6
6487cc9
36e22c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1909,41 +1909,71 @@ def to_dict(self, orient: str = "dict", into=dict): | |
elif orient.startswith("i"): | ||
orient = "index" | ||
|
||
object_dtype_cols = { | ||
col for col, dtype in self.dtypes.items() if is_object_dtype(dtype) | ||
} | ||
are_all_object_dtype_cols = len(object_dtype_cols) == len(self.dtypes) | ||
rhshadrach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if orient == "dict": | ||
return into_c((k, v.to_dict(into)) for k, v in self.items()) | ||
|
||
elif orient == "list": | ||
return into_c( | ||
(k, list(map(maybe_box_native, v.tolist()))) for k, v in self.items() | ||
( | ||
k, | ||
list(map(maybe_box_native, v.tolist())) | ||
if k in object_dtype_cols | ||
else v.tolist(), | ||
) | ||
for k, v in self.items() | ||
) | ||
|
||
elif orient == "split": | ||
if are_all_object_dtype_cols: | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data = [ | ||
list(map(maybe_box_native, t)) | ||
for t in self.itertuples(index=False, name=None) | ||
] | ||
else: | ||
data = [list(t) for t in self.itertuples(index=False, name=None)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you share code between any of these cases? e.g. make a helper function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jreback done |
||
if object_dtype_cols: | ||
object_dtype_indices = [ | ||
i | ||
for i, col in enumerate(self.columns) | ||
if col in object_dtype_cols | ||
] | ||
for row in data: | ||
for i in object_dtype_indices: | ||
row[i] = maybe_box_native(row[i]) | ||
return into_c( | ||
( | ||
("index", self.index.tolist()), | ||
("columns", self.columns.tolist()), | ||
( | ||
"data", | ||
[ | ||
list(map(maybe_box_native, t)) | ||
for t in self.itertuples(index=False, name=None) | ||
], | ||
), | ||
("data", data), | ||
) | ||
) | ||
|
||
elif orient == "tight": | ||
if are_all_object_dtype_cols: | ||
data = [ | ||
list(map(maybe_box_native, t)) | ||
for t in self.itertuples(index=False, name=None) | ||
] | ||
else: | ||
data = [list(t) for t in self.itertuples(index=False, name=None)] | ||
if object_dtype_cols: | ||
object_dtype_indices = [ | ||
i | ||
for i, col in enumerate(self.columns) | ||
if col in object_dtype_cols | ||
] | ||
for row in data: | ||
for i in object_dtype_indices: | ||
row[i] = maybe_box_native(row[i]) | ||
return into_c( | ||
( | ||
("index", self.index.tolist()), | ||
("columns", self.columns.tolist()), | ||
( | ||
"data", | ||
[ | ||
list(map(maybe_box_native, t)) | ||
for t in self.itertuples(index=False, name=None) | ||
], | ||
), | ||
("data", data), | ||
("index_names", list(self.index.names)), | ||
("column_names", list(self.columns.names)), | ||
) | ||
|
@@ -1954,21 +1984,56 @@ def to_dict(self, orient: str = "dict", into=dict): | |
|
||
elif orient == "records": | ||
columns = self.columns.tolist() | ||
rows = ( | ||
dict(zip(columns, row)) | ||
for row in self.itertuples(index=False, name=None) | ||
) | ||
return [ | ||
into_c((k, maybe_box_native(v)) for k, v in row.items()) for row in rows | ||
] | ||
if are_all_object_dtype_cols: | ||
rows = ( | ||
dict(zip(columns, row)) | ||
for row in self.itertuples(index=False, name=None) | ||
) | ||
return [ | ||
into_c((k, maybe_box_native(v)) for k, v in row.items()) | ||
for row in rows | ||
] | ||
else: | ||
data = [ | ||
into_c(zip(columns, t)) | ||
for t in self.itertuples(index=False, name=None) | ||
] | ||
if object_dtype_cols: | ||
for row in data: | ||
for col in object_dtype_cols: | ||
row[col] = maybe_box_native(row[col]) | ||
return data | ||
|
||
elif orient == "index": | ||
if not self.index.is_unique: | ||
raise ValueError("DataFrame index must be unique for orient='index'.") | ||
return into_c( | ||
(t[0], dict(zip(self.columns, map(maybe_box_native, t[1:])))) | ||
for t in self.itertuples(name=None) | ||
) | ||
columns = self.columns.tolist() | ||
if are_all_object_dtype_cols: | ||
return into_c( | ||
(t[0], dict(zip(self.columns, map(maybe_box_native, t[1:])))) | ||
for t in self.itertuples(name=None) | ||
) | ||
elif object_dtype_cols: | ||
is_object_dtype_by_index = [ | ||
col in object_dtype_cols for col in self.columns | ||
] | ||
return into_c( | ||
( | ||
t[0], | ||
{ | ||
columns[i]: maybe_box_native(v) | ||
if is_object_dtype_by_index[i] | ||
else v | ||
for i, v in enumerate(t[1:]) | ||
}, | ||
) | ||
for t in self.itertuples(name=None) | ||
) | ||
else: | ||
return into_c( | ||
(t[0], dict(zip(self.columns, t[1:]))) | ||
for t in self.itertuples(name=None) | ||
) | ||
|
||
else: | ||
raise ValueError(f"orient '{orient}' not understood") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -380,6 +380,16 @@ def test_to_dict_orient_tight(self, index, columns): | |
"b": [float, float, float], | ||
}, | ||
), | ||
( # Make sure we have one df which is all object type cols | ||
{ | ||
"a": [1, "hello", 3], | ||
"b": [1.1, "world", 3.3], | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this hits all of the new added code? |
||
{ | ||
"a": [int, str, int], | ||
"b": [float, str, float], | ||
}, | ||
), | ||
), | ||
) | ||
def test_to_dict_returns_native_types(self, orient, data, expected_types): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to move the note to 2.0