Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

PySpark cannot turn a float pandas column into a Spark string

Your error

PySparkTypeError: Exception thrown when converting pandas.Series (float64) with name 'name' to Arrow Array (string).3 more lines it can show

Fix it fast

Most common cause, 1 of 2

createDataFrame gets float64 data for a field the schema calls string

Make the pandas column a real string column before handing it to Spark.

Fix
pdf = pdf.astype({"name": "string"})
df = spark.createDataFrame(pdf, "id long, name string")

# or keep the numbers numeric
df = spark.createDataFrame(pdf, "id long, name double")
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Exception thrown when converting pandas.Series (float64) with name 'x' to Arrow Array (string) means PySpark was told the column is a string while pandas holds floats. createDataFrame raises it when Arrow fallback is off; pandas UDFs and applyInPandas say Cannot convert the output value instead. Cast with astype("string") or declare a double. Applies to PySpark 4.3.

  • Applies to PySpark 4.3.0, pandas 2.3.3, PyArrow 26.0.1, OpenJDK 17
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the pandas dtypes next to the Spark schema you pass, and note where the error is raised. A failure on the createDataFrame line in the driver is cause 1; a PythonException from a worker while a pandas UDF, applyInPandas or mapInPandas runs is cause 2.

Run this first
print(pdf.dtypes)
print(spark.conf.get("spark.sql.execution.arrow.pyspark.fallback.enabled"))

Then find the line you see in this list and open its fix:

  1. 1createDataFrame gets float64 data for a field the schema calls stringThe error comes from spark.createDataFrame(pdf, schema); with fallback on you only see the createDataFrame attempted Arrow optimization warning
  2. 2A pandas UDF or applyInPandas returns floats for a string return typeA PythonException from the worker ends with Cannot convert the output value of the column ... with type 'float64' to the specified return type of the column: 'string'

What does the error look like?

  • PySparkTypeError: Exception thrown when converting pandas.Series (float64) with name 'name' to Arrow Array (string).
  • createDataFrame attempted Arrow optimization because 'spark.sql.execution.arrow.pyspark.enabled' is set to true; however, failed by the reason below:
  • PySparkTypeError: Cannot convert the output value of the column 'name' with type 'float64' to the specified return type of the column: 'string'. Please check if the data types match and try again.
  • pyarrow.lib.ArrowTypeError: Expected a string or bytes dtype, got float64

Cause 1 of 2

createDataFrame gets float64 data for a field the schema calls string

You seeThe error comes from spark.createDataFrame(pdf, schema); with fallback on you only see the createDataFrame attempted Arrow optimization warning

Check

The Exception thrown when converting pandas.Series message is raised by createDataFrame's Arrow path when a column's pandas dtype, here float64, cannot become the Arrow type of the matching schema field. Arrow is on by default for this call. If spark.sql.execution.arrow.pyspark.fallback.enabled is true, PySpark prints a warning and retries without Arrow; if it is false, the error stops the job.

Check
print(pdf.dtypes["name"], spark.conf.get("spark.sql.execution.arrow.pyspark.enabled"))

Fix

Make the pandas column a real string column before handing it to Spark. The string dtype keeps missing values as pd.NA, which Spark receives as null. If the values are really numbers, change the schema field to double instead.

Fix
pdf = pdf.astype({"name": "string"})
df = spark.createDataFrame(pdf, "id long, name string")

# or keep the numbers numeric
df = spark.createDataFrame(pdf, "id long, name double")

Verify

createDataFrame finishes with no warning, and nulls stay null instead of turning into text.

Verify
df.printSchema(); df.show()

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

A pandas UDF or applyInPandas returns floats for a string return type

You seeA PythonException from the worker ends with Cannot convert the output value of the column ... with type 'float64' to the specified return type of the column: 'string'

Check

In PySpark 4.3 the same mismatch inside a pandas UDF, applyInPandas or mapInPandas is reported as Cannot convert the output value of the column, wrapping PyArrow's Expected a string or bytes dtype, got float64. The Arrow guide says the returned pandas.DataFrame has to match the output schema you declare, and string is not filled from float64 automatically.

Check
print(per_group(pdf.head(5)).dtypes)

Fix

Cast inside the function so the returned column already has the declared type, or declare the column as double if it holds numbers.

Fix
import pandas as pd
from pyspark.sql.functions import pandas_udf

@pandas_udf("string")
def price_label(s: pd.Series) -> pd.Series:
    return (s * 1.5).astype("string")

def per_group(pdf: pd.DataFrame) -> pd.DataFrame:
    out = pd.DataFrame({"id": pdf["id"], "name": pdf["id"] * 1.5})
    out["name"] = out["name"].astype("string")
    return out

df.groupBy("id").applyInPandas(per_group, "id long, name string")

Verify

Run an action such as collect(). Rows come back with string values and no PythonException.

Verify
df.select(price_label("id")).collect()

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Give text columns dtype="string" when the pandas frame is built, so number-like codes never arrive in Spark as float64.
  • Set spark.sql.execution.arrow.pyspark.fallback.enabled to false in tests, so a type mismatch fails there instead of quietly taking the slower non-Arrow path.
  • Test a pandas upgrade before using it with PySpark 4.3: the Arrow guide sets pandas 2.2.0 as the minimum and does not guarantee compatibility or data correctness on newer releases.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.