pd.notnull的奇怪的空检查行为(Weird null checking behaviour by pd.notnull)
这基本上是对我的答案内容的重新阐述。
在尝试使用
pd.notnull
解决这个问题时 ,我遇到了一些奇怪的行为。考虑
x = ('A4', nan)
我想检查这些项中的哪些是null。 直接使用
np.isnan
会抛出一个TypeError(但我已经弄清楚如何解决)。使用
pd.notnull
不起作用。>>> pd.notnull(x) True
它将元组视为单个值(而不是可迭代的值)。 此外,将其转换为列表然后测试也会给出错误的答案。
>>> pd.notnull(list(x)) array([ True, True])
由于第二个值是
nan
,我正在寻找的结果应该是[True, False]
。 它最终适用于您预转换为系列时:>>> pd.Series(x).notnull() 0 True 1 False dtype: bool
因此,解决方案是Series-ify,然后测试值。
沿着类似的路线,另一个(公认的
pd.notnull
)解决方案是预先转换为object
pd.notnull
numpy数组,pd.notnull
或np.isnan
将直接工作:>>> pd.notnull(np.array(x, dtype=object)) Out[151]: array([True, False])
我想
pd.notnull
直接将x
转换为封面下的字符串数组,将NaN渲染为字符串“nan”,因此它不再是“null”值。
pd.notnull
在这里做同样的事情吗? 或者我应该注意哪些内容正在进行?笔记
In [156]: pd.__version__ Out[156]: '0.22.0'
This is essentially a rehashing of the content of my answer here.
I came across some weird behaviour when trying to solve this question, using
pd.notnull
.Consider
x = ('A4', nan)
I want to check which of these items are null. Using
np.isnan
directly will throw a TypeError (but I've figured out how to solve that).Using
pd.notnull
does not work.>>> pd.notnull(x) True
It treats the tuple as a single value (rather than an iterable of values). Furthermore, converting this to a list and then testing also gives an incorrect answer.
>>> pd.notnull(list(x)) array([ True, True])
Since the second value is
nan
, the result I'm looking for should be[True, False]
. It finally works when you pre-convert to a Series:>>> pd.Series(x).notnull() 0 True 1 False dtype: bool
So, the solution is to Series-ify it and then test the values.
Along similar lines, another (admittedly roundabout) solution is to pre-convert to an
object
dtype numpy array, andpd.notnull
ornp.isnan
will work directly:>>> pd.notnull(np.array(x, dtype=object)) Out[151]: array([True, False])
I imagine that
pd.notnull
directly convertsx
to a string array under the covers, rendering the NaN as a string "nan", so it is no longer a "null" value.Is
pd.notnull
doing the same thing here? Or is there something else going on under the covers that I should be aware of?Notes
In [156]: pd.__version__ Out[156]: '0.22.0'
原文:https://stackoverflow.com/questions/51035790
最满意答案
解决方案是生成过滤的QuerySet并将其作为内容传递给表。
以下是
view.py
样子:from django_tables2 import RequestConfig from student_upload.models import ExtendedUser from admin.tables import ExtendedUserTable def index(request): p = ExtendedUser.objects.get_query_set() table = ExtendedUserTable(p.filter(is_admin = False)) RequestConfig(request).configure(table) return render(request, 'admin/index.html', {'table': table})
tables.py
没有变化。class ExtendedUserTable(tables.Table): class Meta: model = ExtendedUser
The solution is to generate a filtered QuerySet and pass it into to the table as content.
Here is how the
view.py
looks like:from django_tables2 import RequestConfig from student_upload.models import ExtendedUser from admin.tables import ExtendedUserTable def index(request): p = ExtendedUser.objects.get_query_set() table = ExtendedUserTable(p.filter(is_admin = False)) RequestConfig(request).configure(table) return render(request, 'admin/index.html', {'table': table})
The
tables.py
is unchanged.class ExtendedUserTable(tables.Table): class Meta: model = ExtendedUser
相关问答
更多-
df.assign(MAX_SUB_ID=df.SUB_ID.mul(df.COND).groupby(df.ID).transform('max')) ID SUB_ID COND MAX_SUB_ID 1 101 1 1 3 2 101 2 1 3 3 101 3 1 3 4 102 1 1 1 5 102 ...
-
同步提供程序将(作为配置步骤的一部分)自动创建逻辑删除表和触发器以跟踪行删除。 当行未被删除但以这种方式更新时,如果超出范围,则自动生成的模式不会将这些记录为删除。 它会将它们记录为更新。 因此,为了扩展Microsoft示例,假设您的应用程序仅将华盛顿数据同步到华盛顿销售代表。 最初作为华盛顿销售进入的一些销售被更正并移至俄勒冈州。 同步框架将不知道它应该从华盛顿代表的本地数据库中删除这些现在的俄勒冈州记录。 你有几个选择来解决这个问题: 修改配置工具以生成将处理该情况的触发器,而不是不生成的默认触发器。 ...
-
如果你需要检查所有的对(即没有对称用于减少问题),那么最简单的可能是Select和Tuples : allPairs[a_,b_,cond_]:=Select[Tuples@{a,b},cond@@#&]; 哪些做我认为你想要的: a=Range[4]; b=Range[3,5]; allPairs[a,b,EvenQ[#1+#2]&] Out[37]= {{1,3},{1,5},{2,4},{3,3},{3,5},{4,4}} 至于更多用于生成配对的工具,请查阅Tuples和Outer : Tuple ...
-
尝试这个: SELECT * FROM A WHERE m_active='N' AND NOT EXISTS ( SELECT * FROM B WHERE B.m_id=A.m_id AND B.v_active<>'N' ); Try this: SELECT * FROM A WHERE m_active='N' AND NOT EXISTS ( SELECT * FROM B WHERE B.m_id=A.m_id AND B.v_acti ...
-
解决方案是生成过滤的QuerySet并将其作为内容传递给表。 以下是view.py样子: from django_tables2 import RequestConfig from student_upload.models import ExtendedUser from admin.tables import ExtendedUserTable def index(request): p = ExtendedUser.objects.get_query_set() table = Ex ...
-
使用sum和一个生成器表达式: sum(n == fresh for n in buckets['actual'][e]) 如True == 1和False == 0 ,所以不需要。 相关阅读: Pythonic是否使用bools作为整数? , 在Python中是False == 0和True == 1是实现细节还是由语言保证? Use sum with a generator expression: sum(n == fresh for n in buckets['actual'][e]) as Tr ...
-
没有标准的高效LINQ解决方案。 我会使用这样的自定义扩展“LINQ like”方法: public static class EnumerableExtensions { public static IEnumerable
SkipLastWhile (this IEnumerable source, Func predicate) { var skipBuffer = new List (); foreach (var ... -
如何计算满足关联模型中条件的记录?(How do I count records that satisfy a condition in a associated model?)[2021-06-14]
Jiraissue.joins(:severities).where(:severities => {:severity => "S1"}).count Jiraissue.joins(:severities).where(:severities => {:severity => "S1"}).count -
您没有指定DBMS,但大多数DBMS都有公用表表达式(CTE)和日期函数。 CTE找到满足您标准的第一个间隔(前3小时,所有测量> 150)。 最终选择为您提供统计信息 with interval as ( select Min(recordTime) [Start], from [YourTable] T1 where not exists ( select 1 from [YourTable ...
-
阅读完评论后,如果您的计数器返回0并且脚本对象是XmlDocument,则问题显然是您的xml的负载...检查一下,它适用于我: var script = new XmlDocument(); script.LoadXml("
test "); XmlNodeLi ...