Fetch the value for the latest date in the index row

Question:

my table looks something like this:

Sector 4/1/2022 5/1/2022 6/1/2022 1Y Min
A 10 05 12 05
B 18 20 09 09
C 02 09 12 02

I want to add a new column "Bps away from 1Y Min" such that values of the new column is calculated using the formula: (Value as of the latest date – 1Y Min)
I want to keep the latest date column dynamic within the formula such that it gets updated whenever a column with a new date is available.

For eg:-

Sector 4/1/2022 5/1/2022 6/1/2022 1Y Min Bps away from 1Y Min
A 10 05 12 05 7
B 18 20 09 09 0
C 02 09 12 02 10
Asked By: Saanchi

||

Answers:

You can use a helper function to find the max date from the column names (and assigning suitably low values for non-dates), and then it’s just pandas.

import datetime
def dt_helper(dt_string):
    try:
        return datetime.datetime.strptime(dt_string, '%d/%m/%Y').date()
    except:
        return datetime.date(1900,1,1)

df['Bps away from 1Y Min'] = df[max(df.columns, key=dt_helper)] - df['1Y Min']

It’s not clear if your dates are dd/mm/yyyy or mm/dd/yyyy, I’ve assumed the former, but if the latter then change the date pattern to '%m/%d/%Y'

Answered By: bn_ln
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.