pandas-dev / pandas

Flexible and powerful data analysis / manipulation library for Python, providing labeled data structures similar to R data.frame objects, statistical functions, and much more
https://pandas.pydata.org
BSD 3-Clause "New" or "Revised" License
43.29k stars 17.8k forks source link

[FEATURE] Ability to pass regex module flags in DataFrame.replace while regex=True #32233

Open vishnu-dev opened 4 years ago

vishnu-dev commented 4 years ago

Code Sample

# For ignoring case, I have to explicitly change the regex
replace_map = {
    '[Q|q]uick[P|p]ay with [Z|z]elle payment to ': 'Payment to',
    '[Q|q]uick[P|p]ay with [Z|z]elle payment from ': 'Payment from'
}
df.replace({'title': replace_map}, regex=True, inplace=True)

Problem description

According to pandas docs for DataFrame.replace the replace has ability to take regex input, but doesn't have the ability to take in regex flags such as re.I or re.IGNORECASE for case insensitive search. Can we have a flags argument to take in regex module flags as input.

Expected Output

replace_map = {
    'QuickPay with Zelle payment to ': 'Payment to',
    'QuickPay with Zelle payment from ': 'Payment from'
}
# Can't we include flags as argument?
df.replace({'title': replace_map}, regex=True, inplace=True, flags=re.I) 
jbrockmendel commented 4 years ago

I'm not sure off the top of my head, but I think you can pass compiled regexp objects, which might do the trick?

rootsmusic commented 9 months ago

@vishnu-dev I assume that this feature wouldn't be possible until there's an enhancement made in issue #22496.