Yicong-Huang opened a new pull request, #54144:
URL: https://github.com/apache/spark/pull/54144

   ### 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 **Spark Connect**.
   
   The issue occurs due to two PyArrow limitations:
   1. `pa.RecordBatch.from_arrays([], [])` loses row count information
   2. `pa.Table.cast()` on a 0-column table resets the row count to 0
   
   **Changes:**
   1. Handle 0-column pandas DataFrames separately using 
`pa.Table.from_struct_array()` to preserve row count
   2. Skip the `cast()` operation for 0-column tables as it loses 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(10))  # 10 rows, 0 columns
   df = spark.createDataFrame(pdf, schema=StructType([]))
   df.count()  # Returns 0 (wrong!)
   ```
   
   After this fix:
   ```python
   df.count()  # Returns 10 (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 Spark Connect.
   
   ### How was this patch tested?
   
   Added unit test `test_from_pandas_dataframe_with_zero_columns` in 
`test_connect_creation.py`
   
   ### 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]

Reply via email to