Yicong-Huang opened a new pull request, #54382: URL: https://github.com/apache/spark/pull/54382
### What changes were proposed in this pull request? This PR fixes the row count loss issue when creating a Spark DataFrame from a pandas DataFrame with 0 columns in **Classic Spark**. The issue occurs due to PyArrow limitations when creating RecordBatches or Tables with 0 columns - row count information is lost. **Changes:** Fixed 3 code paths in `SparkConversionMixin` to preserve row count for 0-column DataFrames: 1. `_create_from_pandas_with_arrow()` - Use `pa.RecordBatch.from_struct_array()` for 0-column case 2. `_convert_from_pandas()` - Return list of empty Rows to preserve row count 3. `_create_from_arrow_table()` - Skip `cast()` operation for 0-column tables as it resets row count ### Why are the changes needed? Before this fix: ```python import pandas as pd from pyspark.sql.types import StructType pdf = pd.DataFrame(index=range(5)) # 5 rows, 0 columns df = spark.createDataFrame(pdf, schema=StructType([])) df.count() # Returns 0 (wrong!) ``` After this fix: ```python df.count() # Returns 5 (correct!) ``` ### Does this PR introduce _any_ user-facing change? Yes. Creating a DataFrame from a pandas DataFrame with 0 columns now correctly preserves the row count in Classic Spark. ### How was this patch tested? Added unit test `test_from_pandas_dataframe_with_zero_columns` in `test_creation.py` that tests both Arrow-enabled and Arrow-disabled paths. ### Was this patch authored or co-authored using generative AI tooling? No -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
