-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from DHI/timeselector
Extract class time step selection
- Loading branch information
Showing
7 changed files
with
177 additions
and
111 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from __future__ import annotations | ||
from datetime import datetime | ||
from dataclasses import dataclass | ||
from typing import List, Iterable, Optional | ||
|
||
import pandas as pd | ||
|
||
|
||
@dataclass | ||
class DateTimeSelector: | ||
"""Helper class for selecting time steps from a pandas DatetimeIndex""" | ||
|
||
index: pd.DatetimeIndex | ||
|
||
def isel( | ||
self, | ||
x: Optional[ | ||
int | Iterable[int] | str | datetime | pd.DatetimeIndex | slice | ||
] = None, | ||
) -> List[int]: | ||
"""Select time steps from a pandas DatetimeIndex | ||
Parameters | ||
---------- | ||
x : int, Iterable[int], str, datetime, pd.DatetimeIndex, slice | ||
Time steps to select, negative indices are supported | ||
Returns | ||
------- | ||
List[int] | ||
List of indices in the range (0, len(index) | ||
Examples | ||
-------- | ||
>>> idx = pd.date_range("2000-01-01", periods=4, freq="D") | ||
>>> dts = DateTimeSelector(idx) | ||
>>> dts.isel(None) | ||
[0, 1, 2, 3] | ||
>>> dts.isel(0) | ||
[0] | ||
>>> dts.isel(-1) | ||
[3] | ||
""" | ||
|
||
indices = list(range(len(self.index))) | ||
|
||
if x is None: | ||
return indices | ||
|
||
if isinstance(x, int): | ||
return [indices[x]] | ||
|
||
if isinstance(x, (datetime, str)): | ||
loc = self.index.get_loc(x) | ||
if isinstance(loc, int): | ||
return [loc] | ||
elif isinstance(loc, slice): | ||
return list(range(loc.start, loc.stop)) | ||
|
||
if isinstance(x, slice): | ||
if isinstance(x.start, int) or isinstance(x.stop, int): | ||
return indices[x] | ||
else: | ||
s = self.index.slice_indexer(x.start, x.stop) | ||
return list(range(s.start, s.stop)) | ||
|
||
if isinstance(x, Iterable): | ||
return [self.isel(t)[0] for t in x] | ||
|
||
return indices |
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
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
Oops, something went wrong.