Skip to content

ERR: GH9513 NaT methods now raise ValueError #10372

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

Closed
wants to merge 1 commit into from

Conversation

kawochen
Copy link
Contributor

This is to close #9513. NaT methods now raise ValueError or return np.nan.


for _maybe_method_name in dir(NaTType):
_maybe_method = getattr(NaTType, _maybe_method_name)
if hasattr(_maybe_method, "__call__") \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use parentheses to group across multiple lines instead of \

@jorisvandenbossche
Copy link
Member

Just a question: isn't it also a possibility to let it return np.nan ? Because now you get different results depending on if you access the date on a full index/series, or on a single element of that index/series

@jreback jreback added Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas labels Jun 17, 2015
@@ -1389,14 +1389,16 @@ def time(self):
"""
# can't call self.map() which tries to treat func as ufunc
# and causes recursion warnings on python 2.6
return self._maybe_mask_results(_algos.arrmap_object(self.asobject.values, lambda x: x.time()))
return self._maybe_mask_results(_algos.arrmap_object(self.asobject.values,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so its better to actually define some of these (e.g. as we did for weekday to directly return np.nan), so .date()/.time() should simply return np.nan

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for nan.

@jreback jreback added this to the 0.17.0 milestone Jun 17, 2015
@kawochen
Copy link
Contributor Author

That makes sense. So leave weekday unchanged, and have date and time return nan? Unsure about toordinal though.

@kawochen
Copy link
Contributor Author

@jreback tests are green.

self.assertTrue(np.isnan(NaT.weekday()))
def test_NaT_methods(self):
# GH 9513
methods_raise = ['astimezone', 'combine', 'ctime', 'dst', 'fromordinal',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I think today/now/utcnow/isoweekday maybe also should return nan

@shoyer
Copy link
Member

shoyer commented Jun 18, 2015

I think I would still prefer to raise for date and time. It's one thing to return nan for methods/attributes that return numbers, but nobody expects that for a method that is supposed to return these specific types.

@jorisvandenbossche
Copy link
Member

@shoyer and what with the attributes that don't return a number but a specific date/time object?

@jreback
Copy link
Contributor

jreback commented Jun 18, 2015

hmm, we could return NaT itself for date/time e.g.

def date(self):
    return self
In [8]: Timestamp('20130101').date()
Out[8]: datetime.date(2013, 1, 1)

In [9]: Timestamp('NaT').date()
Out[9]: NaT

The idea being is that you asked for a date/time you get a python object (as pandas does'nt have a specific date/time object specifically), but you have a missing value, so you get an object that represents a missing value (just typed to the missing value)

@kawochen
Copy link
Contributor Author

That makes perfect sense for date. But time is a different type, so I think users can still be surprised by NaT.

@shoyer
Copy link
Member

shoyer commented Jun 18, 2015

Technically, datetime is a subclass of date, not the other way around. It think it's better just to raise an error as opposed to guessing an appropriate sentinel value.


for method in methods_nan:
if hasattr(NaT, method):
self.assertTrue(getattr(NaT, method)() is np.nan)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use np.isnan for the comparison

@kawochen
Copy link
Contributor Author

Most likely updating tonight. Any verdict on raising vs returning nan or NaT?

@jreback
Copy link
Contributor

jreback commented Jul 12, 2015

right

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

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

number like should be nan (eg weekday)
ordinal should prob raise

@shoyer
Copy link
Member

shoyer commented Jul 12, 2015

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.

number like should be nan (eg weekday)
ordinal should prob raise

Agreed.

@jreback
Copy link
Contributor

jreback commented Jul 13, 2015

ok, so the suggestion is to raise for time. Ok not adverse to that.

@kawochen kawochen force-pushed the BUG-FIX-9513 branch 5 times, most recently from 5452a47 to 0b72f59 Compare July 15, 2015 03:35
@kawochen
Copy link
Contributor Author

updated

@@ -160,6 +160,8 @@ Other API Changes
- Serialize metadata properties of subclasses of pandas objects (:issue:`10553`).


- ``NaT``'s methods now either raise ``ValueError``, or return ``np.nan`` or ``NaT``; ``.weekday()`` now returns ``np.nan`` rather than ``raise``s; ``toordinal`` now ``raise``s (:issue:`9513`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sentence is confusing, either provide a conversion matrix or rework.

@kawochen
Copy link
Contributor Author

updated

@jreback
Copy link
Contributor

jreback commented Jul 16, 2015

ok lgtm

@jorisvandenbossche @shoyer comments?

nan_methods = ['weekday', 'isoweekday']

for method in raise_methods:
if hasattr(NaT, method):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would NaT not have one of these methods? Generally it's better to be entirely explicit in tests, rather than running them conditionally.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see your note below now. In that case, I'd slightly prefer to use if PY3 to adjust the list of tested methods but this is OK too.

@shoyer
Copy link
Member

shoyer commented Jul 16, 2015

Lgtm

if (callable(_maybe_method)
and not _maybe_method_name.startswith("_")
and _maybe_method_name not in
_implemented_methods + _nat_methods + _nan_methods):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually let me change this line. Having list concatenation in a loop doesn't seem like a good idea.

@kawochen
Copy link
Contributor Author

Updated again. Moved the list concatenation outside the loop.

@jreback
Copy link
Contributor

jreback commented Jul 17, 2015

lgtm, @shoyer if ok to you, pls merge.

@shoyer
Copy link
Member

shoyer commented Jul 17, 2015

Merged via c87fa18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Datetime Datetime data dtype Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pd.NaT.date() returns datetime.date(1, 255, 255)
5 participants