A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pandas-dev/pandas/issues/15781 below:

Return value of boolean datetime fields of NaT: False or NaN ? · Issue #15781 · pandas-dev/pandas · GitHub

From discussion in #15589, the handling of NaT is different for different datetime fields.

On a scalar value, is_leap_year returns False, the others are not defined:

In [1]: pd.Timestamp('NaT').is_month_start
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-654cc4e613aa> in <module>()
----> 1 pd.Timestamp('NaT').is_month_start

AttributeError: 'NaTType' object has no attribute 'is_month_start'

In [2]: pd.Timestamp('NaT').is_leap_year
Out[2]: False

On an index (or series .dt accessor), is_leap_year returns False, the others propagate NaN (with the consequence it is no longer a boolean array/series):

In [3]: pd.DatetimeIndex(['2012-01-01', 'NaT']).is_month_start
Out[3]: array([  1.,  nan])

In [4]: pd.DatetimeIndex(['2012-01-01', 'NaT']).is_leap_year
Out[4]: array([ True, False], dtype=bool)

So when is_leap_year was introduced in #13739, this was done on purpose, citing @sinhrks "pd.NaT.is_leap_year results in False, as I think users want bool array.".

This seems indeed the more logical thing to return (certainly for arrays/series), so you can eg use it for indexing. For scalars it is a bit less clear what is the preferable option (return False or NaN), but probably best to be consistent.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4