You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Python] Add a dependency free random array generator for testing #47255
We currently generate random array data with numpy which means we cannot test for cases where numpy is not present. We want a random array generator depending only on pyarrow and Python standard library. We should plug that into _test_table and _test_dataframe test utilities.
See #47199 (review) for more context.
Although if we have a dependency free test data generator we would probably want to use it everywhere. And in that case we probably want to spend the time to connect to the cpp generator we already have in place for cpp testing for Python testing.
One possible solution to expose cpp generator is by extending compute random in https://arrow.apache.org/docs/cpp/compute.html#random-number-generation. Current random only support generate [0, 1) double-precision float numbers. We can extend random to support generate random integers by adding min/max/type arguments.
def random(n, *, initializer='system', options=None, memory_pool=None,
+ min=0.0, max=1.0, type=pyarrow.float64()
"""
Generate numbers in the range [0, 1).
Generated values are uniformly-distributed, double-precision
in range [0, 1). Algorithm and seed can be changed via RandomOptions.
Behaviour change: Current random generate numbers in the range [0, 1.0), but for integer scenario we'd better generate close interval [min, max] just as np.random.randint do. As a result, default number will generate [0, 1.0] instead of [0, 1.0).
For non numerical types(e.g. bool/string), we may choose to not support in random or support without min/max limit. I prefer to not supporting numerical types in random.
Behaviour change: Current random generate numbers in the range [0, 1.0), but for integer scenario we'd better generate close interval [min, max] just as np.random.randint do. As a result, default number will generate [0, 1.0] instead of [0, 1.0).
Numpys random appears to have the same [0, 1.0) behavior so replacing it with pyarrow.compute.random would be ok. Integers could be done with rounding. Minor potential distribution change here would probably not be an issue for our test requirements.
For non numerical types(e.g. bool/string), we may choose to not support in random or support without min/max limit. I prefer to not supporting numerical types in random.
For c++ testing purposes we have a RandomArrayGenerator that would cover our needs here. But we don't expose testing utilities. So perhaps we should create the needed compute kernel (or extend the current one, I'll open an issue) and add required type generators functionality there. See PR for compute.random.
@egolearner I've opened #47288 for the kernel option and we want to wait for the discussion to play out. Meanwhile we could perhaps sketch out a PR using pyarrow.compute.random for numeric types and Python stdlib non-numeric types.
Generating random data for testing (where you generally want data to be "interesting" but you don't need high statistical quality) is not the same thing as providing random generation facilities for production (where you generally want data to have guaranteed statistical properties, such as: uniform/normal/etc., with certain parameters).
So, for the purposes of testing PyArrow, I think we could expose the C++ RandomArrayGenerator in Python.
(but I'm also not sure why requiring NumPy for testing is a problem, can someone elaborate?)
So, for the purposes of testing PyArrow, I think we could expose the C++ RandomArrayGenerator in Python.
What would a good namespace for that be? pyarrow.compute.testing.random_[float,int,str,...]?
(but I'm also not sure why requiring NumPy for testing is a problem, can someone elaborate?)
We are trying to reduce dependency on Pandas and NumPy in testing as we're excluding some tests if they're not present. Currently many tests that work on arrays and tables will be skipped. Perhaps this is not a big issue, but it seems like an easy fix.
We can definitely provide more random generation kernels for other numeric types, but you can also generate float64 and cast to the target type (the exception being higher-precision data types such as decimals).
How about just providing a testing utility that produces data from the uniform random kernel? No C++ changes would be required. I'm not sure if this would make for "interesting" enough binary/string data.
This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.
Activity
egolearner commentedon Aug 6, 2025
take
egolearner commentedon Aug 8, 2025
Following the discussion from #47199 (comment)_
One possible solution to expose cpp generator is by extending compute
randomin https://arrow.apache.org/docs/cpp/compute.html#random-number-generation. Currentrandomonly support generate [0, 1) double-precision float numbers. We can extendrandomto support generate random integers by addingmin/max/typearguments.Behaviour change: Current random generate numbers in the range [0, 1.0), but for integer scenario we'd better generate close interval [min, max] just as
np.random.randintdo. As a result, default number will generate [0, 1.0] instead of [0, 1.0).For non numerical types(e.g. bool/string), we may choose to not support in
randomor support without min/max limit. I prefer to not supporting numerical types inrandom.Any suggestions? @rok @raulcd
rok commentedon Aug 8, 2025
Compute random is a good idea!
Numpys random appears to have the same [0, 1.0) behavior so replacing it with
pyarrow.compute.randomwould be ok. Integers could be done with rounding. Minor potential distribution change here would probably not be an issue for our test requirements.For c++ testing purposes we have a RandomArrayGenerator that would cover our needs here. But we don't expose testing utilities. So perhaps we should create the needed compute kernel (or extend the current one, I'll open an issue) and add required type generators functionality there. See PR for compute.random.
rok commentedon Aug 8, 2025
@egolearner I've opened #47288 for the kernel option and we want to wait for the discussion to play out. Meanwhile we could perhaps sketch out a PR using
pyarrow.compute.randomfor numeric types and Python stdlib non-numeric types.pitrou commentedon Aug 19, 2025
Generating random data for testing (where you generally want data to be "interesting" but you don't need high statistical quality) is not the same thing as providing random generation facilities for production (where you generally want data to have guaranteed statistical properties, such as: uniform/normal/etc., with certain parameters).
So, for the purposes of testing PyArrow, I think we could expose the C++
RandomArrayGeneratorin Python.(but I'm also not sure why requiring NumPy for testing is a problem, can someone elaborate?)
rok commentedon Aug 19, 2025
What would a good namespace for that be? pyarrow.compute.testing.random_[float,int,str,...]?
We are trying to reduce dependency on Pandas and NumPy in testing as we're excluding some tests if they're not present. Currently many tests that work on arrays and tables will be skipped. Perhaps this is not a big issue, but it seems like an easy fix.
pitrou commentedon Aug 19, 2025
pyarrow.testingas compute is not involved.Ok, that sounds reasonable.
rok commentedon Aug 19, 2025
From: #47288 (comment)
How about just providing a testing utility that produces data from the uniform random kernel? No C++ changes would be required. I'm not sure if this would make for "interesting" enough binary/string data.
kou commentedon Aug 20, 2025
If we use
RandomArrayGenerator, we need to depend on GTest. Becausearrow-testingdepends on GTest. It may be a problem.pitrou commentedon Aug 20, 2025
Ah, perhaps we'll need to separate the GTest-specific utilities from the core GTest-agnostic utilities, then. That will be a bit of work...
kou commentedon Aug 20, 2025
#47288 (except binary type?) may be straightforward than separating GTest related parts...
github-actions commentedon Aug 22, 2026
This issue has been marked as stale because it has had no activity in the past 365 days. Please remove the stale label or comment below, or this issue will be closed in 14 days. If this improvement is still desired but has no current owner, please add the 'Status: needs champion' label.