Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jorisvandenbossche opened this issue Mar 22, 2017 · 1 comment · Fixed by #15782
Closed

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

jorisvandenbossche opened this issue Mar 22, 2017 · 1 comment · Fixed by #15782
Labels
API Design Datetime Datetime data dtype
Milestone

Comments

@jorisvandenbossche
Copy link
Member

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.

@jreback
Copy link
Contributor

jreback commented Mar 22, 2017

PR on top of #15589

jreback added a commit to jreback/pandas that referenced this issue Mar 22, 2017
jreback added a commit to jreback/pandas that referenced this issue Mar 22, 2017
TST: add pandas/tests/scalar/test_nat

TST: revise testing of tseries accessors

closes pandas-dev#15781
jreback added a commit to jreback/pandas that referenced this issue Mar 22, 2017
TST: add pandas/tests/scalar/test_nat

TST: revise testing of tseries accessors

closes pandas-dev#15781
jreback added a commit to jreback/pandas that referenced this issue Mar 27, 2017
TST: add pandas/tests/scalar/test_nat

TST: revise testing of tseries accessors

closes pandas-dev#15781
jorisvandenbossche pushed a commit that referenced this issue Mar 27, 2017
TST: add pandas/tests/scalar/test_nat

TST: revise testing of tseries accessors

closes #15781
mattip pushed a commit to mattip/pandas that referenced this issue Apr 3, 2017
TST: add pandas/tests/scalar/test_nat

TST: revise testing of tseries accessors

closes pandas-dev#15781
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Design Datetime Datetime data dtype
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants