Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions python/pyspark/sql/connect/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ def createDataFrame(
spark_type = from_arrow_type(field_type)
struct.add(field.name, spark_type, nullable=field.nullable)
schema = struct
if len(schema) == 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qq, how's Spark Classic code path? I think we should keep it matched

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @HyukjinKwon!
In spark classic the code does not throw an error

df = pd.DataFrame(index=range(5))
spark.createDataFrame(df).collect()

this code results in an empty list []. We could add the same error for the classic spark, but it will be a breaking change technically. Do you think it makes sense to do that?

raise PySparkValueError(
errorClass="CANNOT_INFER_EMPTY_SCHEMA",
messageParameters={},
)
elif isinstance(schema, (list, tuple)) and cast(int, _num_cols) < len(data.columns):
assert isinstance(_cols, list)
_cols.extend([f"_{i + 1}" for i in range(cast(int, _num_cols), len(data.columns))])
Expand Down
19 changes: 15 additions & 4 deletions python/pyspark/sql/tests/connect/test_connect_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,21 @@ def test_with_local_data(self):
self.assertEqual(rows[0][0], 3)
self.assertEqual(rows[0][1], "c")

# Check correct behavior for empty DataFrame
pdf = pd.DataFrame({"a": []})
with self.assertRaises(ValueError):
self.connect.createDataFrame(pdf)
def test_from_empty_pandas_dataframe(self):
dfs = [
pd.DataFrame(),
pd.DataFrame({"a": []}),
pd.DataFrame(index=range(5)),
]

for df in dfs:
with self.assertRaises(PySparkValueError) as pe:
self.connect.createDataFrame(df)
self.check_error(
exception=pe.exception,
errorClass="CANNOT_INFER_EMPTY_SCHEMA",
messageParameters={},
)

def test_with_local_ndarray(self):
"""SPARK-41446: Test creating a dataframe using local list"""
Expand Down