Skip to content

Conversation

@xinrong-meng
Copy link
Member

What changes were proposed in this pull request?

Support DataFrame creation from 2d NumPy array with arbitrary columns.

Why are the changes needed?

Currently, DataFrame creation from 2d ndarray works only with 2 columns. We should provide complete support for DataFrame creation with 2d ndarray.

As part of SPARK-39405.

Does this PR introduce any user-facing change?

Yes.
Before

>>> spark.createDataFrame(np.array([[1], [2]])).dtypes
Traceback (most recent call last):
...
    raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
ValueError: Shape of passed values is (2, 1), indices imply (2, 2)


>>> spark.createDataFrame(np.array([[1, 1, 1], [2, 2, 2]])).dtypes
Traceback (most recent call last):
...
    raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
ValueError: Shape of passed values is (2, 3), indices imply (2, 2)

After

>>> spark.createDataFrame(np.array([[1], [2]])).dtypes
[('value', 'bigint')]

>>> spark.createDataFrame(np.array([[1, 1, 1], [2, 2, 2]])).dtypes
[('_1', 'bigint'), ('_2', 'bigint'), ('_3', 'bigint')]

How was this patch tested?

Unit tests.

@HyukjinKwon
Copy link
Member

Merged to master.

SandishKumarHN pushed a commit to SandishKumarHN/spark that referenced this pull request Dec 12, 2022
…itrary columns

### What changes were proposed in this pull request?
Support DataFrame creation from 2d NumPy array with arbitrary columns.

### Why are the changes needed?
Currently, DataFrame creation from 2d ndarray works only with 2 columns. We should provide complete support for DataFrame creation with 2d ndarray.

As part of [SPARK-39405](https://issues.apache.org/jira/browse/SPARK-39405).

### Does this PR introduce _any_ user-facing change?
Yes.
Before
```py
>>> spark.createDataFrame(np.array([[1], [2]])).dtypes
Traceback (most recent call last):
...
    raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
ValueError: Shape of passed values is (2, 1), indices imply (2, 2)

>>> spark.createDataFrame(np.array([[1, 1, 1], [2, 2, 2]])).dtypes
Traceback (most recent call last):
...
    raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
ValueError: Shape of passed values is (2, 3), indices imply (2, 2)
```

After
```py
>>> spark.createDataFrame(np.array([[1], [2]])).dtypes
[('value', 'bigint')]

>>> spark.createDataFrame(np.array([[1, 1, 1], [2, 2, 2]])).dtypes
[('_1', 'bigint'), ('_2', 'bigint'), ('_3', 'bigint')]
```

### How was this patch tested?
Unit tests.

Closes apache#38473 from xinrong-meng/ncol_ndarr.

Authored-by: Xinrong Meng <[email protected]>
Signed-off-by: Hyukjin Kwon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants