Skip to content

Commit

Permalink
feat: class for time series (#508)
Browse files Browse the repository at this point in the history
Closes #481 

### Summary of Changes

<!-- Please provide a summary of changes in this pull request, ensuring
all changes are explained. -->
added the time series class, with all basic tests and functionalities
so it can be used like a normal table or taggedtrable with an extra time column

---------

Co-authored-by: Ettl<AndiWrp>
Co-authored-by: Simon <s6snbreu@uni-bonn.de>
Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com>
Co-authored-by: Simon Breuer <86068340+sibre28@users.noreply.github.com>
Co-authored-by: Alexander <47296670+Marsmaennchen221@users.noreply.github.com>
  • Loading branch information
5 people committed Jan 29, 2024
1 parent 0cfe80a commit 73cdfb1
Show file tree
Hide file tree
Showing 36 changed files with 3,827 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/safeds/data/tabular/containers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from ._row import Row
from ._table import Table
from ._tagged_table import TaggedTable
from ._time_series import TimeSeries

__all__ = [
"Column",
"Row",
"Table",
"TaggedTable",
"TimeSeries",
]
38 changes: 38 additions & 0 deletions src/safeds/data/tabular/containers/_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from safeds.data.tabular.transformation import InvertibleTableTransformer, TableTransformer

from ._tagged_table import TaggedTable
from ._time_series import TimeSeries

# Enable copy-on-write for pandas dataframes
pd.options.mode.copy_on_write = True
Expand Down Expand Up @@ -1715,6 +1716,43 @@ def tag_columns(self, target_name: str, feature_names: list[str] | None = None)

return TaggedTable._from_table(self, target_name, feature_names)

def time_columns(self, target_name: str, time_name: str, feature_names: list[str] | None = None) -> TimeSeries:
"""
Return a new `TimeSeries` with columns marked as a target and time column or feature columns.
The original table is not modified.
Parameters
----------
target_name : str
Name of the target column.
time_name : str
Name of the time column.
feature_names : list[str] | None
Names of the feature columns. If None, all columns except the target and time columns are used.
Returns
-------
time_series : TimeSeries
A new time series with the given target, time and feature names.
Raises
------
ValueError
If the target column is also a feature column.
ValueError
If there is no other column than the specified target and time columns left to be a feature column
Examples
--------
>>> from safeds.data.tabular.containers import Table, TimeSeries
>>> table = Table.from_dict({"time": ["01.01", "01.02", "01.03"], "price": [1.10, 1.19, 1.79], "amount_bought": [74, 72, 51]})
>>> tagged_table = table.time_columns(target_name="amount_bought",time_name = "time", feature_names=["price"])
"""
from ._time_series import TimeSeries

return TimeSeries._from_table_to_time_series(self, target_name, time_name, feature_names)

def transform_column(self, name: str, transformer: Callable[[Row], Any]) -> Table:
"""
Return a new `Table` with the provided column transformed by calling the provided transformer.
Expand Down
10 changes: 5 additions & 5 deletions src/safeds/data/tabular/containers/_tagged_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ def add_row(self, row: Row) -> TaggedTable:
Returns
-------
table : TaggedTable
A new table with the added row at the end.
A new tagged table with the added row at the end.
Raises
------
Expand All @@ -373,7 +373,7 @@ def add_rows(self, rows: list[Row] | Table) -> TaggedTable:
Returns
-------
result : TaggedTable
A new table which combines the original table and the given rows.
A new tagged table which combines the original table and the given rows.
Raises
------
Expand All @@ -386,7 +386,7 @@ def filter_rows(self, query: Callable[[Row], bool]) -> TaggedTable:
"""
Return a new `TaggedTable` containing only rows that match the given Callable (e.g. lambda function).
The original table is not modified.
The original tagged table is not modified.
Parameters
----------
Expand All @@ -395,8 +395,8 @@ def filter_rows(self, query: Callable[[Row], bool]) -> TaggedTable:
Returns
-------
table : TaggedTable
A table containing only the rows to match the query.
result : TaggedTable
A new tagged table containing only the rows to match the query.
"""
return TaggedTable._from_table(
super().filter_rows(query),
Expand Down
Loading

0 comments on commit 73cdfb1

Please sign in to comment.