Skip to content

Commit b6e76ed

Browse files
x1-Davies Liu
authored andcommitted
[SPARK-8535] [PYSPARK] PySpark : Can't create DataFrame from Pandas dataframe with no explicit column name
Because implicit name of `pandas.columns` are Int, but `StructField` json expect `String`. So I think `pandas.columns` are should be convert to `String`. ### issue * [SPARK-8535 PySpark : Can't create DataFrame from Pandas dataframe with no explicit column name](https://issues.apache.org/jira/browse/SPARK-8535) Author: x1- <viva008@gmail.com> Closes apache#7124 from x1-/SPARK-8535 and squashes the following commits: d68fd38 [x1-] modify unit-test using pandas. ea1897d [x1-] For implicit name of pandas.columns are Int, so should be convert to String.
1 parent f457569 commit b6e76ed

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

python/pyspark/sql/context.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,15 @@ def createDataFrame(self, data, schema=None, samplingRatio=None):
344344
345345
>>> sqlContext.createDataFrame(df.toPandas()).collect() # doctest: +SKIP
346346
[Row(name=u'Alice', age=1)]
347+
>>> sqlContext.createDataFrame(pandas.DataFrame([[1, 2]]).collect()) # doctest: +SKIP
348+
[Row(0=1, 1=2)]
347349
"""
348350
if isinstance(data, DataFrame):
349351
raise TypeError("data is already a DataFrame")
350352

351353
if has_pandas and isinstance(data, pandas.DataFrame):
352354
if schema is None:
353-
schema = list(data.columns)
355+
schema = [str(x) for x in data.columns]
354356
data = [r.tolist() for r in data.to_records(index=False)]
355357

356358
if not isinstance(data, RDD):

0 commit comments

Comments
 (0)