-2

I have a function which calculates the Holidays for a given year like this:

holidays = bf.Holidays(year)

the problem is, there is no way to edit the Holidays function so i need another solutions.

I have a datafame with some years, example:

  year
0 2005
1 2011
2 2015
3 2017

right now if i do this:

yearX = year.get_value(0, 0)

and run

holidays = bf.Holidays(yearX)

it just calculates the holidays for the first year in the dataframe (2005)

How can i implement that the function should take every year and append it?

using a for loop?

Example how it works now:

    year = df['YEAR']
    yearX = year.get_value(0,0)
    holidays = bf.Holidays(year)
    holidays = holidays.get_holiday_list()
    print(holidays)

output:

DATE                                 
2005-01-01
2005-03-25
2005-03-27
2005-03-28
2005-05-01

but i want it to calculate for very dataframe row, not only the first one

Henrij
  • 31
  • 6
  • Yes, a for loop might help but it is difficult to help without knowing what your function is doing. Can you post more detail (i.e. the function and a minimal example)? – tnknepp Oct 19 '17 at 14:05
  • @tnknepp i added an example – Henrij Oct 19 '17 at 14:14

2 Answers2

0

Looks like you're looking for pandas.DataFrame.apply:

holidays = df.apply(bf.Holidays, axis=1)

It will apply function bf.Holidays to each row in your df DataFrame.


For the df from your question:

In [50]: df
Out[50]: 
   year
0  2010
1  2011
2  2015
3  2017

In [51]: def test(x):
    ...:     return x % 13
    ...: 

In [52]: df.apply(test, axis=1)
Out[52]: 
   year
0     8
1     9
2     0
3     2
randomir
  • 17,989
  • 1
  • 40
  • 55
  • bf.Holidays need a year as parameter, how should that work? – Henrij Oct 19 '17 at 14:17
  • @Henrij because `.apply` passes it the parameter. Consider `def apply(f, val): return f(val)`, but `df.apply` works on data-frames, column-wise or row-wise – juanpa.arrivillaga Oct 19 '17 at 14:18
  • TypeError: cannot convert the series to – Henrij Oct 19 '17 at 14:19
  • @Henrij, try it on the example from your question. – randomir Oct 19 '17 at 14:22
  • 'year = df['YEAR'] year = year.drop_duplicates() holidays = df.apply(bf.Holidays(), axis=1) print(holidays)' does not work – Henrij Oct 19 '17 at 14:25
  • 1
    Ahh, sorry. the problem is : bf.Holidays takes a year as int and a state_code as second parameter. i thought thats not important for this case to mention it. But its like: bf.Holidays(year,'HH') – Henrij Oct 19 '17 at 14:29
  • 1
    @Henrij, you can pass kwargs like: `df.year.apply(bf.Holidays, state_code='HH')`. – randomir Oct 19 '17 at 14:32
  • yes i did the following `holidays = year.apply(bf.Holidays(), axis=1,state_code='HH') ` but the result is: `TypeError: __init__() missing 2 required positional arguments: 'year' and 'state_code'` – Henrij Oct 19 '17 at 14:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157069/discussion-between-henrij-and-randomir). – Henrij Oct 19 '17 at 14:34
  • @Henrij, don't call `bf.Holidays()`, just pass the function `bf.Holidays` to `apply`. – randomir Oct 19 '17 at 14:40
  • i did. error: `TypeError: __init__() got an unexpected keyword argument 'axis'` – Henrij Oct 19 '17 at 14:41
  • @Henrij, can you please just run the line from my previous comment: `df.year.apply(bf.Holidays, state_code='HH')`? – randomir Oct 19 '17 at 14:53
0

I think you can follow this example and just write a little wrapper function to return the dates to their respective columns:

def holiday_mapper(row):
    holidays = bf.Holidays(row['year'],'HH').get_holiday_list()
    row['holiday1'], row['holiday2']...row['holidayN'] = holidays
    return row

df = df.apply(holiday_mapper, axis=1)

Assuming your get_holiday_list() function actually returns a list, and that you want to store the holiday dates in columns for each holiday, rather than append a single column with all the dates.

jack6e
  • 1,512
  • 10
  • 12