Skip to content

Return value of boolean datetime fields of NaT: False or NaN ? #15781

Closed
@jorisvandenbossche

Description

@jorisvandenbossche

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions