-
Notifications
You must be signed in to change notification settings - Fork 54
Open
Labels
API extensionAdds new functions or objects to the API.Adds new functions or objects to the API.Needs DiscussionNeeds further discussion.Needs further discussion.RFCRequest for comments. Feature requests and proposed changes.Request for comments. Feature requests and proposed changes.
Description
def item(self) -> Scalar:
"""If array contains exactly one element, retun it as a scalar, else raises ValueError."""Examples:
numpy.ndarray.itemtorch.Tensor.itempandas.Series.itempandas.Index.itempolars.Series.itemxarray.DataArray.item
Demo:
import pytest
import xarray as xr
import pandas as pd
import polars as pl
import numpy as np
@pytest.mark.parametrize("data", [[], [1, 2, 3]])
@pytest.mark.parametrize(
"array_type", [torch.tensor, np.array, pd.Series, pd.Index, pl.Series, xr.DataArray]
)
def test_item_valueerror(data, array_type):
array = array_type(data)
with pytest.raises(ValueError):
array.item()
@pytest.mark.parametrize(
"array_type", [torch.tensor, np.array, pd.Series, pd.Index, pl.Series, xr.DataArray]
)
def test_item(array_type):
array = array_type([1])
array.item()Currently, only torch fails, because it raises RuntimeError instead of ValueError.
Metadata
Metadata
Assignees
Labels
API extensionAdds new functions or objects to the API.Adds new functions or objects to the API.Needs DiscussionNeeds further discussion.Needs further discussion.RFCRequest for comments. Feature requests and proposed changes.Request for comments. Feature requests and proposed changes.