WalksB
WalksB

Reputation: 519

Why does pandas NaT support date() method but not time() method?

I can't seem to comprehend the following behavior, why does one method break and the other doesn't?

In [1]: import pandas as pd                                                     

In [2]: pd.NaT                                                                  
Out[2]: NaT

In [3]: pd.NaT.date()                                                           
Out[3]: NaT

In [4]: pd.NaT.time()                                                           
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-19f140eaf187> in <module>
----> 1 pd.NaT.time()

pandas/_libs/tslibs/nattype.pyx in pandas._libs.tslibs.nattype._make_error_func.f()

ValueError: NaTType does not support time

In [5]:                                     

Upvotes: 1

Views: 1082

Answers (1)

filbranden
filbranden

Reputation: 8898

This has been discussed in the Pandas project before.

See, for example, this comment in PR #10372, which implements the code that makes date() return NaT rather than raise an error:

I think things that have date should be NaT eg date,now,today

Agreed. NaT is a datetime instance and also a date instance.

I am ok with time be Nat as well -even though it pushes the semantic boundaries

I disagree. NaT is not a time instance. We should not return it from a method which promises to return a time.

So it boils down to NaT's type implementing the same interfaces as Python's datetime and date types, but not the time type.

This is natural, since Pandas timestamps can represent a full timestamp (with date and time) or a date, but not a time independent of a date.

For that reason, the few functions that convert or return objects of those two types will return NaT, while most others will raise an error.

Upvotes: 2

Related Questions