You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2021/10/28 00:50:13 UTC

[GitHub] [spark] HyukjinKwon commented on a change in pull request #34401: [SPARK-30537][PYTHON], Fix toPandas wrong dtypes when applied on empty DF when Arrow enabled

HyukjinKwon commented on a change in pull request #34401:
URL: https://github.com/apache/spark/pull/34401#discussion_r737943724



##########
File path: python/pyspark/sql/pandas/conversion.py
##########
@@ -151,7 +151,17 @@ def toPandas(self) -> "PandasDataFrameLike":
                                     _convert_map_items_to_dict(pdf[field.name])
                         return pdf
                     else:
-                        return pd.DataFrame.from_records([], columns=self.columns)
+                        pdf = pd.DataFrame.from_records([], columns=self.columns)
+                        df = pd.DataFrame()
+                        for fieldIdx, field in enumerate(self.schema):
+                            pandas_type = \
+                                PandasConversionMixin._to_corrected_pandas_type(field.dataType)
+                            column_name = self.schema[fieldIdx].name
+                            series = pdf.iloc[:, fieldIdx]
+                            if pandas_type is not None:
+                                series = series.astype(pandas_type, copy=False)
+                            df.insert(fieldIdx, column_name, series, allow_duplicates=True)

Review comment:
       Is there simpler and easier way to assign types to an empty pandas DataFrame? The current way looks too messy.




-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org