Python Pandas

How to Fix TypeError – no numeric data to plot

Visualizing a pandas DataFrame is possible by plotting the columns in plots like scatter plot or bar plot. When plotting data, if the data holds string type, you will encounter a TypeError – no numeric data to plot because the data is not numeric. It will raise the same error if at least one string value is found in the data.

In this guide, we will discuss how to fix this error by converting the data from string to numeric type (int or float) using pandas.DataFrame.astype() and pandas.to_numeric() functions.

Reproducing the Error

First, produce this error by creating the plot.

Create ‘day1_Data’ DataFrame with two columns (Product_Quantity & Purchase). Try to plot the data by passing these two columns as x and y to the pandas.DataFrame.plot() function.

import pandas

# Products DataFrame
day1_Data = pandas.DataFrame({'Product_Quantity': ['2','10','5','7','9','20','30','50','90','100'],
                   'Purchase': ['500','1200','700','900','1000','1500','2000','3500','4400','5000']})
print(day1_Data,"\n")
print(day1_Data.dtypes)

# Try to plot the two columns
day1_Data.plot(x='Product_Quantity', y='Purchase',figsize=(4,2))

Output

You can see that an error is raised due to the datatype of the columns that we are going to plot. It should be either an int or float.

Solution 1: Change the Type Using pandas.DataFrame.astype

If we change the data type of the columns to a numeric type using the pandas.DataFrame.astype() function, DataFrame can be plotted. Basically, this function casts the pandas object (DataFrame in our scenario) to the specified data type.

Syntax

Let’s see the syntax of pandas.DataFrame.astype() function.

pandas.DataFrame.astype(dtype, copy, errors)
  1. dtype – This parameter takes the data type to which the DataFrame columns are coveted to this type.
  2. copy – we can use this parameter to return the copy by setting it to True.
  3. errors (Default = ‘raise’) – If invalid data type is passed as a parameter to this function, it will raise an exception. In order to suppress the exceptions, set this parameter to ‘ignore.

Example

Cast the two columns in the above data into integer type by passing the dtype as ‘int’ to the pandas.DataFrame.astype() function. Then, plot the two columns.

day1_Data['Product_Quantity'] = day1_Data['Product_Quantity'].astype(int) day1_Data['Purchase'] = day1_Data['Purchase'].astype(int)

import pandas

# Products DataFrame
day1_Data = pandas.DataFrame({'Product_Quantity': ['2','10','5','7','9','20','30','50','90','100'],
                   'Purchase': ['500','1200','700','900','1000','1500','2000','3500','4400','5000']})
print(day1_Data.dtypes,"\n")

# Convert both the columns to integer
day1_Data['Product_Quantity'] = day1_Data['Product_Quantity'].astype(int)
day1_Data['Purchase'] = day1_Data['Purchase'].astype(int)
print(day1_Data.dtypes,"\n")

# Plot the two columns
day1_Data.plot(x='Product_Quantity', y='Purchase',figsize=(4,2))

Output

First, we casted the data type from object to int64 and then plotted the two columns.

Solution 2: Change the Type Using pandas.to_numeric()

This function converts the provided argument to numeric type.

Syntax

Let’s see the syntax of the pandas.to_numeric() function with parameters in detail.

pandas.to_numeric(arg, errors, downcast)
  1. arg – It refers to the input one-dimensional data to be converted into numeric type. You can pass DataFrame columns, Series, List, Tuple, Array etc.
  2. errors – If the arg holds string type elements, this function automatically raise a ValueError exception. You can control the flow of the code without raising the error.
    1. If errors = ‘raise’ (default), then invalid parsing will raise an exception.
    2. If errors = ‘coerce’, then invalid elements will be set as NaN.
    3. If errors = ‘ignore’, then invalid elements will return as existing.
  3. downcast – You can change the datatype of the argument while converting it to numeric.
    1. If downcast is ‘integer’ or ‘signed’, then the data type is casted into smallest signed int – int8
    2. If downcast is ‘unsigned’, then the data type is casted into smallest unsigned int – uint8
    3. If downcast is ‘float’, then the data type is casted into smallest float type – float32
    4. If downcast is ‘None’, Actual argument data type is considered.

Example

Cast the two columns in the above data into integer type by passing the dtype as ‘int’ to the pandas.to_numeric() function. Plot the two columns.

day1_Data['Product_Quantity'] = pandas.to_numeric(day1_Data['Product_Quantity']) day1_Data['Purchase'] = pandas.to_numeric(day1_Data['Purchase'])


import pandas

# Products DataFrame
day1_Data = pandas.DataFrame({'Product_Quantity': ['2','10','5','7','9','20','30','50','90','100'],
                   'Purchase': ['500','1200','700','900','1000','1500','2000','3500','4400','5000']})
print(day1_Data.dtypes,"\n")

# Convert both the columns to integer
day1_Data['Product_Quantity'] = pandas.to_numeric(day1_Data['Product_Quantity'])
day1_Data['Purchase'] = pandas.to_numeric(day1_Data['Purchase'])
print(day1_Data.dtypes,"\n")

# Plot the two columns
day1_Data.plot(x='Product_Quantity', y='Purchase',figsize=(4,2),color='green')

Output

First, we casted the data type from object to int64, and then plotted the two columns.

Conclusion

We learned how to plot the data without getting the TypeError: no numeric data to plot by casting the datatype the column from object to integer using pandas.Dataframe.astype() and pandas.to_numeric() functions with examples. Both the functions are explained in detail with all the parameters. It’s your time to plot the data with different plots like scatter plot, bar plot, and more.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain