Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indexing last interval in an IntervalSet using -1 throws an error #407

Closed
sjvenditto opened this issue Jan 30, 2025 · 2 comments
Closed

Comments

@sjvenditto
Copy link
Collaborator

The following code snippet throws an error:

>>> ep = nap.IntervalSet(start=[0,2,4], end=[1,3,5])
>>> ep[-1]

The error trace:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[6], line 2
      1 ep = nap.IntervalSet(start=[0,2,4], end=[1,3,5])
----> 2 ep[-1]

File [/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/interval_set.py:438](http://localhost:8888/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/interval_set.py#line=437), in IntervalSet.__getitem__(self, key)
    435 elif isinstance(key, Number):
    436     # self[Number]
    437     output = self.values.__getitem__(key)
--> 438     return IntervalSet(start=output[0], end=output[1], metadata=metadata)
    439 elif isinstance(key, (slice, list, np.ndarray, pd.Series)):
    440     # self[array_like]
    441     output = self.values.__getitem__(key)

File [/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/interval_set.py:298](http://localhost:8888/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/interval_set.py#line=297), in IntervalSet.__init__(self, start, end, time_units, metadata)
    296 self._initialized = True
    297 if drop_meta is False:
--> 298     self.set_info(metadata)

File [/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/interval_set.py:1124](http://localhost:8888/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/interval_set.py#line=1123), in IntervalSet.set_info(self, metadata, **kwargs)
   1050 @add_meta_docstring("set_info")
   1051 def set_info(self, metadata=None, **kwargs):
   1052     """
   1053     Examples
   1054     --------
   (...)
   1122     shape: (3, 2), time unit: sec.
   1123     """
-> 1124     _MetadataMixin.set_info(self, metadata, **kwargs)

File [/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/metadata_class.py:217](http://localhost:8888/opt/homebrew/Caskroom/miniforge/base/envs/ccn-jan25/lib/python3.11/site-packages/pynapple/core/metadata_class.py#line=216), in _MetadataMixin.set_info(self, metadata, **kwargs)
    215         self._metadata[metadata.columns] = metadata
    216     else:
--> 217         raise ValueError("Metadata index does not match")
    218 elif isinstance(metadata, dict):
    219     # merge metadata with kwargs to use checks below
    220     kwargs = {**metadata, **kwargs}

ValueError: Metadata index does not match
@sjvenditto
Copy link
Collaborator Author

The problem is that integer indexing of metadata uses the pandas DataFrame .loc, which assumes the value is an existing index, which -1 is not. We can't switch to using .iloc directly because then it would break metadata indexing for TsGroup objects. We would have to treat integer/integer array indexing of metadata differently for IntervalSets and TsGroups

@gviejo
Copy link
Contributor

gviejo commented Feb 6, 2025

Fix in #409

@gviejo gviejo closed this as completed Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants