diff --git a/darts/tests/test_timeseries.py b/darts/tests/test_timeseries.py index 1055118250..db5459fee6 100644 --- a/darts/tests/test_timeseries.py +++ b/darts/tests/test_timeseries.py @@ -78,6 +78,18 @@ def test_creation(self): ) _ = TimeSeries.from_xarray(ar) + def test_pandas_creation(self): + pd_series = pd.Series(range(10), name="test_name", dtype="float32") + ts = TimeSeries.from_series(pd_series) + ts_pd_series = ts.pd_series() + assert ts_pd_series.equals(pd_series) + assert ts_pd_series.name == pd_series.name + + pd_df = pd_series.to_frame() + ts = TimeSeries.from_dataframe(pd_df) + ts_pd_df = ts.pd_dataframe() + assert ts_pd_df.equals(pd_df) + def test_integer_range_indexing(self): # sanity checks for the integer-indexed series range_indexed_data = np.random.randn( diff --git a/darts/tests/test_timeseries_static_covariates.py b/darts/tests/test_timeseries_static_covariates.py index 2a6d1ee986..917f76a49e 100644 --- a/darts/tests/test_timeseries_static_covariates.py +++ b/darts/tests/test_timeseries_static_covariates.py @@ -61,8 +61,7 @@ def test_ts_from_x(self, tmpdir_module): ts.pd_dataframe(), static_covariates=ts.static_covariates ), ) - # ts.pd_series() loses component names -> static covariates have different components names - self.helper_test_cov_transfer_values( + self.helper_test_cov_transfer( ts, TimeSeries.from_series( ts.pd_series(), static_covariates=ts.static_covariates diff --git a/darts/timeseries.py b/darts/timeseries.py index 8e8429fa3d..1792dfe345 100644 --- a/darts/timeseries.py +++ b/darts/timeseries.py @@ -1472,10 +1472,16 @@ def pd_series(self, copy=True) -> pd.Series: self._assert_deterministic() if copy: return pd.Series( - self._xa[:, 0, 0].values.copy(), index=self._time_index.copy() + self._xa[:, 0, 0].values.copy(), + index=self._time_index.copy(), + name=self.components[0], ) else: - return pd.Series(self._xa[:, 0, 0].values, index=self._time_index) + return pd.Series( + self._xa[:, 0, 0].values, + index=self._time_index, + name=self.components[0], + ) def pd_dataframe(self, copy=True, suppress_warnings=False) -> pd.DataFrame: """