Skip to content

Commit

Permalink
code review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sjvenditto committed Mar 4, 2025
1 parent b061f5c commit ed2b49a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
8 changes: 5 additions & 3 deletions doc/user_guide/03_metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,14 @@ print(tsgroup.coords)
## Using metadata to slice objects
Metadata can be used to slice or filter objects based on metadata values.
```{code-cell} ipython3
print(tsgroup[tsgroup.label == "good"])
print(tsgroup[tsgroup.label == "A"])
```

## `groupby`: Using metadata to group objects
Similar to pandas, metadata can be used to group objects based on one or more metadata columns using the object method `groupby`, where the first argument is the metadata columns name(s) to group by. This function returns a dictionary with keys corresponding to unique groups and values corresponding to object indices belonging to each group.
```{code-cell} ipython3
tsgroup.groupby("region")
print(tsgroup,"\n")
print(tsgroup.groupby("region"))
```

Grouping by multiple metadata columns should be passed as a list.
Expand All @@ -215,7 +216,8 @@ tsgroup.groupby("region", get_group="hpc")
## `groupby_apply`: Applying functions to object groups
The `groupby_apply` object method allows a specific function to be applied to object groups. The first argument, same as `groupby`, is the metadata column(s) used to group the object. The second argument is the function to apply to each group. If only these two arguments are supplied, it is assumed that the grouped object is the first and only input to the applied function. This function returns a dictionary, where keys correspond to each unique group, and values correspond to the function output on each group.
```{code-cell} ipython3
tsdframe.groupby_apply("label", np.mean)
print(tsdframe,"\n")
print(tsdframe.groupby_apply("label", np.mean))
```

If the applied function requires additional inputs, these can be passed as additional keyword arguments into `groupby_apply`.
Expand Down
8 changes: 4 additions & 4 deletions pynapple/core/metadata_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,12 @@ def get_info(self, key):

def groupby(self, by, get_group=None):
"""
Group pynapple object by metadata column(s).
Group pynapple object by metadata name(s).
Parameters
----------
by : str or list of str
Metadata column name(s) to group by.
Metadata name(s) to group by.
get_group : dictionary key, optional
Name of the group to return.
Expand All @@ -346,7 +346,7 @@ def groupby(self, by, get_group=None):
Raises
------
ValueError
If metadata column does not exist.
If metadata name does not exist.
"""
if isinstance(by, str) and by not in self.metadata_columns:
raise ValueError(
Expand Down Expand Up @@ -383,7 +383,7 @@ def groupby_apply(self, by, func, input_key=None, **func_kwargs):
Parameters
----------
by : str or list of str
Metadata column name(s) to group by.
Metadata name(s) to group by.
func : function
Function to apply to each group.
input_key : str or None, optional
Expand Down
31 changes: 17 additions & 14 deletions pynapple/core/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1487,31 +1487,31 @@ def get_info(self, key):
l2 x x y
dtype: float64, shape: (5, 3)
To access a single metadata column:
To access a single metadata row (transposed to column):
>>> tsdframe.get_info("l1")
0 1
1 2
2 3
Name: l1, dtype: int64
To access multiple metadata columns:
To access multiple metadata rows (transposed to columns):
>>> tsdframe.get_info(["l1", "l2"])
l1 l2
0 1 x
1 2 x
2 3 y
To access metadata of a single column:
To access metadata of a single column (transposed to row):
>>> tsdframe.get_info(0)
rate 0.667223
l1 1
l2 x
Name: 0, dtype: object
To access metadata of multiple columns:
To access metadata of multiple columns (transposed to rows):
>>> tsdframe.get_info([0, 1])
rate l1 l2
Expand Down Expand Up @@ -1573,12 +1573,12 @@ def groupby(self, by, get_group=None):
<BLANKLINE>
dtype: float64, shape: (5, 3)
Grouping by a single column:
Grouping by a single row:
>>> tsdframe.groupby("l2")
{'x': [0, 1], 'y': [2]}
Grouping by multiple columns:
Grouping by multiple rows:
>>> tsdframe.groupby(["l1","l2"])
{(1, 'x'): [0], (2, 'x'): [1], (2, 'y'): [2]}
Expand All @@ -1604,16 +1604,19 @@ def groupby(self, by, get_group=None):
Filtering to a specific group using the get_group argument:
>>> tsdframe.groupby("l2", get_group="x")
Time (s) 0 1 2
---------- -------- -------- --------
0.0 1.0 1.0 1.0
1.0 1.0 1.0 1.0
Time (s) 0 1
---------- -------- --------
0.0 1.0 1.0
1.0 1.0 1.0
2.0 1.0 1.0
3.0 1.0 1.0
4.0 1.0 1.0
Metadata
-------- -------- -------- --------
l1 1 2 2
l2 x x y
-------- -------- --------
l1 1 2
l2 x x
<BLANKLINE>
dtype: float64, shape: (2, 3)
dtype: float64, shape: (5, 2)
"""
return _MetadataMixin.groupby(self, by, get_group)

Expand Down

0 comments on commit ed2b49a

Please sign in to comment.