Skip to content
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

GH-41815: [Python] add use_threads option to feather.write_feather() #41820

Closed
wants to merge 4 commits into from
Closed
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 python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ if(UNIX)
# loaded properly
if(APPLE)
set(CMAKE_INSTALL_NAME_DIR "@rpath")
set(CMAKE_INSTALL_RPATH "@loader_path/")
set(CMAKE_INSTALL_RPATH "@loader_path")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
else()
set(CMAKE_INSTALL_RPATH "\$ORIGIN")
endif()
Expand Down
16 changes: 14 additions & 2 deletions python/pyarrow/feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def check_chunked_overflow(name, col):


def write_feather(df, dest, compression=None, compression_level=None,
chunksize=None, version=2):
chunksize=None, version=2, use_threads=True):
"""
Write a pandas.DataFrame to Feather format.

Expand All @@ -136,6 +136,9 @@ def write_feather(df, dest, compression=None, compression_level=None,
version : int, default 2
Feather file version. Version 2 is the current. Version 1 is the more
limited legacy format
use_threads : bool, default True
Whether to parallelize conversion of a pandas.DataFrame using
multiple threads.
"""
if _pandas_api.have_pandas:
if (_pandas_api.has_sparse and
Expand All @@ -153,7 +156,16 @@ def write_feather(df, dest, compression=None, compression_level=None,
else:
raise ValueError("Version value should either be 1 or 2")

table = Table.from_pandas(df, preserve_index=preserve_index)
# Table.from_pandas will heuristically choose the number of
# threads to use if nthreads == None, but won't use threads if
# nthreads == 1.
if use_threads:
nthreads = None
else:
nthreads = 1

table = Table.from_pandas(df, preserve_index=preserve_index,
nthreads=nthreads)

if version == 1:
# Version 1 does not chunking
Expand Down
8 changes: 7 additions & 1 deletion python/pyarrow/tests/test_feather.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def test_use_threads(version):
columns = ['col_' + str(i) for i in range(10)]
table = pa.Table.from_arrays(values, columns)

write_feather(table, path, version=version)
write_feather(table, path, version=version, use_threads=True)

result = read_feather(path)
assert_frame_equal(table.to_pandas(), result)
Expand All @@ -225,6 +225,12 @@ def test_use_threads(version):
result = read_table(path, use_threads=False)
assert result.equals(table)

# Test write_feather in serial
write_feather(table, path, version=version, use_threads=False)

result = read_feather(path)
assert_frame_equal(table.to_pandas(), result)


@pytest.mark.pandas
def test_float_nulls(version):
Expand Down
Loading