pandas 如何测试变量是否为 pd.NaT?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49435317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:50:02  来源:igfitidea点击:

how to test if a variable is pd.NaT?

pandasunit-testingdatetimenan

提问by Clément F

I'm trying to test if one of my variables is pd.NaT. I know it is NaT, and still it won't pass the test. As an example, the following code prints nothing :

我正在尝试测试我的变量之一是否是 pd.NaT。我知道它是 NaT,但它仍然无法通过测试。例如,以下代码不打印任何内容:

a=pd.NaT

if a == pd.NaT:
    print("a not NaT")

Does anyone have a clue ? Is there a way to effectively test if ais NaT?

有人有线索吗 ?有没有办法有效地测试是否a是 NaT?

回答by user2357112 supports Monica

Pandas NaTbehaves like a floating-point NaN, in that it's not equal to itself. Instead, you can use pandas.isnull:

Pandas 的NaT行为就像一个浮点数NaN,因为它不等于自身。相反,您可以使用pandas.isnull

In [21]: pandas.isnull(pandas.NaT)
Out[21]: True

This also returns Truefor None and NaN.

这也返回TrueNone 和 NaN。

Technically, you could also check for Pandas NaTwith x != x, following a common pattern used for floating-point NaN. However, this is likely to cause issues with NumPy NaTs, which look very similar and represent the same concept, but are actually a different type with different behavior:

从技术上讲,你也可以检查大熊猫NaTx != x,以下用于浮点NaN的一种常用模式。但是,这可能会导致 NumPy NaT 出现问题,它们看起来非常相似并代表相同的概念,但实际上是具有不同行为的不同类型:

In [29]: x = pandas.NaT

In [30]: y = numpy.datetime64('NaT')

In [31]: x != x
Out[31]: True

In [32]: y != y
/home/i850228/.local/lib/python3.6/site-packages/IPython/__main__.py:1: FutureWarning: In the future, NAT != NAT will be True rather than False.
  # encoding: utf-8
Out[32]: False

numpy.isnat, the function to check for NumPy NaT, also fails with a Pandas NaT:

numpy.isnat,检查 NumPy 的函数NaT,也因 Pandas 失败NaT

In [33]: numpy.isnat(pandas.NaT)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-33-39a66bbf6513> in <module>()
----> 1 numpy.isnat(pandas.NaT)

TypeError: ufunc 'isnat' is only defined for datetime and timedelta.

pandas.isnullworks for both Pandas and NumPy NaTs, so it's probably the way to go:

pandas.isnull适用于 Pandas 和 NumPy NaTs,所以这可能是要走的路:

In [34]: pandas.isnull(pandas.NaT)
Out[34]: True

In [35]: pandas.isnull(numpy.datetime64('NaT'))
Out[35]: True

回答by Lukas

You can also use pandas.isna() for pandas.NaT, numpy.nan or None:

您还可以将 pandas.isna() 用于 pandas.NaT、numpy.nan 或 None:

import pandas as pd
import numpy as np

x = (pd.NaT, np.nan, None)
[pd.isna(i) for i in x]

Output:
[True, True, True]

回答by Long Bu

pd.NaT is pd.NaT

True

真的

this works for me.

这对我有用。

回答by Max Ghenis

If it's in a Series(e.g. DataFramecolumn) you can also use .isna():

如果它在Series(例如DataFrame列)中,您还可以使用.isna()

pd.Series(pd.NaT).isna()
# 0    True
# dtype: bool