首页 \ 问答 \ pd.notnull的奇怪的空检查行为(Weird null checking behaviour by pd.notnull)

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.notnullnp.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, and pd.notnull or np.isnan will work directly:

>>> pd.notnull(np.array(x, dtype=object))
Out[151]: array([True,  False])

I imagine that pd.notnull directly converts x 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
更新时间:2022-07-26 22:07

最满意答案

解决方案是生成过滤的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

相关问答

更多

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的