How to Fix the Python Pandas Error: typeerror: empty 'dataframe': no numeric data to plot

Python has some powerful functionality. But perhaps its biggest strength stems from the fact that it can be so effectively built upon with 3rd party libraries. A wide variety of additional libraries are available which can add advanced functionality to your Python code. However, some of these libraries extend the functionality so far that error messages become somewhat cryptic. And this is often the case with math-related libraries such as Pandas which add new data structures and operations to Python’s standard selection.

This is why a “typeerror: empty ‘dataframe’: no numeric data to plot” can pose some problems to Python developers. It emphasizes aspects of Pandas functionality in ways that aren’t always obvious to people more experienced with the language’s core functionality.

The Basics of this Type Error

The fundamental point to focus on with the error is the fact that it’s a type error. However, it’s a little different from the standard conflicts seen when trying to directly combine or work with incompatible types. Instead, the conflict comes about due to the fact that columns within the dataframe container have non-numeric values. Basically, it’s like asking for GPS coordinates and trying to plot a course with an answer of apple, ball, and moon.

A Deeper Dive Into the Type Conflict

This might seem like a straightforward issue at first. But it’s important to keep in mind that types aren’t always as obvious as they might appear at first glance. For example, it’s easy to assume that enteries in a DataFrame are numeric when you’re simply looking at them. But running the dtypes function might show them to be objects.

On top of this, there’s also the possibility that there might not be any data within the dataframe. This would essentially mean that any rows within the dataframe lack properly associated values. Between these issues, we have two potential problems to work through in order to find a solution. We need to ensure the dataframe has values within it. And we also need to ensure that those values are numeric.

How To Fix the Typeerror

It’s best to begin with the simplest possible solution. Pandas has a method called empty which can tell us whether a dataframe is empty or not. The following Python code demonstrates how it works.

import pandas as pd

df = pd.DataFrame()
print(df.empty)
df[‘Alpha’] = []
print(df.empty)

The code begins by creating an empty dataframe as df. It then prints out the results of running empty on that dataframe. As you’d expect, empty returns a value of true because the dataframe is empty. But what if we add a new column without any data in it? When we create an empty column called Alpha, the dataframe still registers as empty. This is one of the reasons why people’s assumptions about dataframes can lead them in the wrong direction. The definition of emptiness within Pandas isn’t quite the same as in conversational English. Because of this, fixing the error message should begin by actually determining whether or not the dataframe actually is empty or not.

But what about a situation where empty verifies that there’s information in the dataframe? In the following code sample, you’ll see another way in which appearances can be deceiving when looking at raw data.

import pandas as pd

df = pd.DataFrame({‘Alpha’: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’], ‘Beta’: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’],’Gamma’: [6, 7, 8, 9, 10]})
print(df)
print(df.dtypes)
df[[‘Alpha’, ‘Beta’]].plot()

In this example we create a dataframe and then print the contents to screen. Note that Beta appears to contain numeric data. However, they’re actually strings. This is an exmaple of something that’s fairly easy to notice when you have a specific assignment to examine. But imagine if Beta was dynamically generated from another part of your code. If you only saw the output from the print which follows it then you might assume those were numbers. Following the print statement, we find an easy way to get to the heart of the problem.

Dtypes will output the specific types within a dataset. Here we see that Alpha and Beta are objects while only Gamma is an integer. And when we try to plot Alpha and Beta the interpreter returns “no numeric data to plot”. When we plot values we first need to make sure they’re compatible with our needs. The astype function is an easy way to ensure compliance with a system’s underlying requirements. The following example shows how astype can ensure numeric values are present before we plot with them.

import pandas as pd

df = pd.DataFrame({‘Alpha’: [‘A’, ‘B’, ‘C’, ‘D’, ‘E’],’Beta’: [‘1’, ‘2’, ‘3’, ‘4’, ‘5’],’Gamma’: [‘6’, ‘7’, ‘8’, ‘9’, ’10’],’Delta’: [11, 12, 13, 14, 15] })

print(df)
print(df.dtypes)
df[‘Beta’]=df[‘Beta’].astype(float)
df[‘Gamma’]=df[‘Gamma’].astype(float)
df[‘Delta’]=df[‘Delta’].astype(float)
print(df.dtypes)
df[[‘Beta’, ‘Gamma’]].plot()
df.plot()

We start off in a similar way as the prior example. The first print shows that the dataframe contains strings with letters, strings with numbers, and actual numbers. Printing the dtypes results to screen shows that Alpha, Beta, and Gamma are objects while Gamma is an integer. We need to make sure that the values to plot are compatible. This process begins by using astype to change the entirety of the numeric strings in Beta and Gamma to actual numeric values. In this case they’re changed to a float. Delta doesn’t necessarily need converting since there are no missing values within it and it’s already a true numeric value. But for the sake of consistency, we’ll change it to a float as well.

Printing the dtypes shows that the dataframe now consists of Alpha as an object while everything else is a float. We can then plot Beta and Gamma. This should execute cleanly without the previous Python error appearing. And just to prove the overall consistency of the dataframe we plot with the entirety of the dataframe. Finally, it’s also important to look through your code and find out why dataframes are being populated in ways you didn’t expect.

How to Fix the Python Pandas Error: typeerror: empty 'dataframe': no numeric data to plot
Scroll to top