pandas.Series.str.replace returns nan

102 Views Asked by At

I have some text stored in pandas.Series. for example:

df.loc[496]

'therapist and friend died in ~2006 Parental/Caregiver obligations:\n'

I need to replace the number in the text with full date, so I wrote

df.str.replace(
    pat=r'(?:[^/])(\d{4}\b)', 
    repl= lambda m: ''.join('Jan/1/', m.groups()[0]), 
    regex=True
)

but the output is nan; though I tried to test the regular expression using findall, and there is no issue:

df.str.findall(r'(?:[^/])(\d{4}\b)')

496    [2006]

I don't understand what the issue is. most of the problems raised are about cases where Series type is number and not str; but my case is different the data type obviously is str. Nonetheless, I tried .astype(str) and still have the same result nan.

1

There are 1 best solutions below

0
On

A possible solution:

df = pd.Series({496: 'therapist and friend died in ~2006 Parental/Caregiver obligations:\n'})

df.replace(r'~?(\d{4})\b', r'Jan 1, \1', regex=True)

Output:

496    therapist and friend died in Jan 1, 2006 Paren...
dtype: object