viirya commented on a change in pull request #28025: 
[SPARK-31186][PySpark][SQL] toPandas should not fail on duplicate column names

 File path: python/pyspark/sql/pandas/
 @@ -132,25 +132,36 @@ def toPandas(self):
         # Below is toPandas without Arrow optimization.
         pdf = pd.DataFrame.from_records(self.collect(), columns=self.columns)
-        dtype = {}
-        for field in self.schema:
+        dtype = [None] * len(self.schema)
+        for fieldIdx in range(len(self.schema)):
+            field = self.schema[fieldIdx]
+            pandas_col = pdf.iloc[:, fieldIdx]
             pandas_type = 
             # SPARK-21766: if an integer field is nullable and has null 
values, it can be
             # inferred by pandas as float column. Once we convert the column 
with NaN back
             # to integer type e.g., np.int16, we will hit exception. So we use 
the inferred
             # float type, not the corrected type from the schema in this case.
             if pandas_type is not None and \
                 not(isinstance(field.dataType, IntegralType) and 
field.nullable and
-                    pdf[].isnull().any()):
-                dtype[] = pandas_type
+                    pandas_col.isnull().any()):
+                dtype[fieldIdx] = pandas_type
             # Ensure we fall back to nullable numpy types, even when whole 
column is null:
-            if isinstance(field.dataType, IntegralType) and 
-                dtype[] = np.float64
-            if isinstance(field.dataType, BooleanType) and 
-                dtype[] = np.object
+            if isinstance(field.dataType, IntegralType) and 
+                dtype[fieldIdx] = np.float64
+            if isinstance(field.dataType, BooleanType) and 
+                dtype[fieldIdx] = np.object
+        df = pd.DataFrame()
+        for index in range(len(dtype)):
+            t = dtype[index]
 Review comment:

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.
For queries about this service, please contact Infrastructure at:

With regards,
Apache Git Services

To unsubscribe, e-mail:
For additional commands, e-mail:

Reply via email to