-
-
Notifications
You must be signed in to change notification settings - Fork 19.3k
BUG: Fix DataFrame.from_dict empty row drop #62893
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
Open
parthava-adabala
wants to merge
5
commits into
pandas-dev:main
Choose a base branch
from
parthava-adabala:fix-from-dict-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+64
−4
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea680cd
BUG: Fix DataFrame.from_dict empty row drop
parthava-adabala 52046f4
Trigger CI
parthava-adabala 4c653b5
Merge branch 'main' into fix-from-dict-bug
parthava-adabala 1d0f442
Merge branch 'main' into fix-from-dict-bug
parthava-adabala 630b3e8
Merge branch 'main' into fix-from-dict-bug
parthava-adabala 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 |
|---|---|---|
|
|
@@ -1929,7 +1929,11 @@ def from_dict( | |
| if len(data) > 0: | ||
| # TODO speed up Series case | ||
| if isinstance(next(iter(data.values())), (Series, dict)): | ||
| original_keys = list(data.keys()) | ||
| data = _from_nested_dict(data) | ||
| if not data and columns is None: | ||
| columns = [] | ||
| index = original_keys | ||
| else: | ||
| index = list(data.keys()) | ||
| # error: Incompatible types in assignment (expression has type | ||
|
|
@@ -14415,13 +14419,26 @@ def values(self) -> np.ndarray: | |
|
|
||
| def _from_nested_dict( | ||
| data: Mapping[HashableT, Mapping[HashableT2, T]], | ||
| ) -> collections.defaultdict[HashableT2, dict[HashableT, T]]: | ||
| new_data: collections.defaultdict[HashableT2, dict[HashableT, T]] = ( | ||
| ) -> collections.defaultdict[HashableT2, dict[HashableT, Any]]: | ||
| new_data: collections.defaultdict[HashableT2, dict[HashableT, Any]] = ( | ||
| collections.defaultdict(dict) | ||
| ) | ||
| all_cols_dict = {} | ||
| for s in data.values(): | ||
| if isinstance(s, (dict, ABCSeries)): | ||
| all_cols_dict.update(dict.fromkeys(s.keys())) | ||
| all_cols_list = list(all_cols_dict.keys()) | ||
| if not all_cols_list: | ||
| return new_data | ||
| for index, s in data.items(): | ||
| for col, v in s.items(): | ||
| new_data[col][index] = v | ||
| if isinstance(s, (dict, ABCSeries)): | ||
| for col in all_cols_list: | ||
| new_data[col][index] = s.get(col, None) | ||
|
Member
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. For a Series, I believe this will be much slower than the previous implementation. Can you profile this case. |
||
| elif s is None or is_scalar(s): | ||
|
Member
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. For what input is this case hit? |
||
| for col in all_cols_list: | ||
| new_data[col][index] = s | ||
| else: | ||
| raise TypeError(f"Value at index {index} is not a dict/Series/scalar/None") | ||
| return new_data | ||
|
|
||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
Why is
T -> Anynecessary?