简体   繁体   中英

How to apply class method correctly?

I describe a class that loads data from server into a dataframe and then processes it. Here is my code (not including importing libraries):

    class Save(Data):
        def __init__(self, server, database, username, driver, group=None):
            super().__init__(server, database, username, driver)
            self.group = group

        def get_all_goods(self):
            goods_table = pd.read_sql_query(f''' SELECT [p_1], [p_2] 
                                  FROM [table] WHERE [group] = '{self.group}' ''',
                                  self.hndl)
            return goods_table

        def data_preprocessing(self):
            data_prepared = self.get_all_goods()
            data_prepared['desc'] = data_prepared[['p_1', 'p_2']].apply(lambda x: ' '.join(x), axis=1)
            return data_prepared

        @staticmethod
        def data_cleaning(str):
            words = []
            str = re.sub(r"(\w*(\.\w*))", ' ', str)
            str = re.sub(r"\d*\_\d*", ' ', str)
            for i in re.split('[;,.,\n,\s,:,-,+,(,),=,/,«,»,@,!,?,",_,*]',str):
                if len(i) > 1:
                    words.append(i)
            return words

        def data_morphy(self, text):
            morph = pymorphy2.MorphAnalyzer()
            tokens = [morph.parse(token)[0].normal_form for token in self.data_cleaning(text) if token != ' ']
            tokens = list(dict.fromkeys(tokens))
            text = ' '.join(tokens)
            return text

        def data_final(self):
            data_final = self.data_preprocessing()
            data_final['desc'] = data_final['desc'].apply(lambda x: self.data_morphy(x))
            return data_final

But when I run the code, it does not complete its work, I waited for half an hour, but the code was not executed. But the methods get_all_goods and data_preprocessing work well. Where did I make a mistake?

EDIT:

There is an error now, when I use cls.data_final() :

    Traceback (most recent call last):
  File "C:\Program Files\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-8-66905faafdf3>", line 103, in <module>
    cls.data_final()
  File "<ipython-input-8-66905faafdf3>", line 68, in data_final
    data_final['desc'] = data_final['desc'].apply(lambda x: self.data_morphy(x))
TypeError: 'method' object is not subscriptable

The name data_final is shared as both the name of the dataframe and the name of the function. When trying to use data_final['desc'] as a pandas dataframe, the code thinks you're trying to call the data_final() method. This is why you're getting the TypeError: 'method' object is not subscriptable

Solution - change the name of your pandas frame to something that isn't the function name, eg:

def data_final(self):
    dont_name_your_variables_the_same_as_your_functions = self.data_preprocessing()
    dont_name_your_variables_the_same_as_your_functions['desc'] = dont_name_your_variables_the_same_as_your_functions['desc'].apply(lambda x: self.data_morphy(x))
    return dont_name_your_variables_the_same_as_your_functions

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM