Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
44 changes: 44 additions & 0 deletions python/pyarrow/_dataset.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,10 @@ cdef class Dataset(_Weakrefable):
n_legs: [[2,4,4,100]]
animal: [["Parrot","Dog","Horse","Centipede"]]
"""
# Apply column projection from rename_columns() if present
if columns is None and 'columns' in self._scan_options:
columns = self._scan_options['columns']

return Scanner.from_dataset(
self,
columns=columns,
Expand Down Expand Up @@ -990,6 +994,46 @@ cdef class Dataset(_Weakrefable):
right_dataset, right_on, right_by,
tolerance, output_type=InMemoryDataset)

def rename_columns(self, names):
"""
Apply logical column renaming on the Dataset.

The rename is applied lazily when data is scanned. Column names in the
files are not changed; the rename is a logical transformation applied
during reads.

Parameters
----------
names : list, tuple, or dict
If a list or tuple, the new names for all columns (must match the
number of columns). If a dict, maps old column names to new names.

Returns
-------
Dataset
The existing dataset with column projection applied.
"""
import pyarrow.dataset as ds

schema = self.schema

if isinstance(names, (list, tuple)):
if len(names) != len(schema):
raise ValueError(
f"Expected {len(schema)} names, got {len(names)}")
name_mapping = {schema.field(i).name: names[i]
for i in range(len(names))}
elif isinstance(names, dict):
name_mapping = names
else:
raise TypeError(f"names must be list, tuple, or dict, not {type(names)!r}")

projection = {new_name: ds.field(old_name)
for old_name, new_name in name_mapping.items()}

self._scan_options['columns'] = projection

return self

cdef class InMemoryDataset(Dataset):
"""
Expand Down
29 changes: 29 additions & 0 deletions python/pyarrow/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -5932,3 +5932,32 @@ def test_scanner_from_substrait(dataset):
filter=ps.BoundExpressions.from_substrait(filtering)
).to_table()
assert result.to_pydict() == {'str': ['4', '4']}


@pytest.mark.parametrize("names", [
["new-index", "new-color"],
("new-index", "new-color"),
{"index": "new-index", "color": "new-color"}
]
)
def test_rename_columns(names):
original_schema = pa.schema([
pa.field('index', pa.int64()),
pa.field('color', pa.string()),
]
)

dataset = ds.InMemoryDataset(
pa.RecordBatch.from_pylist(
[{"index": 1, "color": "green"}, {"index": 2, "color": "blue"}]),
schema=original_schema
)

dataset.rename_columns(names)

expected_schema = pa.schema([
pa.field("new-index", pa.int64()),
pa.field("new-color", pa.string())
])

assert dataset.to_table().schema.equals(expected_schema)
Loading