narwhals-dev / narwhals

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

feat: `nw.from_dict`? #572

Closed MarcoGorelli closed 1 month ago

MarcoGorelli commented 1 month ago

In formulaic they do something like this:

I think we need a way to do the same here. For example:

import numpy as np
import pandas as pd
import narwhals as nw
df = pd.DataFrame({'b': [1, 2]})
nw_df = nw.from_native(df, eager_only=True)

other = nw.from_dict({'a': [1,2,3]}, module=nw.get_native_namespace(nw_df))

So like this, you pass nw_df.__native_namespace__() as an argument in the from_dict call, and Narwhals knows to create a nw.DataFrame backed by the right object (here, a pandas dataframe)

I dunno. We don't have the ability to just create dicts from nothing, so we need to slightly depart from the Polars signature, although the top-level Narwhals functions already depart from Polars anyway

anyone got a better API in mind?


one alternative which comes to mind:

other = nw.__compliant_namespace__().from_dict({'a': [1,2,3]})

which doesn't exactly thrill me

FBruzzesi commented 1 month ago

I like this the first one more:

other = nw.from_dict({'a': [1,2,3]}, module=nw.get_native_namespace(nw_df))

as we can implement nw.DataFrame.from_dict ourselves, while

other = nw.__compliant_namespace__().from_dict({'a': [1,2,3]})

would require that the native namespace has from_dict function which is not necessarily true (arrow has pa.Table.from_pydict).