-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add set_index
, reset_index
and reorder_levels
methods
#1028
Merged
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
aad4a09
add Dataset.set_index method
4fe06b3
add set_index and reset_index methods for dataarray and dataset
08ccdc2
add reorder_levels method for dataset and dataarray
9f53c72
add tests
363b463
update doc
4388142
fix tests py27
f8797fa
review changes
12c5966
fix unresolved rebase conflict
2e3e525
fix reset_index example in docs
1419c00
Merge branch 'master' into multi-index_methods
3dfa539
fix docstring
60853fd
change signature of reset_index
65ebc19
add type annotations
bdefddf
Merge branch 'master' into multi-index_methods
83ca06b
update missing coordinate dims
5ba2ffa
fix and update docs
c58cb47
updated doc
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 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 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 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 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 |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
from .common import AbstractArray, BaseDataObject | ||
from .coordinates import (DataArrayCoordinates, LevelCoordinates, | ||
Indexes) | ||
from .dataset import Dataset | ||
from .dataset import Dataset, merge_indexes, split_indexes | ||
from .pycompat import iteritems, basestring, OrderedDict, zip | ||
from .variable import (as_variable, Variable, as_compatible_data, IndexVariable, | ||
default_index_coordinate, | ||
|
@@ -821,6 +821,108 @@ def swap_dims(self, dims_dict): | |
ds = self._to_temp_dataset().swap_dims(dims_dict) | ||
return self._from_temp_dataset(ds) | ||
|
||
def set_index(self, append=False, inplace=False, **indexes): | ||
"""Set DataArray (multi-)indexes using one or more existing coordinates. | ||
|
||
Parameters | ||
---------- | ||
append : bool, optional | ||
If True, append the supplied index(es) to the existing index(es). | ||
Otherwise replace the existing index(es) (default). | ||
inplace : bool, optional | ||
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. I'm not sure we need an |
||
If True, set new index(es) in-place. Otherwise, return a new DataArray | ||
object. | ||
**indexes : {dim: index, ...} | ||
Keyword arguments with names matching dimensions and values given | ||
by (lists of) the names of existing coordinates or variables to set | ||
as new (multi-)index. | ||
|
||
Returns | ||
------- | ||
reindexed : DataArray | ||
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. wrong name -- should not be |
||
Another dataarray, with this dataarray's data but replaced coordinates. | ||
|
||
See Also | ||
-------- | ||
DataArray.reset_index | ||
""" | ||
coords, _ = merge_indexes(indexes, self._coords, set(), append=append) | ||
if inplace: | ||
self._coords = coords | ||
else: | ||
return self._replace(coords=coords) | ||
|
||
def reset_index(self, dim, levels=None, drop=False, inplace=False): | ||
"""Extract index(es) as new coordinates. | ||
|
||
Parameters | ||
---------- | ||
dim : str or list | ||
Name(s) of the dimension(s) for which to extract and reset | ||
the index. | ||
levels : list or None, optional | ||
If None (default) and if `dim` has a multi-index, extract all levels | ||
as new coordinates. Otherwise extract only the given list of level | ||
names. If more than one dimension is given in `dim`, `levels` should | ||
be a list of the same length than `dim` (or simply None to extract | ||
all indexes/levels from all given dimensions). | ||
drop : bool, optional | ||
If True, remove the specified levels instead of extracting them as | ||
new coordinates (default: False). | ||
inplace : bool, optional | ||
If True, modify the dataarray in-place. Otherwise, return a new | ||
DataArray object. | ||
|
||
Returns | ||
------- | ||
reindexed: DataArray | ||
Another dataarray, with this dataarray's data but replaced | ||
coordinates. | ||
|
||
See Also | ||
-------- | ||
DataArray.set_index | ||
""" | ||
coords, _ = split_indexes(dim, levels, self._coords, set(), drop=drop) | ||
if inplace: | ||
self._coords = coords | ||
else: | ||
return self._replace(coords=coords) | ||
|
||
def reorder_levels(self, inplace=False, **dim_order): | ||
"""Rearrange index levels using input order. | ||
|
||
Parameters | ||
---------- | ||
inplace : bool, optional | ||
If True, modify the dataarray in-place. Otherwise, return a new | ||
DataArray object. | ||
**dim_order : optional | ||
Keyword arguments with names matching dimensions and values given | ||
by lists representing new level orders. Every given dimension | ||
must have a multi-index. | ||
|
||
Returns | ||
------- | ||
reindexed: DataArray | ||
Another dataarray, with this dataarray's data but replaced | ||
coordinates. | ||
""" | ||
replace_coords = {} | ||
for dim, order in dim_order.items(): | ||
coord = self._coords[dim] | ||
index = coord.to_index() | ||
if not isinstance(index, pd.MultiIndex): | ||
raise ValueError("coordinate %r has no MultiIndex" % dim) | ||
replace_coords[dim] = IndexVariable(coord.dims, | ||
index.reorder_levels(order)) | ||
coords = self._coords.copy() | ||
coords.update(replace_coords) | ||
if inplace: | ||
self._coords = coords | ||
else: | ||
return self._replace(coords=coords) | ||
|
||
def stack(self, **dimensions): | ||
""" | ||
Stack any number of existing dimensions into a single new dimension. | ||
|
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.
These docs are great, but I wouldn't call them "indexing methods" exactly. Maybe move this section to Reshaping and reorganizing data?
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.
Makes sense!