Fixing Python Error: max must be larger than min in range parameter

The “max must be larger than min in range parameter” error message shows up in Python when creating a histogram and the data has nan values in it. These missing values throw off the range parameter resulting in our error message. The way to fix this problem is to remove the nan values from the data before putting it through the histogram function. While it is an easy fix, it is not intuitive based on the error message itself because it does not describe the actual problem.

Description of the error.

The “max must be larger than min in range parameter” error message occurs when trying to create a histogram using the hist function. The hist function is a form of the aggregate function and the bin size in a matplotlib histogram is sensitive to the presence of nan values and it causes problems with the default maximum value and minimum value. One thing that can cause this problem is creating a data type using the dtype function. The hist function can be used to define the range providing it within the index for either the x or y axis. The problem occurs when using the default value for the range which is set by the array of values in the data being used. The hist function can be used to create bin edges, but when working with a data frame it uses a column and not a row.

Explanation of the error

Here we have an example of Python code that produces the “max must be larger than min in range parameter” error message.

import matplotlib.pyplot as plt
import pandas as pd
A = [[5], [],[1], [8], [5], [7]]
df = pd.DataFrame(A, columns = [‘X’])
print(df)
X
0 5.0
1 NaN
2 1.0
3 8.0
4 5.0
5 7.0
plt.hist(df[‘X’].values)
plt.show()
ValueError: max must be larger than min in range parameter.

In this example our input data frame has a nan value, as a result, the hist function cannot produce a meaningful range and therefore it produces our error message.

How to fix the error

Here is an example of Python code that fixes the “max must be larger than min in range parameter” error message.

import matplotlib.pyplot as plt
import pandas as pd
A = [[5], [],[1], [8], [5], [7]]
df = pd.DataFrame(A, columns = [‘X’])
print(df)

X
0 5.0
1 NaN
2 1.0
3 8.0
4 5.0
5 7.0

print(df[‘X’].dropna().values)

[5. 1. 8. 5. 7.]

plt.hist(df[‘X’].dropna().values)

To fix the problem we put the input data through the dropna function to remove all nan values before putting it through the hist function. This process eliminates the missing value from our example. This ends the parameter problem caused by the missing value.

The “max must be larger than min in range parameter” error message is an easy-to-understand error message. It can occur anytime you do not have control over the data that you are doing a histogram on. This fix, however, will remove all missing data from the data set on which you are working. It can also happen by accidentally leaving a data space blank. Fortunately, this problem is easy to fix, because there is a function that can do the job.

Scroll to top
Privacy Policy