Back to Blog
Developer guide
PYTHONApril 25, 2026

Python ValueError Examples and Fixes for Developers

Python developers often encounter the ValueError exception, which occurs when a function or operation receives an argument with an incorrect data type or value. This exception can be frustrating, but understanding its causes and fixes can help you resolve the issue quickly and efficiently. In this article, we will explore various Python ValueError examples and provide practical solutions to help you overcome common value-related errors.

1. Invalid literal for int() with base 10

The ValueError 'invalid literal for int() with base 10' error occurs when you attempt to convert a non-numeric string to an integer using the int() function or its variants like int() with base.

Why It Happens

This error occurs when the input string cannot be parsed as an integer, often due to the presence of non-numeric characters, whitespace, or incorrect encoding.

How to Fix It

To resolve this error, ensure that the input string is a valid representation of an integer. You can use the str.isdigit() method to check if the string consists only of digits. If the input is a string, consider using the int() function with a try-except block to handle potential errors.


2. ValueError when using the json.dumps function

The ValueError 'ValueError: No JSON object could be decoded' error occurs when you attempt to serialize a non-JSON-compatible object using the json.dumps() function.

Why It Happens

This error occurs when the object being serialized contains non-JSON-compatible data types, such as non-numeric or non-datetime values, or when the object's attributes are not serializable.

How to Fix It

To resolve this error, ensure that the object being serialized is JSON-compatible. You can use the json.dumps() function's default parameter to specify a custom serialization function for non-JSON-compatible objects. Alternatively, consider using the json.dumps() function's indent parameter to format the output as a pretty JSON string.


3. ValueError when using the datetime.strptime function

The ValueError 'ValueError: time data did not match format' error occurs when you attempt to parse a date or time string using the datetime.strptime() function with an incorrect format string.

Why It Happens

This error occurs when the input string does not match the specified format string, often due to incorrect date or time formats, missing or extra information, or incorrect separators.

How to Fix It

To resolve this error, ensure that the input string matches the specified format string. You can use the datetime.strptime() function's format parameter to specify the correct format string for the input string. Alternatively, consider using the datetime.datetime.now() function or the dateutil library to parse the input string.


4. ValueError when using the pandas.to_datetime function

The ValueError 'ValueError: cannot convert string 'NaN' to float' error occurs when you attempt to convert a Series or DataFrame containing non-numeric values to a datetime type using the pandas.to_datetime function.

Why It Happens

This error occurs when the Series or DataFrame contains non-numeric values, such as NaN or strings, which cannot be converted to a datetime type.

How to Fix It

To resolve this error, ensure that the Series or DataFrame contains only numeric values. You can use the pandas.to_numeric function to convert non-numeric values to NaN before attempting to convert the Series or DataFrame to a datetime type. Alternatively, consider using the pandas.to_datetime function's errors parameter to specify how to handle non-numeric values.


5. ValueError when using the numpy.array function

The ValueError 'ValueError: could not convert string to float' error occurs when you attempt to create a numpy array from a list or tuple containing non-numeric values.

Why It Happens

This error occurs when the list or tuple contains non-numeric values, such as strings or non-numeric characters, which cannot be converted to a float type.

How to Fix It

To resolve this error, ensure that the list or tuple contains only numeric values. You can use the numpy.array function's dtype parameter to specify the data type of the array. Alternatively, consider using the numpy.array function's convert_scalar parameter to convert non-numeric values to NaN before creating the array.


6. ValueError when using the collections.Counter function

The ValueError 'ValueError: cannot increment an instance of Counter' error occurs when you attempt to increment a collections.Counter object with a non-numeric value.

Why It Happens

This error occurs when the Counter object is incremented with a non-numeric value, such as a string or a non-numeric character, which cannot be used to update the Counter's count.

How to Fix It

To resolve this error, ensure that the value being incremented is a numeric value. You can use the collections.Counter.update function to update the Counter's count with a list or tuple of numeric values. Alternatively, consider using the collections.Counter.get function to retrieve the current count for a given value.


7. ValueError when using the re module's search function

The ValueError 'ValueError: cannot find a match for the regular expression' error occurs when you attempt to search for a pattern in a string using the re module's search function with an invalid regular expression.

Why It Happens

This error occurs when the regular expression is invalid or contains syntax errors, which cannot be used to search for a pattern in the string.

How to Fix It

To resolve this error, ensure that the regular expression is valid and contains no syntax errors. You can use the re module's compile function to compile the regular expression before using it to search for a pattern in the string. Alternatively, consider using the re module's error handling features, such as the re module's IGNORECASE flag, to improve error handling.


8. ValueError when using the math module's functions

The ValueError 'ValueError: math domain error' error occurs when you attempt to use a math module function with an input value that is outside the function's domain.

Why It Happens

This error occurs when the input value is outside the domain of the math module function, such as attempting to calculate the square root of a negative number.

How to Fix It

To resolve this error, ensure that the input value is within the domain of the math module function. You can use the math module's functions to check the input value's validity before using it to calculate the result. Alternatively, consider using the math module's error handling features, such as the math module's copysign function, to improve error handling.

Conclusion

Python ValueError errors can be frustrating, but understanding their causes and fixes can help you resolve them quickly and efficiently. By following the solutions and examples provided in this article, you can overcome common value-related issues and improve your overall Python development experience.

Explore More Debugging Resources

- [Browse all PYTHON errors](/languages/python)

- [Browse errors by type](/error-types)

- [Search all documented errors](/search)

- [Use the Error Explainer](/error-explainer-tool)

Browse allPython errors

Related PYTHON Articles

Have a specific error? Get an instant explanation.

Explain an Error