Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions tests/units/feature_extraction/test_categorical_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""Feature extraction must not invent time series for unused ID categories."""

import pandas as pd
import pytest

from tsfresh import extract_features


@pytest.mark.parametrize("data_format", ["wide", "long", "dict"])
@pytest.mark.parametrize("column_sort", [None, "time"])
@pytest.mark.parametrize("id_type", ["integer", "categorical", "unused_category"])
def test_extract_features_only_observed_ids(data_format, column_sort, id_type):
"""Filtering categorical IDs must produce the same rows as integer IDs."""
df = pd.DataFrame({"id": [1, 3, 1, 3], "time": [2, 2, 1, 1], "value": [4, 8, 2, 6]})
if id_type != "integer":
categories = [1, 2, 3] if id_type == "unused_category" else [1, 3]
df["id"] = pd.Categorical(df["id"], categories=categories)

kwargs = {}
if data_format == "dict":
data = {"value": df}
kwargs["column_value"] = "value"
elif data_format == "long":
data = df.assign(kind="value")
kwargs.update(column_kind="kind", column_value="value")
else:
data = df
kwargs["column_value"] = "value"

result = extract_features(
data,
column_id="id",
column_sort=column_sort,
default_fc_parameters={"length": None, "sum_values": None},
n_jobs=1,
disable_progressbar=True,
**kwargs,
)
expected = pd.DataFrame(
{"value__length": [2.0, 2.0], "value__sum_values": [6.0, 14.0]},
index=pd.Index([1, 3], dtype=df["id"].dtype),
)
pd.testing.assert_frame_equal(result, expected)
6 changes: 3 additions & 3 deletions tsfresh/feature_extraction/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def __init__(self, df, column_id, column_sort=None, value_columns=None):
_check_nan(df, column_sort)

self.column_sort = column_sort
self.df_grouped = df.groupby(column_id)
self.df_grouped = df.groupby(column_id, observed=True)

super().__init__(df, column_id)

Expand Down Expand Up @@ -319,12 +319,12 @@ def __init__(self, ts_dict, column_id, column_value, column_sort=None):
_check_nan(df, column_sort)

self.grouped_dict = {
key: df.sort_values([column_sort]).groupby(column_id)
key: df.sort_values([column_sort]).groupby(column_id, observed=True)
for key, df in ts_dict.items()
}
else:
self.grouped_dict = {
key: df.groupby(column_id) for key, df in ts_dict.items()
key: df.groupby(column_id, observed=True) for key, df in ts_dict.items()
}

super().__init__(df, column_id)
Expand Down