Skip to content

Allow dataset row indexing with np.int types (#7423) #7438

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/datasets/formatting/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import numbers
import operator
from collections.abc import Iterable, Mapping, MutableMapping
from functools import partial
Expand Down Expand Up @@ -566,7 +567,7 @@ def _check_valid_index_key(key: Union[int, slice, range, Iterable], size: int) -


def key_to_query_type(key: Union[int, slice, range, str, Iterable]) -> str:
if isinstance(key, int):
if isinstance(key, numbers.Integral):
return "row"
elif isinstance(key, str):
return "column"
Expand Down
10 changes: 10 additions & 0 deletions tests/test_arrow_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -4494,12 +4494,22 @@ async def f(batch):
assert len(out) == 1


def test_dataset_getitem_int_np_equivalence():
ds = Dataset.from_dict({"a": [0, 1, 2, 3]})

assert ds[1] == ds[np.int64(1)]


def test_dataset_getitem_raises():
ds = Dataset.from_dict({"a": [0, 1, 2, 3]})
with pytest.raises(TypeError):
ds[False]
with pytest.raises(TypeError):
ds._getitem(True)
with pytest.raises(TypeError):
ds[np.bool_(True)]
with pytest.raises(TypeError):
ds[1.0]


def test_categorical_dataset(tmpdir):
Expand Down