-
Notifications
You must be signed in to change notification settings - Fork 115
[ENH] set partial triangle through loc #1103
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f18ce20
enable loc setting on partial triangles
henrydingliu a717850
fixing
henrydingliu 1187a52
adding test
henrydingliu de661e2
Loc setter (#1102)
henrydingliu c5d6f2b
more comment and docstrings; adding non-contig support
henrydingliu 992d5a2
Merge branch 'loc_setter' into loc_setter
henrydingliu d91f14d
Merge pull request #1178 from henrydingliu/loc_setter
henrydingliu 4618748
generalizing across iloc
henrydingliu 64329cc
Merge pull request #1179 from henrydingliu/loc_setter
henrydingliu ff1f162
Update slice.py
henrydingliu 82519ee
Merge pull request #1201 from casact/main
henrydingliu 06a4206
Update slice.py
henrydingliu 38765f1
Update slice.py
henrydingliu 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,7 +67,7 @@ def get_idx(self, idx: tuple[_AxisKey, _AxisKey, _AxisKey, _AxisKey]) -> Triangl | |
| c_idx: slice | np.ndarray = _LocBase._contig_slice(idx[1]) | ||
| o_idx: slice | np.ndarray = _LocBase._contig_slice(idx[2]) | ||
| d_idx: slice | np.ndarray = _LocBase._contig_slice(idx[3]) | ||
| if type(o_idx) != slice or type(d_idx) != slice: | ||
| if type(o_idx) is not slice or type(d_idx) is not slice: | ||
| raise ValueError("Fancy indexing on origin/development is not supported.") | ||
| if type(i_idx) is slice or type(c_idx) is slice: | ||
| obj.values = obj.values[i_idx, c_idx, o_idx, d_idx] | ||
|
|
@@ -150,11 +150,23 @@ def __setitem__( | |
| raise ValueError('Setting values with sparse backend requires .at or .iat') | ||
| if isinstance(values, TriangleSlicer): | ||
| values = values.values | ||
| # attempt to make keys contig | ||
| contig_key = tuple([_LocBase._contig_slice(x) for x in key]) | ||
| # Create a slice for any key elements that are integers, otherwise preserve the slice or array. | ||
| key = tuple( | ||
| [slice(item, item + 1) if isinstance(item, int) else item for item in key] | ||
| tuple_key = tuple( | ||
| [slice(item, item + 1) if isinstance(item, int) else item for item in contig_key] | ||
| ) | ||
| cast(np.ndarray, cast(object, self.obj.values)).__setitem__(self._normalize_index(key), values) | ||
| norm_key = self._normalize_index(tuple_key) | ||
| if type(norm_key[2]) is not slice or type(norm_key[3]) is not slice: | ||
| raise ValueError("Setting while fancy indexing on origin/development is not supported.") | ||
| if type(norm_key[0]) is slice or type(norm_key[1]) is slice: | ||
| cast(np.ndarray, cast(object, self.obj.values)).__setitem__(norm_key, values) | ||
| else: | ||
| #the getter uses arr[idx,:][:,idx] to get the Cartesian product, using np.ix_ on the setter to match | ||
| cast(np.ndarray, cast(object, self.obj.values)).__setitem__( | ||
| np.ix_(norm_key[0], norm_key[1]) + (norm_key[2], norm_key[3]), | ||
| values | ||
| ) | ||
|
|
||
| def _normalize_index(self, key: IndexExpression) -> tuple[_AxisKey, _AxisKey, _AxisKey, _AxisKey]: | ||
| """ | ||
|
|
@@ -174,19 +186,19 @@ def _normalize_index(self, key: IndexExpression) -> tuple[_AxisKey, _AxisKey, _A | |
| """ | ||
| # Apply sparse normalization, fills out the rest of the dimensions using the shape of the Triangle. | ||
| key: tuple[_AxisKey, _AxisKey, _AxisKey, _AxisKey] = _slicing.normalize_index(key, self.obj.shape) | ||
| l = [] | ||
| key_list = [] | ||
| # Preserve start/stop/step boundaries if the user has specified them, otherwise replace them with None. | ||
| # None indicates "go-to-boundary" for the slice. | ||
| for n, i in enumerate(key): | ||
| if isinstance(i, slice): | ||
| start: int | None= i.start if i.start > 0 else None | ||
| stop: int | None = i.stop if i.stop > -1 else None | ||
| stop: int | None = None if stop == self.obj.shape[n] else stop | ||
| step: int | None = None if start is None and stop is None else i.step | ||
| l.append(slice(start, stop, step)) | ||
| step: int | None = None if start is None and stop is None and (i.step == 1) else i.step | ||
| key_list.append(slice(start, stop, step)) | ||
| else: | ||
| l.append(i) | ||
| key = tuple(l) | ||
| key_list.append(i) | ||
| key = tuple(key_list) | ||
| return key | ||
|
|
||
| def _sparse_setitem( | ||
|
|
@@ -219,7 +231,7 @@ def _sparse_setitem( | |
| (arr.coords[3] == key[3])) | ||
| # If it does, index the location and assign the value directly. | ||
| if check.max(): | ||
| data_index = np.where(check == True)[0][0] | ||
| data_index = np.where(check)[0][0] | ||
| arr.data[data_index] = values | ||
| # Otherwise, create a new sparse array with the updated coordinates and data. | ||
| else: | ||
|
|
@@ -431,6 +443,21 @@ def key_to_slice(self, key: _LabelKey) -> tuple[_AxisKey, _AxisKey, _AxisKey, _A | |
| return out | ||
|
|
||
| def __setitem__(self, key: _LabelKey, values: int | float | TriangleSlicer) -> None: | ||
| """ | ||
| Supports the .loc[] for setting Triangle values. Only supported for numpy backend. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| key: _LabelKey | ||
| Indicates the location of the Triangle you want to set values for. | ||
| values: int | float | TriangleSlicer | ||
| The value(s) you want to assign to the slice of the Triangle. | ||
|
|
||
| Returns | ||
| ------- | ||
| None | ||
|
|
||
| """ | ||
| super().__setitem__(cast(tuple[_AxisKey, _AxisKey, _AxisKey, _AxisKey], self.key_to_slice(key)), values) | ||
|
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. Looks like I missed filling out a docstring when I annotated the file. Could you fill it out?
Member
Author
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. done |
||
|
|
||
| class Ilocation(_LocBase): | ||
|
|
@@ -454,7 +481,10 @@ class TriangleSlicer: | |
| def __getitem__(self: TriangleProtocol, key: pd.Series | np.ndarray | list[str]) -> Triangle: ... | ||
| @overload | ||
| def __getitem__(self: TriangleProtocol, key: str | int) -> Triangle | pd.Series: ... | ||
| def __getitem__(self: TriangleProtocol, key: pd.Series | np.ndarray | str | list[str] | int) -> Triangle | pd.Series: | ||
| def __getitem__( | ||
| self: TriangleProtocol, | ||
| key: pd.Series | np.ndarray | str | list[str] | int | ||
| ) -> Triangle | pd.Series: | ||
| """ | ||
| Boolean Slicer functionality. | ||
|
|
||
|
|
@@ -756,7 +786,7 @@ def _check_index(self, key: IndexExpression) -> tuple[int, int, int, int]: | |
| """ | ||
| idx = self._normalize_index(key) | ||
| types = {type(i) for i in idx} | ||
| if len(types) > 1 or list(types)[0] != int: | ||
| if len(types) > 1 or list(types)[0] is not int: | ||
| raise ValueError('iAt based indexing can only have integer indexers') | ||
| return cast("tuple[int, int, int, int]", idx) | ||
|
|
||
|
|
||
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.