Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to extract the last year (YYYY) from a YYYY-YY format column in Pandas

I am trying to extract the last year (YY) of a fiscal date string in the format of YYYY-YY. e.g The last year of this ‘1999-00’ would be 2000.

Current code seems to cover most cases other than this.

import pandas as pd
import numpy as np


test_df = pd.DataFrame(data={'Season':['1996-97', '1997-98', '1998-99', 
'1999-00', '2000-01', '2001-02',
'2002-03','2003-04','2004-05', 
'2005-06','2006-07','2007-08', 
'2008-09', '2009-10', '2010-11', '2011-12'],
'Height':np.random.randint(20, size=16), 
'Weight':np.random.randint(40, size=16)})

I need a logic to include a case where if it is the end of the century then my apply method should add to the first two digits, I believe this is the only case I am missing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Current code is as follows:

test_df['Season'] = test_df['Season'].apply(lambda x: x[0:2] + x[5:7])

>Solution :

Here you go! Use the following function instead of the lambda:

def get_season(string):
    century = int(string[:2])
    preyear = int(string[2:4])
    postyear = int(string[5:7])
    if postyear < preyear:
        century += 1
    # zfill is so that "1" becomes "01"
    return str(century).zfill(2) + str(postyear).zfill(2)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading