Skip to content

ENH: Better error message when interpolating dtype integer with method="linear" #41565

@AartGoossens

Description

@AartGoossens

Is your feature request related to a problem?

When trying to interpolate a column with dtype integer with method="linear" the error message is not very descriptive (example to reproduce at the bottom of this issue):

ValueError: Invalid fill method. Expecting pad (ffill) or backfill (bfill). Got linear

As "linear" is the default interpolation method I think the error message should be more descriptive, even though IntegerArray is still experimental according to the docs.

Describe the solution you'd like

I think the error message should include a hint to the potential cause that the array you're trying to interpolate is of dtype integer. Something like this could work:

ValueError: Invalid fill method. Expecting pad (ffill) or backfill (bfill). Got linear. Are you trying to interpolate an integer column?

I would be happy to create a PR with the improvement myself.

API breaking implications

As this only requires a change to the error message it does not break any API.

Describe alternatives you've considered

An alternative solution would be to implement linear interpolation for integer arrays but I suspect that that is not compatible/unexpected with some use-cases of integer arrays.

Additional context

Reproducable example:

import pandas as pd


s = pd.Series([1, None, 3], dtype=pd.Int64Dtype())

s.interpolate(method="linear")

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementError ReportingIncorrect or improved errors from pandasMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateNA - MaskedArraysRelated to pd.NA and nullable extension arraysNeeds TestsUnit test(s) needed to prevent regressionsgood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions