[英]Pandas “fillna” ignores the “inplace=True” keyword
我有一个DataFrame
(相当大,难以再现&c),为此我观察到以下行为:
>>> df.info(verbose=True,memory_usage=True,null_counts=True)
<class 'pandas.core.frame.DataFrame'>
Int64Index: 49841 entries, 0 to 49878
Data columns (total 70 columns):
...
channel 25101 non-null object
...
dtypes: bool(10), datetime64[ns](6), float64(2), int64(32), object(20)
memory usage: 23.7+ MB
>>> df.channel.fillna("Unknown",inplace=True)
>>> df.info(verbose=True,memory_usage=True,null_counts=True)
<class 'pandas.core.frame.DataFrame'>
Int64Index: 49841 entries, 0 to 49878
Data columns (total 70 columns):
...
channel 25101 non-null object
...
dtypes: bool(10), datetime64[ns](6), float64(2), int64(32), object(20)
memory usage: 23.7+ MB
IOW,似乎df.channel.fillna("Unknown",inplace=True)
无效。
怎么可能? 这是错误吗? 我究竟做错了什么?!
PS。 评论摘要:
df.is_copy
为“ None
df._is_view
为False
channel
是一列,而不是属性,因为它按info
列出 从文档中 :
您可以使用属性访问来修改Series或DataFrame列的现有元素,但要小心; 如果您尝试使用属性访问来创建新列,则它会静默失败,从而创建新属性而不是新列。
我们怀疑您先分配了df.channel
,然后分配了df['channel']
,这会导致意外的行为。
原来原因是以下sqlalchemy
查询:
select *
from table1
join table2
on table1.id = table2.id
生成的DF有两列,分别为id
和总破坏。
解:
select *
from table1
join (select id as id2, ... from table2) t2
on table1.id = t2.id2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.