Pandas 0.24 替换正则表达式问题 [英] Pandas 0.24 replace regex issue

查看:74
本文介绍了Pandas 0.24 替换正则表达式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用pandas 0.19.2 python 3.6.0 DataFrame.replace 和字典作用于子字符串(如查找"),Series.replace 也是如此.Pandas 0.24.0 python 3.6.8 似乎作用于 DataFrames 的整个字符串(如match"),并且仍然作用于 Series 的子字符串(如find").

With pandas 0.19.2 python 3.6.0 DataFrame.replace with a dictionary acts on substrings (like "find"), and so does Series.replace. Pandas 0.24.0 python 3.6.8 seems to act on the entire string (like "match") for DataFrames, and still act on substrings for Series (like "find").

df = pd.DataFrame({'c1':['AD','BD'],'c2':['AD','BD']})
print(df)
print(df.replace(to_replace={'c1':{r'D': ''}, 'c2':{r'BD': ''}},regex=True))
print(df.replace(to_replace={r'D': ''},regex=True))
print(df['c1'].replace(to_replace=r'D', value='',regex=True))

Pandas 0.19.2 生成(我添加了一些空行以提高可读性):

Pandas 0.19.2 produces (I added some blank lines for legibility):

   c1  c2
0  AD  AD
1  BD  BD

  c1  c2
0  A  AD
1  B    

  c1 c2
0  A  A
1  B  B

0    A
1    B
Name: c1, dtype: object

使用 Pandas 0.24.0:

With Pandas 0.24.0:

   c1  c2
0  AD  AD
1  BD  BD

   c1  c2
0  AD  AD
1  BD    

   c1  c2
0  AD  AD
1  BD  BD

0    A
1    B
Name: c1, dtype: object

对我来说看起来像熊猫虫,还是我遗漏了什么?

Looks like a pandas bug to me, or am I missing something?

推荐答案

该错误在 修复了 Pandas 0.24.2 的回归:

修复了 DataFrame.replace() 其中 regex=True 仅替换匹配字符串开头的模式(GH25259)

Fixed regression in DataFrame.replace() where regex=True was only replacing patterns matching the start of the string (GH25259)

如你所见,只有

print(df.replace(to_replace={'c1':{r'D': ''}, 'c2':{r'BD': ''}},regex=True))
print(df.replace(to_replace={r'D': ''},regex=True))

没有正常工作.现在,问题已解决.

did not work correctly. Now, the issue is fixed.

这篇关于Pandas 0.24 替换正则表达式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆