narwhals-dev / narwhals

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

enh: create dataframe from 2D numpy array and column names #1434

Open MarcoGorelli opened 3 days ago

MarcoGorelli commented 3 days ago

For classical numpy dtypes in pandas, this should be more efficient than forcing slices + dicts

Spotted in Prophet

Desired syntax:

import narwhals as nw
import numpy as np
import pandas as pd

arr = np.zeros([[1,2,3], [4,5,6]])
df = nw.from_2d_array(arr, column_names=['a', 'b', 'c'])

It should also be possible to pass in dtype as an argument - if not passed, it'll be autoinferred

import narwhals as nw
import numpy as np
import pandas as pd

arr = np.zeros([[1,2,3], [4,5,6]])
df = nw.from_2d_array(arr, column_names=['a', 'b', 'c'], dtype=nw.Float64)