-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Auguste Baum <52001167+augustebaum@users.noreply.github.com>
- Loading branch information
1 parent
1b9b1ad
commit 3749ca7
Showing
8 changed files
with
147 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
"""PandasSeriesItem. | ||
This module defines the PandasSeriesItem class, | ||
which represents a pandas Series item. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from functools import cached_property | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
import pandas | ||
|
||
from skore.item.item import Item | ||
|
||
|
||
class PandasSeriesItem(Item): | ||
""" | ||
A class to represent a pandas Series item. | ||
This class encapsulates a pandas Series along with its | ||
creation and update timestamps. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
series_list: list, | ||
created_at: str | None = None, | ||
updated_at: str | None = None, | ||
): | ||
""" | ||
Initialize a PandasSeriesItem. | ||
Parameters | ||
---------- | ||
series_list : list | ||
The list representation of the series. | ||
created_at : str | ||
The creation timestamp in ISO format. | ||
updated_at : str | ||
The last update timestamp in ISO format. | ||
""" | ||
super().__init__(created_at, updated_at) | ||
|
||
self.series_list = series_list | ||
|
||
@cached_property | ||
def series(self) -> pandas.Series: | ||
"""The pandas Series.""" | ||
import pandas | ||
|
||
return pandas.Series(self.series_list) | ||
|
||
@classmethod | ||
def factory(cls, series: pandas.Series) -> PandasSeriesItem: | ||
""" | ||
Create a new PandasSeriesItem instance from a pandas Series. | ||
Parameters | ||
---------- | ||
series : pd.Series | ||
The pandas Series to store. | ||
Returns | ||
------- | ||
PandasSeriesItem | ||
A new PandasSeriesItem instance. | ||
""" | ||
import pandas | ||
|
||
if not isinstance(series, pandas.Series): | ||
raise TypeError(f"Type '{series.__class__}' is not supported.") | ||
|
||
instance = cls(series_list=series.to_list()) | ||
|
||
# add series as cached property | ||
instance.series = series | ||
|
||
return instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import pytest | ||
from pandas import Series | ||
from pandas.testing import assert_series_equal | ||
from skore.item import PandasSeriesItem | ||
|
||
|
||
class TestPandasSeriesItem: | ||
@pytest.fixture(autouse=True) | ||
def monkeypatch_datetime(self, monkeypatch, MockDatetime): | ||
monkeypatch.setattr("skore.item.item.datetime", MockDatetime) | ||
|
||
@pytest.mark.order(0) | ||
def test_factory(self, mock_nowstr): | ||
series = Series([0, 1, 2]) | ||
series_list = series.to_list() | ||
|
||
item = PandasSeriesItem.factory(series) | ||
|
||
assert item.series_list == series_list | ||
assert item.created_at == mock_nowstr | ||
assert item.updated_at == mock_nowstr | ||
|
||
@pytest.mark.order(1) | ||
def test_series(self, mock_nowstr): | ||
series = Series([0, 1, 2]) | ||
series_list = series.to_list() | ||
|
||
item1 = PandasSeriesItem.factory(series) | ||
item2 = PandasSeriesItem( | ||
series_list=series_list, | ||
created_at=mock_nowstr, | ||
updated_at=mock_nowstr, | ||
) | ||
|
||
assert_series_equal(item1.series, series) | ||
assert_series_equal(item2.series, series) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters