Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to test if a variable is pd.NaT?

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 :

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

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

like image 929
Clément F Avatar asked Mar 22 '18 17:03

Clément F


People also ask

How do you check if a value is NaT?

To test element-wise for NaT, use the numpy. isnat() method in Python Numpy. It checks the value for datetime or timedelta data type. The condition is broadcast over the input.

What is Panda NaT?

nat means a missing date. Copy. df['time'] = pd. Timestamp('20211225') df. loc['d'] = np.


2 Answers

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

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

This also returns True for None and NaN.

Technically, you could also check for Pandas NaT with 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:

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:

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.isnull works for both Pandas and NumPy NaTs, so it's probably the way to go:

In [34]: pandas.isnull(pandas.NaT) Out[34]: True  In [35]: pandas.isnull(numpy.datetime64('NaT')) Out[35]: True 
like image 94
user2357112 supports Monica Avatar answered Oct 11 '22 12:10

user2357112 supports Monica


pd.NaT is pd.NaT 

True

this works for me.

like image 30
Long Bu Avatar answered Oct 11 '22 14:10

Long Bu