Skip to content

Commit 6e385f6

Browse files
committed
_
1 parent 995a4d1 commit 6e385f6

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

skrub/_session_encoder.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
The name of the session column is "{timestamp}_{suffix}", where "timestamp" is the name
1212
of the timestamp column, and "suffix" is a string that can be set via the "suffix"
1313
parameter (default is "session_id"). The session column contains a unique identifier for
14-
each session, which is a combination of the "split_by" column(s) and a session number
14+
each session, which is a combination of the "split_by" column(s) and a session number.
15+
16+
Null values in "timestamp" or "split_by" columns are assigned a session ID of -1,
17+
and are ignored when computing time intervals.
18+
1519
"""
1620

1721
import numbers
@@ -35,14 +39,14 @@
3539

3640

3741
@dispatch
38-
def _add_session_column(
42+
def _get_session_column(
3943
X, split_by_columns, timestamp_column, session_gap, session_id_column
4044
):
4145
raise_dispatch_unregistered_type(X, kind="Dataframe")
4246

4347

44-
@_add_session_column.specialize("pandas")
45-
def _add_session_column_pandas(
48+
@_get_session_column.specialize("pandas")
49+
def _get_session_column_pandas(
4650
X, split_by_columns, timestamp_column, session_gap, session_id_column
4751
):
4852
# Adding a row order column to sort lines back
@@ -102,8 +106,8 @@ def _add_session_column_pandas(
102106
return X_with_session_id.sort_values(by=row_order_col)[session_id_column]
103107

104108

105-
@_add_session_column.specialize("polars")
106-
def _add_session_column_polars(
109+
@_get_session_column.specialize("polars")
110+
def _get_session_column_polars(
107111
X, split_by_columns, timestamp_column, session_gap, session_id_column
108112
):
109113
# Adding a row order column to sort lines back
@@ -566,7 +570,7 @@ def transform(self, X, y=None):
566570
X, **{self.session_id_column_: np.array([], dtype=np.int64)}
567571
)
568572

569-
session_id = _add_session_column(
573+
session_id = _get_session_column(
570574
X,
571575
self._split_by_columns,
572576
self.timestamp_col,

skrub/tests/test_session_encoder.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from packaging.version import parse
77

88
from .. import _dataframe as sbd
9-
from .._session_encoder import SessionEncoder, _add_session_column
9+
from .._session_encoder import SessionEncoder, _get_session_column
1010

1111

1212
@pytest.fixture
@@ -542,7 +542,7 @@ def test_get_feature_names(df_module):
542542

543543

544544
@pytest.mark.skipif(parse(pd.__version__).major >= 3, reason="Test only for pandas < 3")
545-
def test_add_session_column_old_pandas(df_module):
545+
def test_get_session_column_old_pandas(df_module):
546546
"""Old versions of pandas have a different branch that needs to be covered"""
547547
df = df_module.make_dataframe(
548548
{
@@ -555,7 +555,7 @@ def test_add_session_column_old_pandas(df_module):
555555
}
556556
)
557557
session_id = sbd.to_list(
558-
_add_session_column(
558+
_get_session_column(
559559
df, [], "timestamp", 30 * 60, session_id_column="timestamp_session_id"
560560
),
561561
)
@@ -592,7 +592,7 @@ def test_proper_suffix(timestamp, suffix, df_module):
592592
result = SessionEncoder(
593593
timestamp_col=timestamp, split_by="user_id", suffix=suffix
594594
).fit_transform(df)
595-
# _add_session_column now returns the full dataframe with session_id added
595+
# SessionEncoder now returns the full dataframe with session_id added
596596
expected_name = f"{timestamp}_{suffix}"
597597
assert expected_name in sbd.column_names(result)
598598

@@ -625,7 +625,7 @@ def test_preserves_input_order(df_module):
625625

626626
def test_error_dispatch():
627627
with pytest.raises(TypeError, match="Expecting a Pandas or Polars Dataframe"):
628-
_add_session_column(
628+
_get_session_column(
629629
np.array([1]),
630630
split_by_columns=[],
631631
timestamp_column="timestamp",

0 commit comments

Comments
 (0)