Python : Return the missing weekdays dates and assign rate next to missing date

2024/11/15 19:39:56
Dates       rates
7/26/2019   1.04
7/30/2019   1.0116
7/31/2019   1.005
8/1/2019    1.035
8/2/2019    1.01
8/6/2019    0.9886
8/7/2019    1.0048
8/8/2019    0.97
8/9/2019    0.9659
8/12/2019   0.965

In the above dataframe, 29th July 2019 and 5th August 2019 are missing. These are weekdays. I need to populate the missing weekdays dates and assign the 'rate' which is next to missing date. For example: Assign the 30th july 2019 'rate' to the missing 29th july 2019 as well. Please suggest. Thanks

Answer

You can use for example pd.offsets.BDay() to get next business day.

Initial dataframe (the Dates column is of DateTime type):

       Dates   rates
0 2019-07-26  1.0400
1 2019-07-30  1.0116
2 2019-07-31  1.0050
3 2019-08-01  1.0350
4 2019-08-02  1.0100
5 2019-08-06  0.9886
6 2019-08-07  1.0048
7 2019-08-08  0.9700
8 2019-08-09  0.9659
9 2019-08-12  0.9650

Then:

df = df.merge(pd.DataFrame({'Dates':df['Dates'] + pd.offsets.BDay()}), on='Dates', how='outer').sort_values('Dates').bfill().dropna().reset_index(drop=True)print(df)

Prints:

        Dates   rates
0  2019-07-26  1.0400
1  2019-07-29  1.0116
2  2019-07-30  1.0116
3  2019-07-31  1.0050
4  2019-08-01  1.0350
5  2019-08-02  1.0100
6  2019-08-05  0.9886
7  2019-08-06  0.9886
8  2019-08-07  1.0048
9  2019-08-08  0.9700
10 2019-08-09  0.9659
11 2019-08-12  0.9650
https://en.xdnf.cn/q/119556.html

Related Q&A

How do I use a list or set as keys in file renaming

Is something like this possible? Id like to use a dictionary or set as the key for my file renamer. I have a lot of key words that id like to filter out of the file names but the only way iv found to …

How to change Python comment font style in the latest VS Code? [duplicate]

This question already has an answer here:How to change the font-style of code comments in vscode?(1 answer)Closed 6 months ago.Seems like with the latest VS Code update, all the comment font style has…

How to crop an image based on a complex criteria?

I have a set of similar images like the one below. I want to keep the portion of the image that is within the top red irregular rectangle (green arrows represent the space that I want to keep; anything…

Getting current video tag URL with selenium

Im trying to get the current html5 video tag URL using selenium (with python bindings):from selenium import webdriverdriver = webdriver.Chrome() driver.get(https://www.youtube.com/watch?v=9x6YclsLHN0)…

How to determine if two rows are identical (similar) if row 2 contains part of the info from row 1?

Hope you are having a good day. I am currently working with an extremely dirty dataframe containing First Name, Last Name, and Middle Name. One the issues that I am trying to resolve looks like below:F…

Cartopy fancy box

Hello I have been trying to plot data in a Orthographic projection. The data is plotted but I want the box to follow the data limits. Like in this example I am sharing form M_map[enter image descriptio…

discord.py - No DM sent to the user

I am making a discord.Client. I have a DM command that sends a DM to a specific user, but no message is sent to the user when the command is run, but a message is sent on the Context.channel. Here is m…

Improve CPU time of conditional statement

I have written an if-elif statement, which I believe not be very efficient:first_number = 1000 second_number = 700 switch = {upperRight: False,upperLeft: False,lowerRight: False,lowerLeft: False,middle…

Why no colon in forming a list from loop in one line in Python?

From this website, there is a way to form a list in Python from loop in one line squares = [i**2 for i in range(10)]My question is, typically, after a loop, there is a colon, e.g., squares = [] for i i…

Merge each groups rows into one row

Im experienced with Pandas but stumbled upon a problem that I cant seem to figure out. I have a large dataset ((40,000, 16)) and I am trying to group it by a specific column ("group_name" for…