Skip to content
Advertisement

Spline interpolation on dataframes by row

I have the following data frame:

JavaScript

I am trying to apply a spline interpolation on each row to get the values for 2017 and 2018 using the following code:

JavaScript

However, I get the following error: ValueError: Index column must be numeric or datetime type when using spline method other than linear. Try setting a numeric or datetime index column before interpolating.

The dataframe in this question is just a subset of a much larger dataset I am using. All of the examples I have seen do the spline interpolation down each column, but I can’t seem to get it work across each row. I feel like it’s a simple solution and I’m just missing it. Could someone please help?

Advertisement

Answer

It appears to be because the dtype of the index (really columns for axis=1) is probably object in your case since the index contains a string column name also. Even though you are grabbing a slice of the columns that contains only integer years the overall index dtype remains the same – object. Then it looks like interpolate looks at the dtype and punts when it sees a dtype of object.

Example – even though the years are stored as integers the overall dtype is object:

JavaScript

If we did this:

JavaScript

Then the axis=1 interpolation works:

JavaScript

Dropping the OBJECTID was done to illustrate what is going on.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement