Skip to content

Commit

Permalink
Merge branch 'master' into fix/unit8co#1101
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzn committed Aug 7, 2022
2 parents a6356d4 + 7ad25b4 commit 8a90725
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
25 changes: 25 additions & 0 deletions darts/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,31 @@ def test_integer_indexing(self):
list(indexed_ts.time_index) == list(pd.RangeIndex(2, 7, step=1))
)

def test_univariate_component(self):
series = TimeSeries.from_values(np.array([10, 20, 30])).with_columns_renamed(
"0", "component"
)
mseries = concatenate([series] * 3, axis="component")
mseries = mseries.with_hierarchy(
{"component_1": ["component"], "component_2": ["component"]}
)

static_cov = pd.DataFrame(
{"dim0": [1, 2, 3], "dim1": [-2, -1, 0], "dim2": [0.0, 0.1, 0.2]}
)

mseries = mseries.with_static_covariates(static_cov)

for univ_series in [
mseries.univariate_component(1),
mseries.univariate_component("component_1"),
]:
# hierarchy should be dropped
self.assertIsNone(univ_series.hierarchy)

# only the right static covariate column should be retained
self.assertEqual(univ_series.static_covariates.sum().sum(), 1.1)

def test_column_names(self):
# test the column names resolution
columns_before = [
Expand Down
10 changes: 5 additions & 5 deletions darts/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2702,6 +2702,9 @@ def univariate_component(self, index: Union[str, int]) -> "TimeSeries":
Retrieve one of the components of the series
and return it as new univariate ``TimeSeries`` instance.
This drops the hierarchy (if any), and retains only the relevant static
covariates column.
Parameters
----------
index
Expand All @@ -2713,11 +2716,8 @@ def univariate_component(self, index: Union[str, int]) -> "TimeSeries":
TimeSeries
A new univariate TimeSeries instance.
"""
if isinstance(index, int):
new_xa = self._xa.isel(component=index).expand_dims(DIMS[1], axis=1)
else:
new_xa = self._xa.sel(component=index).expand_dims(DIMS[1], axis=1)
return self.__class__(new_xa)

return self[index if isinstance(index, str) else self.components[index]]

def add_datetime_attribute(
self, attribute, one_hot: bool = False, cyclic: bool = False
Expand Down

0 comments on commit 8a90725

Please sign in to comment.