1
$\begingroup$

I ran into a weird error with pandas quantile() (pandas.DataFrame.quantile()) method. I have a dataframe with both numeric and non-numeric columns. I wanted to calculate the 75th quantile for each numeric column. So instead of providing only the numeric columns, I saw in the documentation that quantile() takes a keyword argument numeric_only which does the job. Here is the link to the documentation. But when I run the quantile() with numeric_only = True, python gives me this error:

TypeError: quantile() got an unexpected keyword argument 'numeric_only'

I also checked the implementation of the quantile() and this keyword argument is implemented. Can anyone explain why am I getting this error? Below is a toy example.

df = pd.DataFrame.from_dict({'a':[1,1,2,1,3,2,4,5,4,6,2,3,2,5,5,5]})
df['a'].quantile(q=0.5,numeric_only=True) # doesn't work
df['a'].quantile(q=0.5) # works

Thanks for your help.

$\endgroup$

2 Answers 2

3
$\begingroup$

By using df['a'].quantile your are calling the quantile method of a column, i.e. a series. This does not have this parameter ( see here).

Since the series has just one type, it is not required.

$\endgroup$
1
$\begingroup$

The error message you're receiving is because the quantile() method does not accept a numeric_only argument.

The numeric_only argument is applicable for some pandas methods (like mean(), sum(), etc.) that can be applied to a DataFrame which contains both numeric and non-numeric columns. These methods will only be applied to the numeric columns when numeric_only=True.

However, when you are using the quantile() method on a Series (which is what df['a'] is), the numeric_only argument is not applicable since a Series only contains a single set of data and should be of the same data type. If you are applying quantile() on a DataFrame, it will automatically apply the method to the numeric columns only, as non-numeric data does not have a concept of quantiles.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.