Python Forum
How to apply a class method to an entire dataframe column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to apply a class method to an entire dataframe column
How can i apply a class method to an entire dataframe?

A minimal code is here for the problem:

I have a Price time series dataframe df with these columns column 1 date is a data object, other columns are float:

Date | Open | High | Low | Close
I have a method of a class as below:

class MyScanner():
def __init__(self, df, scandate):
self.df = df
self.mydate = scandate

#get the index to current date
self.currentindex = df[df['Date'] == scandate].index[0]

#Next Date High Low
self.d1 = self.df[self.df.index == self.currentindex+1]
self.h1 = self.d1['High'].max()
self.l1 = self.d1['Low'].min()

#Similarly assigned h2,l2,h3,l3

# stock price have made lower high and lower lows for next 3 days
def isbearish():
if (self.h1 > self.h2 > self.h3) &\
(self.l1 > self.l2 > self.l3):
return True

# stock have made lower high and lower lows for next 3 days
def isbullish():
if (self.h1 < self.h2 < self.h3) &\
(self.l1 < self.l2 < self.l3):
return True

# similar other functions which uses next 3 days high lows logic
def issomething():
..check something..
return True
So now I can run this for a particular date:

MyScanner(df, Date).isbullish()
This will return True if the next 3 'Date' matches the called method condition.

Now my query is I want to create a new Column called 'ISBullish', and apply this above method for respective dates in the Date Column to the whole data frame. This new Column will have either True or None, based on next 3 days data.

I am stuck with the syntax to be passed to the apply method:

df['ISBullish'] = df.Date.apply(MyClass(df, somedate).isbullish())
Hi @tirtha9,

Good day!

Hope the below answer helps

df2['ISBearish'] = df2['Date'].apply(lambda x: (Scanner(df2, x).isbearish2()))
Date Open High Low Close ISBearish 0 2020-01-31 788.00 791.50 764.00 767.1 True 1 2020-02-01 771.85 798.00 728.40 742.8 None 2 2020-02-03 754.00 794.95 752.45 785.7 None 3 2020-02-04 784.70 794.50 773.60 776.9 None

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get OpenCV to display entire camera frame? George713 1 233 Aug-12-2021, 02:45 AM
Last Post: Pedroski55
  Apply fillna to multiple columns in dataframe rraillon 2 279 Aug-05-2021, 01:11 PM
Last Post: rraillon
  anonymous method in a class Skaperen 8 1,033 May-23-2021, 11:17 PM
Last Post: Skaperen
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 480 May-04-2021, 10:51 PM
Last Post: rhat398
  How to get value in Dataframe given row & column values? moonlight 1 645 Apr-26-2021, 09:30 PM
Last Post: Larz60+
  IF statement to apply at each date illmattic 2 914 Apr-08-2021, 12:31 PM
Last Post: illmattic
  python tool to collect the time measurement of entire code maiya 3 682 Feb-12-2021, 05:39 PM
Last Post: BashBedlam
  Adding a new column to a dataframe lokhtar 2 615 Jan-14-2021, 07:18 PM
Last Post: buran
  Function won't apply dynamically in timeseries illmattic 1 484 Jan-08-2021, 03:15 PM
Last Post: stullis
  2d Array adds last element to entire list waiteup 2 581 Nov-19-2020, 08:25 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020