Skip to content

Add nw_df.to(backend, index). A generic method from Narwhals to Pandas, Polars, and Arrow with Index Handling Options #2056

@authierj

Description

@authierj

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?

We are trying to narwhalify darts.

We already implemented the method TimeSeries.to_dataframe() with narwhals. The next step would be to go from TimeSeries.pd_dataframe() to a more generic TimeSeries.to_dataframe() enabling the users to choose their preferred backend. The issue is described here

We store our TImeSeries data in an xarray, where the time information is stored as a pd.DatetimeIndex. Right now we are going from the xarray to a pandas dataframe and this is trivial.
However, going dataframe agnostic, the handling of the DatetimeIndex becomes problematic for two reasons:

  1. Initiating the narwhals dataframe with .from_numpy() we loose all the information from the DatetimeIndex (like the frequency) except the date itself
  2. Initating the narwhals dataframe with from_native(), the index information will be lost if converted to Polars.

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

to solve 2) we propose the following:

A generic method nw_df.to(backend, keep_index)
backend = the dataframe library to return the dataframe to (Pandas, Polars, Arrows)
keep_index = [True, False] whether the index contains important information and should therefore be reset as in Pandas before being converted to Polars or Arrows.

Suggest a solution if possible.

A wrapper around to_pandas to_polars and to_arrows and a smart way to handle the index.

If you have tried alternatives, please describe them below.

No response

Additional information that may help us understand your needs.

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions