Assign a function as a class method

shantanuo

I want to pass only valid parameters to a function ("read_excel") I tried the following code but getting an error...

import pandas as pd
expected_keys=['io', 'sheet_name','header', 'names', 'index_col', 'usecols', 'squeeze', 'dtype', 'engine', 
               'converters', 'true_values',  'false_values', 'skiprows', 'nrows', 'na_values', 'parse_dates',
               'date_parser', 'thousands', 'comment', 'skipfooter', 'convert_float']

def safe_read_excel(self,  *args, **kwargs):
    if set(kwargs.keys()).difference(set(expected_keys)): 
        raise ValueError('invalid parameter found')
    return self.read_excel(f_name, *args, **kwargs)

pd.safe_read_excel = safe_read_excel

When I use the default "read_excel" method a dataframe is created...

df= pd.read_excel('sales_summary.xlsx', header=0)

But my custom method throws an error...

df= pd.safe_read_excel('sales_summary.xlsx', header=0)

AttributeError: 'str' object has no attribute 'read_excel'

How do I assign my function as pandas method?

FLab

That's because you are writing your safe_read_excel function as a method of a class, while it is a "normal function" (or static method).

In practical words, you do not need self:

def safe_read_excel(f_name,  *args, **kwargs):
    if set(kwargs.keys()).difference(set(expected_keys)): 
        raise ValueError('invalid parameter found')
    return pd.read_excel(f_name, *args, **kwargs)

I changed the first input of the function from self to f_name and changed the return to pd.read_excel

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

PHP: assign function to class method

Assign an imported function as a class method?

Pass the name of the class to a function, assign to a class attribute, outside of a method

Pass function with optional template type param to a class constructor and assign it to a method

assign a function to a method of an instance

Error using functions inside of a class. I can't assign a function to a new class method

Assign an anonymous function to a scala method?

How to assign class method to class attribute?

Assign Python class variable with a method of that class

Assign Function to other class variable

Assign argument function to class member

Assign class variable to function and call

Assign function pointer inside class

Assign inner class function variable to class variable

Can I assign a cython class method to a gsl_function structure via wrapping or cython re-declaration of gsl_function?

How to properly assign type to class method in TypeScript?

Assign class method result to constructor parameter

tkinter (python): assign class method to a key

Assign module method to a Class variable or Instance variable

Cannot Assign to class because it is a method group

Check if a function is a method of a class?

Javascript class, method is not an function

Class function vs method?

How to assign Stream method to a Function in java 8?

How to assign a function to a object method in javascript?

assign function type to an object's method

Dart assign a function/method to variable after declaration

Assign external function to class variable in Python

Python: How to assign function name dynamically in Class