DEV Community

Cover image for Azure Databricks: Fix “DataFrame Object Has No Attribute write”
Jin
Jin

Posted on • Originally published at blog.luca-liu.com

Azure Databricks: Fix “DataFrame Object Has No Attribute write”

While writing data from an Azure Databricks notebook to SQL Server, I encountered the following error:

AttributeError: 'DataFrame' object has no attribute 'write'
Enter fullscreen mode Exit fullscreen mode

The notebook was running on Azure Databricks Serverless Compute.

The error occurred because the DataFrame was created with pandas, but the code attempted to use the PySpark .write API.

The Problem

Suppose we have a Pandas DataFrame:

import pandas as pd

data = {
    "Product": [
        "Laptop", "Mouse", "Keyboard", "Monitor", "Headset",
        "Webcam", "USB Hub", "Laptop", "Mouse", "Monitor"
    ],
    "Region": [
        "North", "South", "East", "West", "North",
        "East", "South", "West", "North", "East"
    ],
    "Units_Sold": [15, 42, 30, 8, 25, 17, 60, 22, 38, 10],
    "Revenue": [
        22500, 1260, 2400, 3200, 1875,
        850, 1800, 33000, 1140, 4000
    ]
}

df = pd.DataFrame(data)
Enter fullscreen mode Exit fullscreen mode

We then try to write the DataFrame to SQL Server:

df.write \
    .format("sqlserver") \
    .options(**SQL_OPTIONS) \
    .option("dbtable", "test_table") \
    .mode("overwrite") \
    .save()
Enter fullscreen mode Exit fullscreen mode

The operation fails with:

Why This Error Happens

The variable df is a Pandas DataFrame, not a PySpark DataFrame.

You can confirm this by checking its type:

print(type(df))
Enter fullscreen mode Exit fullscreen mode

The result will be similar to:

<class 'pandas.core.frame.DataFrame'>
Enter fullscreen mode Exit fullscreen mode

Pandas and PySpark both use the name DataFrame, but they are different Python classes with different APIs.

A Pandas DataFrame provides writing methods such as:

df.to_csv()
df.to_excel()
df.to_parquet()
df.to_sql()
Enter fullscreen mode Exit fullscreen mode

It does not provide the Spark .write property.

The following syntax belongs to the PySpark DataFrame API:

df.write.format(...).options(...).mode(...).save()
Enter fullscreen mode Exit fullscreen mode

For a PySpark DataFrame, .write returns a DataFrameWriter, which is responsible for writing data to external storage systems and databases.

The Solution

Convert the Pandas DataFrame to a PySpark DataFrame before calling .write:

df = spark.createDataFrame(df)
Enter fullscreen mode Exit fullscreen mode

After the conversion, the write operation works:

df.write \
    .format("sqlserver") \
    .options(**SQL_OPTIONS) \
    .option("dbtable", "test_table") \
    .mode("overwrite") \
    .save()
Enter fullscreen mode Exit fullscreen mode

spark.createDataFrame() can create a PySpark DataFrame directly from a Pandas DataFrame. Spark converts the local Pandas data into a Spark DataFrame and infers the column schema when no schema is provided.

Complete Working Example

Using different variable names makes it clear which DataFrame type is being used:

Summary

The error is caused by using a PySpark method on a Pandas DataFrame.


Explore more

Thank you for taking the time to explore data-related insights with me. I appreciate your engagement.

Connect with me on LinkedIn

Connect with me on X

Top comments (0)