Pandas interpolate() backwards in dataframe

2024/9/8 8:45:11

Going forward, interpolate works great:

       name    days
0      a       NaN
1      a       NaN
2      a         2
3      a         3
4      a       NaN 
5      a       NaN  records.loc[:, 'days'].interpolate(method='linear', inplace=True)name    days
0      a       NaN
1      a       NaN
2      a         2
3      a         3
4      a         4 
5      a         5  

...however, it does not address the beginning rows (only goes forward). The limit_direction param allows {‘forward’, ‘backward’, ‘both’}. None of these works. Is there a proper way to interpolate backwards?

We can assume a series incrementing or decrementing by 1, which may not start at 0 as it happens to in this example.

Answer

It seems it works only with parameter limit see docs [In 47]:

Add a limit_direction keyword argument that works with limit to enable interpolate to fill NaN values forward, backward, or both (GH9218, GH10420, GH11115)

records = pd.DataFrame(
{'name': {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'a', 5: 'a', 6: 'a', 7: 'a', 8: 'a', 9: 'a'}, 
'days': {0: 0.0, 1: np.nan, 2: np.nan, 3: np.nan, 4: 4.0, 5: 5.0, 6: np.nan, 7: np.nan, 8: np.nan, 9: 9.0}}, 
columns=['name','days'])print (records)name  days
0    a   0.0
1    a   NaN
2    a   NaN
3    a   NaN
4    a   4.0
5    a   5.0
6    a   NaN
7    a   NaN
8    a   NaN
9    a   9.0
#by default limit_direction='forward'
records['forw'] = records['days'].interpolate(method='linear', limit=1)
records['backw'] = records['days'].interpolate(method='linear',limit_direction='backward', limit=1)
records['both'] = records['days'].interpolate(method='linear', limit_direction='both', limit=1)
print (records)name  days  forw  backw  both
0    a   0.0   0.0    0.0   0.0
1    a   NaN   1.0    NaN   1.0
2    a   NaN   NaN    NaN   NaN
3    a   NaN   NaN    3.0   3.0
4    a   4.0   4.0    4.0   4.0
5    a   5.0   5.0    5.0   5.0
6    a   NaN   6.0    NaN   6.0
7    a   NaN   NaN    NaN   NaN
8    a   NaN   NaN    8.0   8.0
9    a   9.0   9.0    9.0   9.0
https://en.xdnf.cn/q/72968.html

Related Q&A

PEP8 for long methods name [duplicate]

This question already has an answer here:How to choose proper variable names for long names in python(1 answer)Closed 5 years ago.What is the PEP8 correct way for long methods name? I have a unit test…

POST method to upload file with json object in python flask app

I am stuck in a problem where I am trying to build single API which will upload file along with json object. I need this API to create webhook.Using multi part, I am able to upload file and in option f…

Django Logging with FileHandler not Working

I am using the logging setup below with a django project (also using sentry/raven). The sentry/raven bit is working fine, but the file logging isnt. An empty logfile is created, but whenever I use logg…

How to directly access a resource in a Py2app (or Py2exe) program?

This is mostly for Py2app, but I plan to also port to Windows so Py2exe is also applicable.For Mac: How can I access the Resources folder of my app bundle from Python code? The ideal way for me would …

Workflow for Python with Docker + IDE for non-web applications

I am currently trying to insert Docker in my Python development workflow of non-web applications.What are the current best practices in Python development using Docker and an IDE? I need the possibili…

Django Deserialization Error Problem installing Fixture

Traceback (most recent call last):File "/Users/sparshkedia/Desktop/task/venv/lib/python3.6/site-packages/django/core/serializers/json.py", line 69, in Deserializeryield from PythonDeserialize…

Python HTTP Exception Handling

Im running a program that downloads files from a web sit. Ive introduced one exception handling urllib.error.HTTPError, but now Im getting from time to time additional errors that Im not sure how to ca…

Weird lambda behaviour in loops [duplicate]

This question already has answers here:What do lambda function closures capture?(8 answers)Closed 10 years ago.I stumbled upon a behaviour in python that I have a hard time understanding. This is the…

Why does inspect return different line for class inheriting from superclass?

While trying to figure out if a function is called with the @decorator syntax, we realized that inspect has a different behaviour when looking at a decorated class that inherits from a superclass.The f…

Sorting a list of tuples with multiple conditions

I am currently trying to sort the following list:list_ = [(1, 0101), (1, 1010), (1, 101), (2, 01), (2, 010), (2, 10)]These are the steps I want to take in order to sort it:Sort the list by the value of…