Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update this melt so that it is not mandatory to have a 'time' indice #61

Open
fredmontet opened this issue Sep 18, 2024 · 3 comments · May be fixed by #73
Open

Update this melt so that it is not mandatory to have a 'time' indice #61

fredmontet opened this issue Sep 18, 2024 · 3 comments · May be fixed by #73
Assignees
Labels
bug Something isn't working

Comments

@fredmontet
Copy link
Collaborator

df = df.melt("time", var_name="variable", value_name="value")

@fredmontet fredmontet self-assigned this Sep 18, 2024
@fredmontet fredmontet added the bug Something isn't working label Sep 18, 2024
@fredmontet fredmontet added this to the Plots milestone Sep 18, 2024
@fredmontet
Copy link
Collaborator Author

Here you go @ben-jy, I added everything in the param of the function to be able to adjust this when needed, while staying non-destructive.

@ben-jy
Copy link
Collaborator

ben-jy commented Jan 6, 2025

there is another problem that could be fix in this issue. When a time series has a component named "value", an error is raised :

ts = on.TimeSeries.from_darts(EnergyDataset().load())[:1000]
ts.rename({"generation biomass": "value"}).plot()

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[39], line 1
----> 1 ts.rename({"generation biomass": "value"}).plot()

File ~/projects_dev/ontime/docs/../src/ontime/core/time_series/time_series.py:32, in TimeSeries.plot(self, width, height, **kwargs)
     28 from ..plotting.plot import Plot
     29 from ..plotting._marks.line import line
     31 return (
---> 32     Plot(self).add(line, **kwargs).properties(width=width, height=height).show()
     33 )

File ~/projects_dev/ontime/docs/../src/ontime/core/plotting/plot.py:26, in Plot.add(self, mark, ts, **kwargs)
     17 """
     18 Add a mark to the plot
     19 
   (...)
     23 :return: Plot
     24 """
     25 ts = self.ts if ts is None else ts
---> 26 self.layers.append(mark(ts, **kwargs))
     27 return self

File ~/projects_dev/ontime/docs/../src/ontime/core/plotting/_marks/line.py:17, in line(ts, type, encode_kwargs)
      7 def line(ts: TimeSeries, type: str = None, encode_kwargs: dict = None) -> Chart:
      8     """
      9     Line plot for TimeSeries
     10 
   (...)
     14     :return: Altair Chart
     15     """
---> 17     df = Plot.melt(ts)
     19     default_kwargs = {
     20         "x": "time:T",
     21         "y": "value:Q",
     22         "color": "variable:N",
     23     }
     24     encode_kwargs = encode_kwargs if encode_kwargs is not None else default_kwargs

File ~/projects_dev/ontime/docs/../src/ontime/core/plotting/plot.py:57, in Plot.melt(ts)
     55 df = ts.pd_dataframe()
     56 df = df.reset_index()
---> 57 df = df.melt("time", var_name="variable", value_name="value")
     58 return df

File ~/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages/pandas/core/frame.py:9942, in DataFrame.melt(self, id_vars, value_vars, var_name, value_name, col_level, ignore_index)
   9932 @Appender(_shared_docs["melt"] % {"caller": "df.melt(", "other": "melt"})
   9933 def melt(
   9934     self,
   (...)
   9940     ignore_index: bool = True,
   9941 ) -> DataFrame:
-> 9942     return melt(
   9943         self,
   9944         id_vars=id_vars,
   9945         value_vars=value_vars,
   9946         var_name=var_name,
   9947         value_name=value_name,
   9948         col_level=col_level,
   9949         ignore_index=ignore_index,
   9950     ).__finalize__(self, method="melt")

File ~/.cache/pypoetry/virtualenvs/ontime-2OQVvbNf-py3.10/lib/python3.10/site-packages/pandas/core/reshape/melt.py:54, in melt(frame, id_vars, value_vars, var_name, value_name, col_level, ignore_index)
     43 @Appender(_shared_docs["melt"] % {"caller": "pd.melt(df, ", "other": "DataFrame.melt"})
     44 def melt(
     45     frame: DataFrame,
   (...)
     51     ignore_index: bool = True,
     52 ) -> DataFrame:
     53     if value_name in frame.columns:
---> 54         raise ValueError(
     55             f"value_name ({value_name}) cannot match an element in "
     56             "the DataFrame columns."
     57         )
     58     id_vars = ensure_list_vars(id_vars, "id_vars", frame.columns)
     59     value_vars_was_not_none = value_vars is not None

ValueError: value_name (value) cannot match an element in the DataFrame columns.

@ben-jy ben-jy modified the milestones: Plots, v0.9 Plotting module improvements Jan 8, 2025
@ben-jy
Copy link
Collaborator

ben-jy commented Jan 8, 2025

Here you go @ben-jy, I added everything in the param of the function to be able to adjust this when needed, while staying non-destructive.

Actually, melt() method is never called directly by the user, the parameters to define the time index and value column names should be given at the time the plot is created (i guess)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants