Skip to content

Commit

Permalink
fix TimeSeries.pd_series not retaining original name (unit8co#2102)
Browse files Browse the repository at this point in the history
* fix TimeSeries.pd_series not retaining original name

* Improve access to TimeSeries's original name

By directly accessing to the `components.array` attribute from the TimeSeries
instance, I do not have to convert the `components` attribute to a list
anymore.

* Apply suggestions from code review

As suggested by @dennisbader, we can directly access the items in the `components` attribute from the TimeSeries instance, no need to use the `components.array` attribute.

Co-authored-by: Dennis Bader <[email protected]>

* Reformat code to pass lint check

* fix failing unit test

---------

Co-authored-by: Dennis Bader <[email protected]>
  • Loading branch information
fmerinocasallo and dennisbader authored Dec 10, 2023
1 parent 70752ce commit 8897e93
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
12 changes: 12 additions & 0 deletions darts/tests/test_timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions darts/tests/test_timeseries_static_covariates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions darts/timeseries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down

0 comments on commit 8897e93

Please sign in to comment.