Skip to content

Accept numpy integers as train_size / test_size in train_test_split - #8442

Open
LeSingh1 wants to merge 1 commit into
huggingface:mainfrom
LeSingh1:fix-tts-numpy-int
Open

Accept numpy integers as train_size / test_size in train_test_split#8442
LeSingh1 wants to merge 1 commit into
huggingface:mainfrom
LeSingh1:fix-tts-numpy-int

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown

Dataset.train_test_split() uses isinstance(..., int) / isinstance(..., float) to tell "a number of rows" from "a fraction", and rejects everything else:

if train_size is not None and not isinstance(train_size, (int, float)):
    raise ValueError(f"Invalid value for train_size: {train_size} of type {type(train_size)}")

numpy floats are subclasses of float, so they already work. numpy integers are not subclasses of int, so they don't:

import numpy as np

ds.train_test_split(test_size=np.float64(0.2))   # works
ds.train_test_split(test_size=np.int64(2))
# ValueError: Invalid value for test_size: 2 of type <class 'numpy.int64'>

The asymmetry is accidental, and the error is confusing because the printed value (2) looks perfectly valid. numpy integers turn up naturally — np.floor(0.2 * len(ds)).astype(int), a value read out of a numpy array, a pandas .nunique(), etc. sklearn.model_selection.train_test_split, which this API is modelled on, accepts them.

This PR converts numpy integers to int before the checks, leaving the int/float semantics untouched.

Added tests/test_arrow_dataset.py::test_train_test_split_with_numpy_integer_sizes, parametrized over int32, int64 and uint8, covering both test_size and train_size. All three fail on main.

`Dataset.train_test_split()` decides between "number of rows" and "fraction"
with `isinstance(test_size, int)` / `isinstance(test_size, float)`, and rejects
anything else:

    ds.train_test_split(test_size=np.int64(2))
    # ValueError: Invalid value for test_size: 2 of type <class 'numpy.int64'>

numpy floats are subclasses of `float` so they already work, but numpy integers
are not subclasses of `int`, which makes the behaviour inconsistent for values
that come out of the same numpy computation.

Convert numpy integers to `int` before the checks.
@LeSingh1
LeSingh1 force-pushed the fix-tts-numpy-int branch from 0bb6a46 to 8ce7e3f Compare August 9, 2026 02:20
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

Successfully merging this pull request may close these issues.

1 participant