Skip to content

fix: Preserve pandas column name attribute #2363

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

Open
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

FBruzzesi
Copy link
Member

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

@@ -110,7 +110,7 @@ def collect(
from narwhals._pandas_like.dataframe import PandasLikeDataFrame

return PandasLikeDataFrame(
result,
result.rename_axis(columns=self.native.columns.name),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be one step ahead of dask#11874

Comment on lines -491 to +492
backend_version=parse_version(pd),
implementation=Implementation.PANDAS,
backend_version=parse_version(pd),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mental sanity for symmetry with other pandas-like and order of the spec πŸ˜…


df = nw.from_native(df_native)

result = df.with_columns(b=nw.col("a") + 1, c=nw.col("a") * 2).select("c", "b")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to concatenate two methods here, mostly for the sake of it.
Might be worth reparametrizing it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dask select and with_columns have no issues since we use .assign method for both!

@FBruzzesi FBruzzesi added the fix label Apr 8, 2025
@FBruzzesi FBruzzesi added the pandas-like Issue is related to pandas-like backend label Apr 9, 2025
@MarcoGorelli
Copy link
Member

thanks! i'll do a little test to check there's no accidental / surprise overhead

@FBruzzesi
Copy link
Member Author

FBruzzesi commented Apr 10, 2025

@MarcoGorelli I tried a simple script to check rename_axis overhead (not in narwhals), and it seems to be a pretty low overhead operation:

import timeit
from pprint import pprint

import numpy as np
import pandas as pd


def benchmark_rename_axis_overhead(n_rows: int, n_cols: int, repetitions: int = 1000) -> float:
    df = pd.DataFrame(np.random.randn(n_rows, n_cols))

    return timeit.timeit(
        lambda: df.rename_axis(columns="new_name", copy=False),
        number=repetitions
    ) / repetitions


TOTAL_SIZE = 10_000_000

results = {
    (int(TOTAL_SIZE/10**i), 10**i): benchmark_rename_axis_overhead(n_rows=int(TOTAL_SIZE/10**i), n_cols=10**i)
    for i in range(1, 6)
}

pprint(results)
{(100, 100000): 1.2109115000015436e-05,
 (1000, 10000): 1.679945200001498e-05,
 (10000, 1000): 1.2669480999988991e-05,
 (100000, 100): 1.331965500000365e-05,
 (1000000, 10): 2.9313745999985485e-05}

Let me know what your thoughts are and if we need to check something more extensively

@FBruzzesi FBruzzesi marked this pull request as draft April 17, 2025 18:06
@FBruzzesi
Copy link
Member Author

FBruzzesi commented Apr 17, 2025

Messed up the merge πŸ₯²
Fixed

@FBruzzesi FBruzzesi marked this pull request as ready for review April 17, 2025 18:10
@MarcoGorelli
Copy link
Member

thanks!

Let me know what your thoughts are and if we need to check something more extensively

i'm still slightly concerned about there being extra copies in old versions, i'll take another look

@FBruzzesi
Copy link
Member Author

thanks!

Let me know what your thoughts are and if we need to check something more extensively

i'm still slightly concerned about there being extra copies in old versions, i'll take another look

No rush! I keep merging main to avoid having to deal with a large conflicting diff if that happens to be the case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix pandas-like Issue is related to pandas-like backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: select and with_columns don't always preserve column index name for pandas-like
3 participants