-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Support create_index: bool in to_dataframe() to skip creating MultiIndex
#10979
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
dhruvak001
wants to merge
3
commits into
pydata:main
Choose a base branch
from
dhruvak001:issue#10912
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.
+89
−6
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7200,7 +7200,7 @@ def to_pandas(self) -> pd.Series | pd.DataFrame: | |||||||||||||||||||||
| "Please use Dataset.to_dataframe() instead." | ||||||||||||||||||||||
| ) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def _to_dataframe(self, ordered_dims: Mapping[Any, int]): | ||||||||||||||||||||||
| def _to_dataframe(self, ordered_dims: Mapping[Any, int], create_index: bool = True): | ||||||||||||||||||||||
| from xarray.core.extension_array import PandasExtensionArray | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| # All and only non-index arrays (whether data or coordinates) should | ||||||||||||||||||||||
|
|
@@ -7231,7 +7231,13 @@ def _to_dataframe(self, ordered_dims: Mapping[Any, int]): | |||||||||||||||||||||
| self._variables[k].set_dims(ordered_dims).values.reshape(-1) | ||||||||||||||||||||||
| for k in non_extension_array_columns | ||||||||||||||||||||||
| ] | ||||||||||||||||||||||
| index = self.coords.to_index([*ordered_dims]) | ||||||||||||||||||||||
| if create_index: | ||||||||||||||||||||||
| index = self.coords.to_index([*ordered_dims]) | ||||||||||||||||||||||
| else: | ||||||||||||||||||||||
| # Use a simple RangeIndex when create_index=False | ||||||||||||||||||||||
| # Calculate the total size from ordered_dims | ||||||||||||||||||||||
| total_size = np.prod(list(ordered_dims.values())) if ordered_dims else 0 | ||||||||||||||||||||||
| index = pd.RangeIndex(total_size) | ||||||||||||||||||||||
| broadcasted_df = pd.DataFrame( | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| **dict(zip(non_extension_array_columns, data, strict=True)), | ||||||||||||||||||||||
|
|
@@ -7259,7 +7265,11 @@ def _to_dataframe(self, ordered_dims: Mapping[Any, int]): | |||||||||||||||||||||
| broadcasted_df = broadcasted_df.join(extension_array_df) | ||||||||||||||||||||||
| return broadcasted_df[columns_in_order] | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def to_dataframe(self, dim_order: Sequence[Hashable] | None = None) -> pd.DataFrame: | ||||||||||||||||||||||
| def to_dataframe( | ||||||||||||||||||||||
| self, | ||||||||||||||||||||||
| dim_order: Sequence[Hashable] | None = None, | ||||||||||||||||||||||
| create_index: bool = True, | ||||||||||||||||||||||
| ) -> pd.DataFrame: | ||||||||||||||||||||||
| """Convert this dataset into a pandas.DataFrame. | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Non-index variables in this dataset form the columns of the | ||||||||||||||||||||||
|
|
@@ -7278,6 +7288,11 @@ def to_dataframe(self, dim_order: Sequence[Hashable] | None = None) -> pd.DataFr | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| If provided, must include all dimensions of this dataset. By | ||||||||||||||||||||||
| default, dimensions are in the same order as in `Dataset.sizes`. | ||||||||||||||||||||||
| create_index : bool, default: True | ||||||||||||||||||||||
| If True (default), create a MultiIndex from the Cartesian product | ||||||||||||||||||||||
| of this dataset's indices. If False, use a RangeIndex instead. | ||||||||||||||||||||||
| This can be useful to avoid the potentially expensive MultiIndex | ||||||||||||||||||||||
| creation. | ||||||||||||||||||||||
|
Comment on lines
+7293
to
+7297
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.
Suggested change
To avoid any confusion with |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| Returns | ||||||||||||||||||||||
| ------- | ||||||||||||||||||||||
|
|
@@ -7288,7 +7303,7 @@ def to_dataframe(self, dim_order: Sequence[Hashable] | None = None) -> pd.DataFr | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| ordered_dims = self._normalize_dim_order(dim_order=dim_order) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return self._to_dataframe(ordered_dims=ordered_dims) | ||||||||||||||||||||||
| return self._to_dataframe(ordered_dims=ordered_dims, create_index=create_index) | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| def _set_sparse_data_from_dataframe( | ||||||||||||||||||||||
| self, idx: pd.Index, arrays: list[tuple[Hashable, np.ndarray]], dims: tuple | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
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
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.
Uh oh!
There was an error while loading. Please reload this page.