From 667f2cbc6cda9fb100f39e0d28ed3b6035a20454 Mon Sep 17 00:00:00 2001 From: shaido987 Date: Sun, 26 Jun 2022 14:23:52 +0800 Subject: [PATCH 1/3] Add drop_columns method --- darts/timeseries.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/darts/timeseries.py b/darts/timeseries.py index f8c244d017..9c2c142c89 100644 --- a/darts/timeseries.py +++ b/darts/timeseries.py @@ -2660,6 +2660,32 @@ def stack(self, other: "TimeSeries") -> "TimeSeries": """ return concatenate([self, other], axis=1) + def drop_columns(self, col_names: Union[List[str], str]) -> "TimeSeries": + """ + Return a new ``TimeSeries`` instance with dropped columns/components. + + Parameters + ------- + col_names + String or list of strings corresponding the the columns to be dropped. + + Returns + ------- + TimeSeries + A new TimeSeries instance with specified columns dropped. + """ + if isinstance(col_names, str): + col_names = [col_names] + + raise_if_not( + all([(x in self.columns.to_list()) for x in col_names]), + "Some column names in col_names don't exist in the time series.", + logger, + ) + + new_xa = self._xa.drop_sel({"component": col_names}) + return self.__class__(new_xa) + def univariate_component(self, index: Union[str, int]) -> "TimeSeries": """ Retrieve one of the components of the series From 5d31e726238235a181b39f731db572240c35e2fb Mon Sep 17 00:00:00 2001 From: shaido987 Date: Sun, 26 Jun 2022 14:25:57 +0800 Subject: [PATCH 2/3] Add drop columns test --- darts/tests/test_timeseries_multivariate.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/darts/tests/test_timeseries_multivariate.py b/darts/tests/test_timeseries_multivariate.py index f8850a4389..3c037ce567 100644 --- a/darts/tests/test_timeseries_multivariate.py +++ b/darts/tests/test_timeseries_multivariate.py @@ -243,3 +243,15 @@ def test_first_last_values(self): self.assertEqual( self.series3.univariate_component(1).last_values().tolist(), [20] ) + + def test_drop_column(self): + # testing dropping a single column + seriesA = self.series1.drop_columns("0") + self.assertNotIn("0", seriesA.columns.values) + self.assertEqual(seriesA.columns.tolist(), ["1", "2"]) + self.assertEqual(len(seriesA.columns), 2) + + # testing dropping multiple columns + seriesB = self.series1.drop_columns(["0", "1"]) + self.assertIn("2", seriesB.columns.values) + self.assertEqual(len(seriesB.columns), 1) From d26fb0fb882e8fa7b0aa5f3722c0db98ae6d9d44 Mon Sep 17 00:00:00 2001 From: shaido987 Date: Sun, 26 Jun 2022 14:39:15 +0800 Subject: [PATCH 3/3] Indentation fix --- darts/timeseries.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/darts/timeseries.py b/darts/timeseries.py index 9c2c142c89..c4a001d124 100644 --- a/darts/timeseries.py +++ b/darts/timeseries.py @@ -2660,7 +2660,7 @@ def stack(self, other: "TimeSeries") -> "TimeSeries": """ return concatenate([self, other], axis=1) - def drop_columns(self, col_names: Union[List[str], str]) -> "TimeSeries": + def drop_columns(self, col_names: Union[List[str], str]) -> "TimeSeries": """ Return a new ``TimeSeries`` instance with dropped columns/components.