-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
|
||
for _maybe_method_name in dir(NaTType): | ||
_maybe_method = getattr(NaTType, _maybe_method_name) | ||
if hasattr(_maybe_method, "__call__") \ |
There was a problem hiding this comment.
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 \
Just a question: isn't it also a possibility to let it return |
@@ -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, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 for nan.
That makes sense. So leave |
@jreback tests are green. |
self.assertTrue(np.isnan(NaT.weekday())) | ||
def test_NaT_methods(self): | ||
# GH 9513 | ||
methods_raise = ['astimezone', 'combine', 'ctime', 'dst', 'fromordinal', |
There was a problem hiding this comment.
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
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. |
@shoyer and what with the attributes that don't return a number but a specific date/time object? |
hmm, we could return
The idea being is that you asked for a |
That makes perfect sense for |
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) |
There was a problem hiding this comment.
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
Most likely updating tonight. Any verdict on raising vs returning |
right I think things that have date should be NaT eg I am ok with time be Nat as well -even though it pushes the semantic boundaries number like should be nan (eg weekday) |
Agreed. NaT is a datetime instance and also a date instance.
I disagree. NaT is not a time instance. We should not return it from a method which promises to return a time.
Agreed. |
ok, so the suggestion is to raise for |
5452a47
to
0b72f59
Compare
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`) |
There was a problem hiding this comment.
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.
updated |
ok lgtm @jorisvandenbossche @shoyer comments? |
nan_methods = ['weekday', 'isoweekday'] | ||
|
||
for method in raise_methods: | ||
if hasattr(NaT, method): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Lgtm |
if (callable(_maybe_method) | ||
and not _maybe_method_name.startswith("_") | ||
and _maybe_method_name not in | ||
_implemented_methods + _nat_methods + _nan_methods): |
There was a problem hiding this comment.
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.
Updated again. Moved the list concatenation outside the loop. |
lgtm, @shoyer if ok to you, pls merge. |
Merged via c87fa18 |
This is to close #9513.
NaT
methods now raiseValueError
or returnnp.nan
.