Commit Graph
17 Commits
Author SHA1 Message Date
57f6261893 GH-47861: [Python] reduce memory usage when using to_pandas() with many extension arrays columns (#47860)
### Rationale for this change

See GH-47861. With this change, the extension array variation takes ~192MB of memory instead of 7GB. 

From what I can tell, this is because the `PandasOptions` struct is copied around frequently (for example it seems like there is an `ExtensionWriter` for each extension column and each `ExtensionWriter` has a copy of `PandasOptions` which has a set of all extension columns). I haven't fully traced the PandasOptions structure, but it seems to get copied and modified in some codepaths so I have decided to put the column sets into a `std::shared_ptr` rather than pass around a `shared_ptr<PandasOptions>`.

### What changes are included in this PR?

The `PandasOptions` column sets have been swapped from `std::unordered_set<std::string>` to `std::shared_ptr<const std::unordered_set<std::string>>` and usages have been updated.

### Are these changes tested?

Yes, no regression in the pytests. Also tested memory usage by hand. 

### Are there any user-facing changes?

All changes are internal to the pyarrow C++ binding code. There are no changes to the exposed Python API.

* GitHub Issue: #47861

Lead-authored-by: Will Gulian <williamgulian@gmail.com>
Co-authored-by: Will Gulian <git@will.gl>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
Signed-off-by: Antoine Pitrou <antoine@python.org>
2025-10-28 11:03:26 +01:00
6c9e30b111 GH-46728: [Python] Skip test_gdb.py tests if PyArrow wasn't built debug (#46755)
### Rationale for this change
As mentioned in #46728, if Arrow C++ was built debug, and PyArrow wasn't, test_gdb.py runs tests that fail.

### What changes are included in this PR?
The CMAKE_BUILD_TYPE environment variable is propagated from build into PyArrow, where it's checked to skip unit tests.

### Are these changes tested?
Yes. I have built PyArrow in release, debug, and relwithdebinfo and observed the new behavior. Because CMakeLists.txt was changed, I built PyArrow twice via setup.py and pip install, and checked the new function.

### Are there any user-facing changes?
Devs may skip unit tests that would fail. PyArrow now has build_info() with information about the build type.

* GitHub Issue: #46728

Lead-authored-by: Eric Dinse <293818+dinse@users.noreply.github.com>
Co-authored-by: Antoine Pitrou <antoine@python.org>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
Signed-off-by: Antoine Pitrou <antoine@python.org>
2025-07-15 18:04:37 +02:00
Raúl Cumplido b3e261ddf7 GH-46771: [Python][C++] Implement pa.arange function to generate array sequences (#46778)
### Rationale for this change

When slicing arrays with non-trivial steps we were using `numpy.arange` to generate the indices for take. As numpy is an optional dependency, implementing it via Python caused a performance penalty. Creating a pyarrow function to build our own ranges that mimics Python range or numpy arange is useful for that uses case and might also be useful for other use cases. Currently we only generate `Array[Int64]` we could potentially generate more types.

### What changes are included in this PR?

provide a `pa.arange` function that allows us to generate indices when slicing arrays.

### Are these changes tested?

Yes new tests added.

### Are there any user-facing changes?

No but a new pyarrow.arange function has been added.

* GitHub Issue: #46771

Authored-by: Raúl Cumplido <raulcumplido@gmail.com>
Signed-off-by: Raúl Cumplido <raulcumplido@gmail.com>
2025-06-20 12:33:27 +02:00
Raúl CumplidoandAntoine Pitrou 55628cd5fe GH-46611: [Python][C++] Allow building float16 arrays without numpy (#46618)
### Rationale for this change

When we added Float16 we did not update pyarrow to be able to convert from Python objects to Arrow. Float16 required numpy and it crashed if numpy was not present.

### What changes are included in this PR?

Allow to not require numpy to generate float16 scalars and arrays on pyarrow and do not fail if numpy is not present.

### Are these changes tested?

Yes, new tests have been added

### Are there any user-facing changes?

No changes for old functionality. Users will be allowed to use float16 without requiring to use np.float16 and directly from Python objects

* GitHub Issue: #46611

Lead-authored-by: Raúl Cumplido <raulcumplido@gmail.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
Signed-off-by: Raúl Cumplido <raulcumplido@gmail.com>
2025-06-04 07:54:05 +02:00
Alenka Frim 5c0015d34b GH-43587: [Python] Remove no longer used serialize/deserialize PyArrow C++ code (#45743)
### Rationale for this change
We deprecated serialization functionality in C++ code in https://github.com/apache/arrow/pull/44064. We can now remove it.

### What changes are included in this PR?
Serialization functionality is removed from PyArrow C++ code.

### Are these changes tested?
Tests should pass.

### Are there any user-facing changes?
Serialization functionality is removed from PyArrow C++ code.

* GitHub Issue: #43587

Authored-by: AlenkaF <frim.alenka@gmail.com>
Signed-off-by: AlenkaF <frim.alenka@gmail.com>
2025-03-24 15:53:10 +01:00
Enrico Minack d534e77248 GH-44679: [C++][Python] Fix Flight Timestamp precision, revert workaround from #43537 (#44681)
### What changes are included in this PR?
Make `arrow.flight.Timestamp` nanoseconds precision and OS-independent.

Use the `CTimePoint` for `FlightEndpoint.expiration_time` to have OS-independent nanoseconds precision.

https://github.com/apache/arrow/blob/093655c60783321c786a8e69632f185d37520f4d/python/pyarrow/includes/libarrow_flight.pxd#L133-L139

This reverts workaround for `expiration_time` from  #43537.

### Are these changes tested?
Existing unit tests are adjusted to the new precision.

### Are there any user-facing changes?

**This PR includes breaking changes to public APIs.**

The values of `FlightEndpoint.expiration_time` now have nanoseconds precision on any operating system.

* GitHub Issue: #44679

Authored-by: Enrico Minack <github@enrico.minack.dev>
Signed-off-by: David Li <li.davidm96@gmail.com>
2024-11-13 21:59:24 -05:00
b193c4f701 GH-36954: [Python] Add more FlightInfo / FlightEndpoint attributes (#43537)
### Rationale for this change
The C++ classes `FlightInfo` and `FlightEndpoint` have attributes that are not available via the Python API.

### What changes are included in this PR?
Make the following attributes available in Python:
- `FlightInfo.ordered`
- `FlightInfo.app_metadata`
- `FlightEndpoint.expiration_time`
- `FlightEndpoint.app_metadata`

Also makes existing attributes optional in constructor:
- `FlightInfo.total_records`
- `FlightInfo.total_bytes`

### Are these changes tested?
Existing tests that test existing attributes are extended.

### Are there any user-facing changes?
Yes, changes are backward compatible.
* GitHub Issue: #36954

Lead-authored-by: Enrico Minack <github@enrico.minack.dev>
Co-authored-by: Adam Reeve <adreeve@gmail.com>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Co-authored-by: David Li <li.davidm96@gmail.com>
Signed-off-by: David Li <li.davidm96@gmail.com>
2024-11-07 06:37:23 -05:00
9ab9532a20 GH-25118: [Python] Make NumPy an optional runtime dependency (#41904)
### Rationale for this change

Being able to run pyarrow without requiring numpy.

### What changes are included in this PR?

If numpy is not present we are able to import pyarrow and run functionality.
A new CI job has been created to run some basic tests without numpy.

### Are these changes tested?

Yes via CI.

### Are there any user-facing changes?

Yes, NumPy can be removed from the user installation and pyarrow functionality still works

* GitHub Issue: #25118

Lead-authored-by: Raúl Cumplido <raulcumplido@gmail.com>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
Signed-off-by: Antoine Pitrou <antoine@python.org>
2024-09-02 16:35:26 +02:00
2de8008748 GH-41910: [Python] Add support for Pyodide (#37822)
pyarrow knows about ARROW_ENABLE_THREADING and doesn't use threads if they are not enabled in libarrow.

Split from #37696 

* GitHub Issue: #41910

Lead-authored-by: Joe Marshall <joe.marshall@nottingham.ac.uk>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Co-authored-by: Raúl Cumplido <raulcumplido@gmail.com>
Co-authored-by: Sutou Kouhei <kou@cozmixng.org>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
2024-07-06 06:17:25 +09:00
d6b9051fa0 GH-40066: [Python] Support requested_schema in __arrow_c_stream__() (#40070)
### Rationale for this change

The `requested_schema` portion of the `__arrow_c_stream__()` protocol methods errored in all cases if passed an unequal schema. There was a note about figuring out how to check the cast before doing it and a comment in https://github.com/apache/arrow/issues/40066 about how it should be done lazily. This PR (hopefully) solves both!

### What changes are included in this PR?

- Added `arrow::py::CastingRecordBatchReader`, which wraps a `arrow::RecordBatchReader`, casting each batch as it is pulled.

### Are these changes tested?

Yes.

### Are there any user-facing changes?

Yes: the current approach adds `RecordBatchReader.cast()` as the way to access the casting reader.

* Closes: #40066
* GitHub Issue: #40066

Lead-authored-by: Dewey Dunnington <dewey@fishandwhistle.net>
Co-authored-by: Dewey Dunnington <dewey@voltrondata.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
2024-02-28 14:49:48 +01:00
Antoine Pitrou 96645ebc50 GH-39599: [Python] Avoid leaking references to Numpy dtypes (#39636)
### Rationale for this change

`PyArray_DescrFromScalar` returns a new reference, so we should be careful to decref it when we don't use it anymore.

### Are these changes tested?

No.

### Are there any user-facing changes?

No.
* Closes: #39599

Authored-by: Antoine Pitrou <antoine@python.org>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
2024-01-17 11:26:37 +01:00
Josh Soref 6101d12676 GH-38944: [Python] Fix spelling (#38945)
### Rationale for this change

### What changes are included in this PR?

Spelling fixes to python/

### Are these changes tested?

### Are there any user-facing changes?

* Closes: #38944

Authored-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
2023-12-01 09:30:13 +01:00
Antoine Pitrou d076c69e81 GH-38676: [Python] Fix potential deadlock when CSV reading errors out (#38713)
### Rationale for this change

A deadlock can happen in a C++ destructor in the following case:
* the C++ destructor is called from Python, holding the GIL
* the C++ destructor waits for a threaded task to finish
* the threaded task has invoked some Python code which is waiting to acquire the GIL

### What changes are included in this PR?

To reliably present such a deadlock, introduce `std::shared_ptr` and `std::unique_ptr` wrappers that release the GIL when deallocating the embedded pointer.

### Are these changes tested?

Yes.

### Are there any user-facing changes?

No.
* Closes: #38676

Authored-by: Antoine Pitrou <antoine@python.org>
Signed-off-by: Antoine Pitrou <antoine@python.org>
2023-11-15 16:15:42 +01:00
David LiandAntoine Pitrou b6ab9092af GH-37093: [Python] Add async Flight client with GetFlightInfo (#36986)
### Rationale for this change

Demonstrate how to deal with async Flight from Python.

### What changes are included in this PR?

Add bindings for GetFlightInfo.

### Are these changes tested?

Yes.

### Are there any user-facing changes?

Yes, new APIs.

* Closes: #37093

Lead-authored-by: David Li <li.davidm96@gmail.com>
Co-authored-by: Antoine Pitrou <antoine@python.org>
Signed-off-by: Antoine Pitrou <antoine@python.org>
2023-08-23 19:33:57 +02:00
Dane PitkinandJoris Van den Bossche 4f56aba35b GH-33321: [Python] Support converting to non-nano datetime64 for pandas >= 2.0 (#35656)
Do not coerce temporal types to nanosecond when pandas >= 2.0 is imported, since pandas now supports s/ms/us time units.

This PR adds support for the following Arrow -> Pandas conversions, which previously all defaulted to `datetime64[ns]` or `datetime64[ns, <TZ>]`:
```
date32 -> datetime64[ms]
date64 -> datetime64[ms]
datetime64[s] -> datetime64[s]
datetime64[ms] -> datetime64[ms]
datetime64[us] -> datetime64[us]
datetime64[s, <TZ>] -> datetime64[s, <TZ>]
datetime64[ms, <TZ>] -> datetime64[ms, <TZ>]
datetime64[us, <TZ>] -> datetime64[us, <TZ>]
```
### Rationale for this change

Pandas 2.0 introduces proper support for temporal types.

### Are these changes tested?

Yes. Pytests added and updated.

### Are there any user-facing changes?

Yes, arrow-to-pandas default conversion behavior will change when users have pandas >= 2.0, but a legacy option is exposed to provide backwards compatibility.
* Closes: #33321

Lead-authored-by: Dane Pitkin <dane@voltrondata.com>
Co-authored-by: Dane Pitkin <48041712+danepitkin@users.noreply.github.com>
Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
2023-07-07 16:45:58 +02:00
Michael Lui 1a697abd08 GH-34729: [C++][Python] Enhanced Arrow<->Pandas map/pydict support (#34730)
### Rationale for this change

Explained in issue #34729 

### What changes are included in this PR?

- Add support for list of maps when converting Arrow to Pandas. There doesn't seem to be a strong reason to omit this. Previously it was a hard error as unsupported, due to a bool check.
- Refactor Arrow Map -> Pandas to support two paths: (1) list of tuples, or (2) pydicts
- Add another option in PandasOptions to enable (2), above
- Bugfix in nested pydicts -> Arrow maps. 
- Unit tests

### Are these changes tested?

Unit tests are added in `test_pandas.py`

### Are there any user-facing changes?

- An additional option flag in PandasOptions
- Enable list of maps to Pandas, which was previously disabled
* Closes: #34729

Authored-by: Mike Lui <mikelui@meta.com>
Signed-off-by: Will Jones <willjones127@gmail.com>
2023-04-21 08:18:32 -07:00
Raúl Cumplido b67e3c8ef1 ARROW-16062: [Python] Move libarrow_python include definitions to its own file
This PR moves all the `arrow/python` definitions to its own `libarrow_python.pxd` file as requested on ARROW-16062

Closes #12873 from raulcd/ARROW-16062

Authored-by: Raúl Cumplido <raulcumplido@gmail.com>
Signed-off-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
2022-04-15 17:35:15 +02:00