Home > OS >  Python FutureWarning. new syntax?
Python FutureWarning. new syntax?

Time:07-30

Python swears at my syntax, says soon it will be impossible to write like that. Can you please tell me how to change the function?

def cleaning_name1(data):
    """Cleaning name1 minus brand   space articul   art   round brackets """
    data['name1'] = data['name1'].str.split('артикул').str[0]
    data['name1'] = data['name1'].str.split('арт').str[0]
    data['name1'] = (data['name1'].str.replace('brand ', '', )
                     .str.replace(' ', '', ).str.replace('(', '', ).str.replace(')', '', ))
    return data

CodePudding user response:

Currently, .str.replace() defaults to regex=True. This is planned to change to regex=False in the future. You should make this explicit in your calls.

    data['name1'] = data['name1'].str.replace('brand ', '', regex=False)
                     .str.replace(' ', '', regex=False).str.replace('(', '', regex=False ).str.replace(')', '', regex=False)

Although in your case, it would be better to use a regular expression, so you can do all the replacements in a single call:

    data['name1'] = data['name1'].str.replace('brand|[ ()]', '', regex=True)
  • Related