pynapple.TsdFrame.set_info#

TsdFrame.set_info(metadata=None, **kwargs)[source]#

Add metadata information about the object. Metadata are saved as a pandas.DataFrame.

If the metadata name does not contain special nor overlaps with class attributes, it can also be set using attribute assignment.

If the metadata name does not overlap with class-reserved keys, it can also be set using key assignment.

Metadata entries (excluding “rate” for TsGroup) are mutable and can be overwritten.

Parameters:
  • metadata (pandas.DataFrame or dict or pandas.Series, optional) –

    Object containing metadata information, where metadata names are extracted from column names (pandas.DataFrame), key names (dict), or index (pandas.DataFrame).

    • If a pandas.DataFrame is passed, the index must match the metadata index.

    • If a dictionary is passed, the length of each value must match the metadata index length.

    • A pandas.Series can only be passed if the object has a single interval.

  • **kwargs (optional) – Key-word arguments for setting metadata. Values can be either pandas.Series, numpy.ndarray, list or tuple, and must have the same length as the metadata index. If pandas.Series, the index must match the metadata index. If the object only has one index, non-iterable values are also accepted.

Raises:
  • ValueError

    • If metadata index does not match input index (pandas.DataFrame, pandas.Series) - If input array length does not match metadata length (numpy.ndarray, list, tuple)

  • RuntimeError – If the metadata argument is passed as a pandas.Series (for more than one metadata index), numpy.ndarray, list or tuple.

  • TypeError – If key-word arguments are not of type pandas.Series, tuple, list, or numpy.ndarray and cannot be set.

Examples

>>> import pynapple as nap
>>> import numpy as np
>>> tsdframe = nap.TsdFrame(t=np.arange(5), d=np.ones((5, 3)), columns=["a", "b", "c"])

To add metadata with a pandas.DataFrame:

>>> import pandas as pd
>>> metadata = pd.DataFrame(index=tsdframe.columns, data=["red", "blue", "green"], columns=["color"])
>>> tsdframe.set_info(metadata)
>>> tsdframe
Time (s)    a         b         c
----------  --------  --------  --------
0.0         1.0       1.0       1.0
1.0         1.0       1.0       1.0
2.0         1.0       1.0       1.0
3.0         1.0       1.0       1.0
4.0         1.0       1.0       1.0
Metadata
--------    --------  --------  --------
color       red       blue      green

dtype: float64, shape: (5, 3)

To add metadata with a dictionary:

>>> metadata = {"xpos": [10, 20, 30]}
>>> tsdframe.set_info(metadata)
>>> tsdframe
Time (s)    a         b         c
----------  --------  --------  --------
0.0         1.0       1.0       1.0
1.0         1.0       1.0       1.0
2.0         1.0       1.0       1.0
3.0         1.0       1.0       1.0
4.0         1.0       1.0       1.0
Metadata
--------    --------  --------  --------
color       red       blue      green
xpos        10        20        30

dtype: float64, shape: (5, 3)

To add metadata with a keyword arument (pd.Series, numpy.ndarray, list or tuple):

>>> ypos = pd.Series(index=tsdframe.columns, data = [10, 10, 10])
>>> tsdframe.set_info(ypos=ypos)
>>> tsdframe
Time (s)    a         b         c
----------  --------  --------  --------
0.0         1.0       1.0       1.0
1.0         1.0       1.0       1.0
2.0         1.0       1.0       1.0
3.0         1.0       1.0       1.0
4.0         1.0       1.0       1.0
Metadata
--------    --------  --------  --------
color       red       blue      green
xpos        10        20        30
ypos        10        10        10

dtype: float64, shape: (5, 3)

To add metadata as an attribute:

>>> tsdframe.label = ["a", "b", "c"]
>>> tsdframe
Time (s)    a         b         c
----------  --------  --------  --------
0.0         1.0       1.0       1.0
1.0         1.0       1.0       1.0
2.0         1.0       1.0       1.0
3.0         1.0       1.0       1.0
4.0         1.0       1.0       1.0
Metadata
--------    --------  --------  --------
color       red       blue      green
xpos        10        20        30
ypos        10        10        10
label       a         b         c

dtype: float64, shape: (5, 3)

To add metadata as a key:

>>> tsdframe["region"] = ["M1", "M1", "M2"]
>>> tsdframe
Time (s)    a         b         c
----------  --------  --------  --------
0.0         1.0       1.0       1.0
1.0         1.0       1.0       1.0
2.0         1.0       1.0       1.0
3.0         1.0       1.0       1.0
4.0         1.0       1.0       1.0
Metadata
--------    --------  --------  --------
color       red       blue      green
xpos        10        20        30
ypos        10        10        10
label       a         b         c
region      M1        M1        M2

dtype: float64, shape: (5, 3)

Metadata can be overwritten:

>>> tsdframe.set_info(label=["x", "y", "z"])
>>> tsdframe
Time (s)    a         b         c
----------  --------  --------  --------
0.0         1.0       1.0       1.0
1.0         1.0       1.0       1.0
2.0         1.0       1.0       1.0
3.0         1.0       1.0       1.0
4.0         1.0       1.0       1.0
Metadata
--------    --------  --------  --------
color       red       blue      green
xpos        10        20        30
ypos        10        10        10
label       x         y         z
region      M1        M1        M2

dtype: float64, shape: (5, 3)