pkz
pkz

Reputation: 41

How to control X's and Y's when interplolating with pandas

I want to use spline interpolation so I can fill some nulls, but I can not find a way to specify X's and Y's for Pandas. It automatically select the index to be the X's and fill nulls for all columns that have nulls respectively. Any ideas of how to make it work? or do I need to use SciPy?

I tried somthing like this:

df.interpolate(x='some_column1', y='some_column2', method='spline', order=1)

but I got: TypeError: _interpolate_scipy_wrapper() got multiple values for keyword argument 'y'

I know there is an option to reset the index, but is there another way to choose which columns I want to use?

Upvotes: 1

Views: 173

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76297

Pandas interpolation doesn't allow you to simultaneously specify x, y, and metho. For greater control over interpolation, you might want to use scipy.interp1d:

df['some_column_2'] = scipy.interp1d(df['some_column_1'], df['some_column_2'])

Notes:

  • If the DataFrame is not sorted by some_column_1, specify assume_sorted=False.
  • By default, interp1d does linear interpolation. It's possible to change this with the kind parameter.

Upvotes: 1

Related Questions