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

[Python] Allow rename_columns to take a mapping #40644

Closed
judahrand opened this issue Mar 18, 2024 · 1 comment
Closed

[Python] Allow rename_columns to take a mapping #40644

judahrand opened this issue Mar 18, 2024 · 1 comment

Comments

@judahrand
Copy link
Contributor

Describe the enhancement requested

Pandas allows a mapping to be passed like df.rename(columns={'foo': 'bar'}). This is often very useful when you only want to rename a subset of the columns. Currently, rename_columns only accepts a list of all column names.

I propose that rename_columns be extended to accept a Mapping[str, str]. All columns with a name matching a key of the mapping will be renamed to the relevant value. If any key does not correspond to at least one column then a KeyError should be raised.

Example:

>>> import pyarrow as pa
>>> import pandas as pd
>>> df = pd.DataFrame({'n_legs': [2, 4, 5, 100],
...                    'animals': ["Flamingo", "Horse", "Brittle stars", "Centipede"]})
>>> batch = pa.RecordBatch.from_pandas(df)
>>> new_names = {"n_legs": "n", "animals": "name"}
>>> batch.rename_columns(new_names)
pyarrow.RecordBatch
 n: int64
 name: string
 ----
 n: [2,4,5,100]
 name: ["Flamingo","Horse","Brittle stars","Centipede"]

Component(s)

Python

AlenkaF pushed a commit that referenced this issue Apr 15, 2024
…_columns` (#40645)

### Rationale for this change

See #40644 

### What changes are included in this PR?

### Are these changes tested?

Yes.

Tests have been added.

### Are there any user-facing changes?

* GitHub Issue: #40644

Authored-by: Judah Rand <[email protected]>
Signed-off-by: AlenkaF <[email protected]>
@AlenkaF AlenkaF added this to the 17.0.0 milestone Apr 15, 2024
@AlenkaF
Copy link
Member

AlenkaF commented Apr 15, 2024

Issue resolved by pull request 40645
#40645

@AlenkaF AlenkaF closed this as completed Apr 15, 2024
tolleybot pushed a commit to tmct/arrow that referenced this issue May 2, 2024
…rename_columns` (apache#40645)

### Rationale for this change

See apache#40644 

### What changes are included in this PR?

### Are these changes tested?

Yes.

Tests have been added.

### Are there any user-facing changes?

* GitHub Issue: apache#40644

Authored-by: Judah Rand <[email protected]>
Signed-off-by: AlenkaF <[email protected]>
vibhatha pushed a commit to vibhatha/arrow that referenced this issue May 25, 2024
…rename_columns` (apache#40645)

### Rationale for this change

See apache#40644 

### What changes are included in this PR?

### Are these changes tested?

Yes.

Tests have been added.

### Are there any user-facing changes?

* GitHub Issue: apache#40644

Authored-by: Judah Rand <[email protected]>
Signed-off-by: AlenkaF <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants