0

i have a data frame

    sec    date     l1  l2 
0   abc 2001-03-27  12  31
1   abc 2001-03-29  13  32
2   abc 2001-04-03  14  33
3   abc 2001-05-04  15  34
4   abc 2001-07-05  16  35
5   abc 2001-07-06  17  36
6   abc 2001-08-07  18  37
7   abc 2001-09-08  19  38
8   abc 2001-10-09  20  39
9   abc 2001-11-10  21  40
10  abc 2001-12-11  22  41

I want to write a function that would take 2 parameters say flexible_date_adder(data, no_of_years_to_add=[1,4])

Expectation: If I pass my data frame, I should get 2 columns named "date1yr" and "date4yr" that would add 1year and 4 years to my date and give the result

Expected Output:

    sec    date     l1  l2  date1yr      date4yr   
0   abc  2001-03-27 12  31  2002-03-27  2005-03-27

for all the data frame dates.
Thank you

Amit
  • 763
  • 1
  • 5
  • 14
  • 1
    Does this answer your question? [Subtract a year from a datetime column in pandas](https://stackoverflow.com/questions/31169774/subtract-a-year-from-a-datetime-column-in-pandas) – mpx Mar 05 '21 at 05:00

2 Answers2

1

try:

df.date = pd.to_datetime(df.date)

def flexible_date_adder(data, no_of_years_to_add):
    add_year = {}
    for y in no_of_years_to_add:
         add_year[f"year{y}"] = (data.date+pd.DateOffset(years=y))
    return (add_year)

no_of_years_to_add=[1,4]
df = pd.concat([df,pd.DataFrame(df.apply(flexible_date_adder, no_of_years_to_add=[1,4], axis=1).tolist())],axis=1)

df:

sec date l1 l2 year1 year4
0 abc 2001-03-27 12 31 2002-03-27 2005-03-27
1 abc 2001-03-29 13 32 2002-03-29 2005-03-29
2 abc 2001-04-03 14 33 2002-04-03 2005-04-03
3 abc 2001-05-04 15 34 2002-05-04 2005-05-04
4 abc 2001-07-05 16 35 2002-07-05 2005-07-05
5 abc 2001-07-06 17 36 2002-07-06 2005-07-06
6 abc 2001-08-07 18 37 2002-08-07 2005-08-07
7 abc 2001-09-08 19 38 2002-09-08 2005-09-08
8 abc 2001-10-09 20 39 2002-10-09 2005-10-09
9 abc 2001-11-10 21 40 2002-11-10 2005-11-10
10 abc 2001-12-11 22 41 2002-12-11 2005-12-11
Pygirl
  • 12,969
  • 5
  • 30
  • 43
0

Though a bit late, I will code it this way to let the function handle all the things:

def flexible_date_adder(df1, no_of_years_to_add=None):
    if no_of_years_to_add is None:
        no_of_years_to_add = [1, 4]
 
    for year in no_of_years_to_add:
        df1[f'date{year}yr'] = df1['date'] + pd.DateOffset(years=year)

    return df1

Test Run:

df['date'] = pd.to_datetime(df['date'])
df = flexible_date_adder(df)

Output:
    sec       date  l1  l2    date1yr    date4yr
0   abc 2001-03-27  12  31 2002-03-27 2005-03-27
1   abc 2001-03-29  13  32 2002-03-29 2005-03-29
2   abc 2001-04-03  14  33 2002-04-03 2005-04-03
3   abc 2001-05-04  15  34 2002-05-04 2005-05-04
4   abc 2001-07-05  16  35 2002-07-05 2005-07-05
5   abc 2001-07-06  17  36 2002-07-06 2005-07-06
6   abc 2001-08-07  18  37 2002-08-07 2005-08-07
7   abc 2001-09-08  19  38 2002-09-08 2005-09-08
8   abc 2001-10-09  20  39 2002-10-09 2005-10-09
9   abc 2001-11-10  21  40 2002-11-10 2005-11-10
10  abc 2001-12-11  22  41 2002-12-11 2005-12-11

Some additional note:

As I seen you set default list for the list parameter in your sample function layout, I add this side note for your reference.

Never provide a default list for list parameter at function argument. Set it to None and init inside function instead. Otherwise, the "Mutable Defaults" will cause unpredictable error when the function is run more than once.

For example:

    def something(x=[]):
        x.append(1)
        print (x)

    >>> something()
    [1]

    >>> something()
    [1, 1]

    >>> something()
    [1, 1, 1]
SeaBean
  • 22,547
  • 3
  • 13
  • 25