Skip to content

temporian.EventSet.select_index_values #

select_index_values(
    keys: Optional[IndexKeyList] = None,
    *,
    number: Optional[int] = None,
    fraction: Optional[float] = None
) -> EventSetOrNode

Selects a subset of index values from an EventSet.

Exactly one of keys, number, or fraction should be provided.

If number or fraction is specified, the index values are selected randomly.

If fraction is specified and fraction * len(index keys) doesn't result in an integer, the number of index values selected is rounded down.

If used in compiled or graph mode, the specified keys are compiled as-is along with the operator, which means that they must be available when loading and running the graph on new data.

Example with keys with a single index and a single key:

>>> a = tp.event_set(
...     timestamps=[0, 1, 2, 3],
...     features={
...         "f": [10, 20, 30, 40],
...         "x": ["A", "B", "A", "B"],
...     },
...     indexes=["x"],
... )
>>> b = a.select_index_values("A")
>>> b
indexes: [('x', str_)]
features: [('f', int64)]
events:
    x=b'A' (2 events):
        timestamps: [0. 2.]
        'f': [10 30]
...

Example with keys with multiple indexes and keys:

>>> a = tp.event_set(
...     timestamps=[0, 1, 2, 3],
...     features={
...         "f": [10, 20, 30, 40],
...         "x": [1, 1, 2, 2],
...         "y": ["A", "B", "A", "B"],
...     },
...     indexes=["x", "y"],
... )
>>> b = a.select_index_values([(1, "A"), (2, "B")])
>>> b
indexes: [('x', int64), ('y', str_)]
features: [('f', int64)]
events:
    x=1 y=b'A' (1 events):
        timestamps: [0.]
        'f': [10]
    x=2 y=b'B' (1 events):
        timestamps: [3.]
        'f': [40]
...

Example with number:

>>> import random
>>> random.seed(0)

>>> a = tp.event_set(
...     timestamps=[0, 1, 2, 3],
...     features={
...         "f": [10, 20, 30, 40],
...         "x": [1, 1, 2, 2],
...         "y": ["A", "B", "A", "B"],
...     },
...     indexes=["x", "y"],
... )
>>> b = a.select_index_values(number=2)
>>> b
indexes: [('x', int64), ('y', str_)]
features: [('f', int64)]
events:
    x=1 y=b'A' (1 events):
        timestamps: [0.]
        'f': [10]
    x=2 y=b'A' (1 events):
        timestamps: [2.]
        'f': [30]
...

Example with fraction:

>>> import random
>>> random.seed(0)

>>> a = tp.event_set(
...     timestamps=[0, 1, 2, 3],
...     features={
...         "f": [10, 20, 30, 40],
...         "x": [1, 1, 2, 2],
...         "y": ["A", "B", "A", "B"],
...     },
...     indexes=["x", "y"],
... )
>>> b = a.select_index_values(fraction=0.75)
>>> b
indexes: [('x', int64), ('y', str_)]
features: [('f', int64)]
events:
    x=1 y=b'A' (1 events):
        timestamps: [0.]
        'f': [10]
    x=2 y=b'A' (1 events):
        timestamps: [2.]
        'f': [30]
...

Parameters:

Name Type Description Default
keys Optional[IndexKeyList]

index key or list of index keys to select from the EventSet.

None
number Optional[int]

number of index values to select. If number is greater than the number of index values, all the index values are selected.

None
fraction Optional[float]

fraction of index values to select, expressed as a float between 0 and 1.

None

Returns:

Type Description
EventSetOrNode

EventSet with a subset of the index values.