Pandas: df.index.str.extract() not working like re.findall()

Question:

I am trying to use df.<column_name or index>.str.extract(r'd+') on the following:

28    USD-ABC-S__7y
29    USD-ABC-S__8y
Name: InstrumentIdentifier, dtype: object

This is giving me a ValueError though:

ValueError: pattern contains no capture groups

However, when I try:

import re
re.findall(r'd+', 'USD-ABC-S__8y')

this returns the expected result: [8]

Any ideas why the Pandas version isn’t giving the same result as re?

Thx

Asked By: keynesiancross

||

Answers:

You need to wrap the regex in a capturing group ((...)):

df.<column_name or index>.str.extract(r'(d+)')

Example:

df['InstrumentIdentifier'].str.extract(r'(d+)', expand=False)

output:

28    7
29    8
Name: InstrumentIdentifier, dtype: object
Answered By: mozway
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.