-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
disallow Timedelta ops with unitless timedelta64 #19870
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
Codecov Report
@@ Coverage Diff @@ ## master #19870 +/- ## ========================================== - Coverage 91.67% 91.66% -0.01% ========================================== Files 150 150 Lines 48936 48938 +2 ========================================== + Hits 44860 44861 +1 - Misses 4076 4077 +1
Continue to review full report at Codecov.
|
pandas/_libs/tslibs/timedeltas.pyx Outdated
| ------ | ||
| ValueError | ||
| """ | ||
| # GH#19388 |
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.
remove the issue number
| # GH#19388 | ||
| if value.dtype == 'm8': | ||
| # i.e. has no unit | ||
| if util.is_array(value) or (value.view('i8') != NPY_NAT): |
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.
I think you can just do a direct check here
In [1]: np.timedelta64('NaT') Out[1]: numpy.timedelta64('NaT') In [2]: np.timedelta64('NaT') is np.timedelta64(1, 'ns') Out[2]: False In [3]: np.timedelta64('NaT') is np.array([1], dtype='m8[ns]') Out[3]: False IOW only if it IS np.timedelta64('NaT') will it be ok
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.
Doesn't work like pd.NaT:
>>> np.timedelta64('NaT') is np.timedelta64('NaT') False | divmod(np.array([22, 24]), td) | ||
| | ||
| | ||
| def test_require_td64_unit(): |
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.
should be in test_constructor
pandas/_libs/tslibs/timedeltas.pyx Outdated
| return res | ||
| | ||
| | ||
| cdef _require_td64_has_unit(value): |
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.
type value (object) you can also add inline
pandas/_libs/tslibs/timedeltas.pyx Outdated
| Parameters | ||
| ---------- | ||
| value : np.timedelta64 or ndarray[timedelta64] |
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.
is this always true?
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.
Not quite; good catch.
| ValueError | ||
| """ | ||
| if value.dtype == 'm8': | ||
| # i.e. has no unit |
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.
pls make this more concise (e.g. a single if)
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.
The exception message could be shortened, but everything else is needed. Combining the two ifs into one line would hurt clarity.
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.
you can just use missing.is_null_datetimelike instead of repeating code here. pls use a single if.
| ValueError | ||
| """ | ||
| if value.dtype == 'm8': | ||
| # i.e. has no unit |
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.
you can just use missing.is_null_datetimelike instead of repeating code here. pls use a single if.
git diff upstream/master -u -- "*.py" | flake8 --diff