pynapple.TsGroup.groupby#
- TsGroup.groupby(by, get_group=None)[source]#
Group pynapple object by metadata name(s).
- Parameters:
by (str or list of str) – Metadata name(s) to group by.
get_group (dictionary key, optional) – Name of the group to return.
- Returns:
Dictionary of object indices (dictionary values) corresponding to each group (dictionary keys), or pynapple object corresponding to ‘get_group’ if it has been supplied.
- Return type:
dict or pynapple object
- Raises:
ValueError – If metadata name does not exist.
Examples
>>> import pynapple as nap >>> import numpy as np >>> tmp = {0:nap.Ts(t=np.arange(0,40), time_units='s'), ... 1:nap.Ts(t=np.arange(0,40,0.5), time_units='s'), ... 2:nap.Ts(t=np.arange(0,40,0.25), time_units='s'), ... } >>> metadata = {"l1": [1, 2, 2], "l2": ["x", "x", "y"]} >>> tsgroup = nap.TsGroup(tmp,metadata=metadata) >>> print(tsgroup) Index rate l1 l2 ------- ------- ---- ---- 0 1.00629 1 x 1 2.01258 2 x 2 4.02516 2 y
Grouping by a single column:
>>> tsgroup.groupby("l2") {'x': [0, 1], 'y': [2]}
Grouping by multiple columns:
>>> tsgroup.groupby(["l1","l2"]) {(1, 'x'): [0], (2, 'x'): [1], (2, 'y'): [2]}
Filtering to a specific group using the output dictionary:
>>> groups = tsgroup.groupby("l2") >>> tsgroup[groups["x"]] Index rate l1 l2 ------- ------- ---- ---- 1 1.00503 1 x 2 2.01005 2 x
Filtering to a specific group using the get_group argument:
>>> ep.groupby("l2", get_group="x") Index rate l1 l2 ------- ------- ---- ---- 1 1.00503 1 x 2 2.01005 2 x