ENH: add string method remove prefix and suffix, python 3.9 · Issue #36944 · pandas-dev/pandas (original) (raw)

Opening this issue for discussion:

Since python 3.9 is out, and we have the new string methods removeprefix and removesuffix, it would be nice to add them to the pandas string methods as well

In [2]: import pandas as pd

In [3]: df = pd.DataFrame({'A': ['str_string1', 'str_string2', 'str_string3']})

In [4]: print(df) A 0 str_string1 1 str_string2 2 str_string3

In [5]: df['A'].str.removeprefix('str_') Out[5]: 0 string1 1 string2 2 string3 Name: A, dtype: object

An argument not to add this is that it's pretty easily to achieve with str.split:

df['A'].str.split('_').str[-1]