Skip to content

[Python] Add a dependency free random array generator for testing #47255

Open

Description

@rok

Describe the enhancement requested

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.

Component(s)

C++, Python

Activity

  1. egolearner commented on Aug 6, 2025

    @egolearner
    Contributor

    take

  2. egolearner commented on Aug 8, 2025

    @egolearner
    Contributor

    Following the discussion from #47199 (comment)_

    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.

    Any suggestions? @rok @raulcd

  3. rok commented on Aug 8, 2025

    @rok
    MemberAuthor

    Compute random is a good idea!

    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.

    Any suggestions? @rok @raulcd

    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.

  4. rok commented on Aug 8, 2025

    @rok
    MemberAuthor

    @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.

  5. pitrou commented on Aug 19, 2025

    @pitrou
    Member

    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?)

  6. rok commented on Aug 19, 2025

    @rok
    MemberAuthor

    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.

  7. pitrou commented on Aug 19, 2025

    @pitrou
    Member

    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,...]?

    pyarrow.testing as compute is not involved.

    (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.

    Ok, that sounds reasonable.

  8. rok commented on Aug 19, 2025

    @rok
    MemberAuthor

    From: #47288 (comment)

    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.

  9. kou commented on Aug 20, 2025

    @kou
    Member

    If we use RandomArrayGenerator, we need to depend on GTest. Because arrow-testing depends on GTest. It may be a problem.

  10. pitrou commented on Aug 20, 2025

    @pitrou
    Member

    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...

  11. kou commented on Aug 20, 2025

    @kou
    Member

    #47288 (except binary type?) may be straightforward than separating GTest related parts...

  12. github-actions commented on Aug 22, 2026

    @github-actions

    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.

  13. added
    Status: stale-warning
    on Aug 22, 2026
  14. reopened this on Nov 13, 2026
  15. removed
    Status: stale-warning
    on Nov 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions