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

Viewed 96

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.

Current code is as follows:

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

This should work too:

pd.to_numeric(test_df['Season'].str.split('-').str[0]) + 1

Output:

0     1997
1     1998
2     1999
3     2000
4     2001
5     2002
6     2003
7     2004
8     2005
9     2006
10    2007
11    2008
12    2009
13    2010
14    2011
15    2012

You can use .str.extract to extract the first four digits

df['Season'] = df['Season'].str.extract('^(\d{4})').astype(int).add(1)
    Season  Height  Weight
0     1997       4      22
1     1998      18       4
2     1999      19      27
3     2000       7      10
4     2001      19       9
5     2002      18      31
6     2003      19       9
7     2004      18      29
8     2005      13      17
9     2006      13      30
10    2007       5      14
11    2008      15       3
12    2009      13      10
13    2010      15       8
14    2011       0      23
15    2012       2      38

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)

I use the fiscalyear module.

import numpy as np
import pandas as pd
import fiscalyear as fy

...

test_df['Season'] = test_df['Season'].apply(lambda x : fy.FiscalYear(int(x[0:4]) + 1).fiscal_year)
print(test_df)
Related