narwhals-dev / narwhals

Lightweight and extensible compatibility layer between dataframe libraries!
https://narwhals-dev.github.io/narwhals/
MIT License
592 stars 89 forks source link

[Enh]: adding nth() to dataframe #1028

Closed Cheukting closed 1 month ago

Cheukting commented 1 month ago

We would like to learn about your use case. For example, if this feature is needed to adopt Narwhals in an open source project, could you please enter the link to it below?

Like nth() in polars, it would be a nice to have as an alternative to select()

Please describe the purpose of the new feature or describe the problem to solve.

To provide more options to select columns

Suggest a solution if possible.

@Cheukting is going to investigate

If you have tried alternatives, please describe them below.

No response

Additional information that may help us understand your needs.

No response

Cheukting commented 1 month ago

@MarcoGorelli In PyArrow, select() also works on indexes. In Narwhals do you want nth() to be used for indexing columns (i.e. behave like Polars), or do you want select() to also work on indexes (i.e. behave like PyArrow)?

MarcoGorelli commented 1 month ago

thanks @Cheukting! I think it should mirror the Polars behaviour, e.g.

@nw.narwhalify
def func(df):
    return df.select(nw.nth(1))

would work consistently for pd.DataFrame / pl.DataFrame / pa.Table / etc. inputs