-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: Cythonize from_nested_dict
#33485
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
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6d44e55
PERF: Cythonize `from_nested_dict`
78ae5ab
Merge remote-tracking branch 'upstream/master' into TODO-cythonize
61c841d
List issues
1c7ffbb
Added wrappers
94977b2
Converting the object dict inside the function
bcb25b9
Avoiding expensive call
b3ebae6
Merge remote-tracking branch 'upstream/master' into TODO-cythonize
7dfbca8
Converting the data to builtin dict, in the python space
c8e515f
Going less offen to the python space
4829f78
Got rid of unneeded vars
5faa02b
Better perf if we have nested series
d349b4f
Assigning the new value into a variable
4f8afed
Remove the `if` statement
babaf64
Merge remote-tracking branch 'upstream/master' into TODO-cythonize
e22fbda
Suggestion by @topper-123
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1265,8 +1265,13 @@ def from_dict(cls, data, orient="columns", dtype=None, columns=None) -> "DataFra | |
if orient == "index": | ||
if len(data) > 0: | ||
# TODO speed up Series case | ||
if isinstance(list(data.values())[0], (Series, dict)): | ||
data = _from_nested_dict(data) | ||
first_val = next(iter((data.values())), None) | ||
if isinstance(first_val, (Series, dict)): | ||
# If we are dealing with not a builtin dict, | ||
# `collections.defaultdict` for example, we need to convert it | ||
# to a regular dict so Cython will not raise. | ||
data = dict(data) if not type(data) is dict else data | ||
data = lib.from_nested_dict(data) | ||
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. If you change the above to data = dict(data) if not type(data) is dict else data # convert OrderedDict
data = lib.from_nested_dict(data) you can change the function interface in |
||
else: | ||
data, index = list(data.values()), list(data.keys()) | ||
elif orient == "columns": | ||
|
@@ -8817,12 +8822,3 @@ def isin(self, values) -> "DataFrame": | |
|
||
ops.add_flex_arithmetic_methods(DataFrame) | ||
ops.add_special_arithmetic_methods(DataFrame) | ||
|
||
|
||
def _from_nested_dict(data): | ||
# TODO: this should be seriously cythonized | ||
new_data = collections.defaultdict(dict) | ||
for index, s in data.items(): | ||
for col, v in s.items(): | ||
new_data[col][index] = v | ||
return new_data |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you keep with using the defaultdict? Should be more performant as it is implemented already in C
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.
Sure, one thing I do have a concern and it's the return type, if it's a
defaultdict
, the return type must be anobject
, unless we return it as adict
, e.gThere 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.
Does it make a difference whether typed as object or dict? I think both are just mapped to PyObject anyway so maybe doesn’t matter?