-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix Timedelta.__floordiv__, __rfloordiv__ #18961
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
Changes from all commits
71c6835
3699934
c031864
09903b4
a2d6dc9
54cc35f
599e50f
13a5a20
0e532fc
79c3a8e
6af6ee4
a0029ad
c0f4783
0401851
3c38971
3501956
f101378
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,7 @@ def test_binary_ops_nat(self): | |
assert (td * pd.NaT) is pd.NaT | ||
assert (td / pd.NaT) is np.nan | ||
assert (td // pd.NaT) is np.nan | ||
assert (td // np.timedelta64('NaT')) is np.nan | ||
|
||
def test_binary_ops_integers(self): | ||
td = Timedelta(10, unit='d') | ||
|
@@ -162,6 +163,98 @@ def test_binary_ops_with_timedelta(self): | |
# invalid multiply with another timedelta | ||
pytest.raises(TypeError, lambda: td * td) | ||
|
||
def test_floordiv(self): | ||
# GH#18846 | ||
td = Timedelta(hours=3, minutes=4) | ||
scalar = Timedelta(hours=3, minutes=3) | ||
|
||
# scalar others | ||
assert td // scalar == 1 | ||
assert -td // scalar.to_pytimedelta() == -2 | ||
assert (2 * td) // scalar.to_timedelta64() == 2 | ||
|
||
assert td // np.nan is pd.NaT | ||
assert np.isnan(td // pd.NaT) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these should prob be in test_nat with other nat tests There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TestTimedeltaArithmetic doesn't have a dedicated test/section for nat tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pandas/tests/scalar/test_nat is where the nat tests go (i don't mind them here), but should have some (even if slightly duplicative) |
||
assert np.isnan(td // np.timedelta64('NaT')) | ||
|
||
with pytest.raises(TypeError): | ||
td // np.datetime64('2016-01-01', dtype='datetime64[us]') | ||
|
||
expected = Timedelta(hours=1, minutes=32) | ||
assert td // 2 == expected | ||
assert td // 2.0 == expected | ||
assert td // np.float64(2.0) == expected | ||
assert td // np.int32(2.0) == expected | ||
assert td // np.uint8(2.0) == expected | ||
|
||
# Array-like others | ||
assert td // np.array(scalar.to_timedelta64()) == 1 | ||
|
||
res = (3 * td) // np.array([scalar.to_timedelta64()]) | ||
expected = np.array([3], dtype=np.int64) | ||
tm.assert_numpy_array_equal(res, expected) | ||
|
||
res = (10 * td) // np.array([scalar.to_timedelta64(), | ||
np.timedelta64('NaT')]) | ||
expected = np.array([10, np.nan]) | ||
tm.assert_numpy_array_equal(res, expected) | ||
|
||
ser = pd.Series([1], dtype=np.int64) | ||
res = td // ser | ||
assert res.dtype.kind == 'm' | ||
|
||
def test_rfloordiv(self): | ||
# GH#18846 | ||
td = Timedelta(hours=3, minutes=3) | ||
scalar = Timedelta(hours=3, minutes=4) | ||
|
||
# scalar others | ||
# x // Timedelta is defined only for timedelta-like x. int-like, | ||
# float-like, and date-like, in particular, should all either | ||
# a) raise TypeError directly or | ||
# b) return NotImplemented, following which the reversed | ||
# operation will raise TypeError. | ||
assert td.__rfloordiv__(scalar) == 1 | ||
assert (-td).__rfloordiv__(scalar.to_pytimedelta()) == -2 | ||
assert (2 * td).__rfloordiv__(scalar.to_timedelta64()) == 0 | ||
|
||
assert np.isnan(td.__rfloordiv__(pd.NaT)) | ||
assert np.isnan(td.__rfloordiv__(np.timedelta64('NaT'))) | ||
|
||
dt64 = np.datetime64('2016-01-01', dtype='datetime64[us]') | ||
with pytest.raises(TypeError): | ||
td.__rfloordiv__(dt64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. adding a bit more comments here enumerating the cases would be useful |
||
|
||
assert td.__rfloordiv__(np.nan) is NotImplemented | ||
assert td.__rfloordiv__(3.5) is NotImplemented | ||
assert td.__rfloordiv__(2) is NotImplemented | ||
|
||
with pytest.raises(TypeError): | ||
td.__rfloordiv__(np.float64(2.0)) | ||
with pytest.raises(TypeError): | ||
td.__rfloordiv__(np.int32(2.0)) | ||
with pytest.raises(TypeError): | ||
td.__rfloordiv__(np.uint8(9)) | ||
|
||
# Array-like others | ||
assert td.__rfloordiv__(np.array(scalar.to_timedelta64())) == 1 | ||
|
||
res = td.__rfloordiv__(np.array([(3 * scalar).to_timedelta64()])) | ||
expected = np.array([3], dtype=np.int64) | ||
tm.assert_numpy_array_equal(res, expected) | ||
|
||
arr = np.array([(10 * scalar).to_timedelta64(), | ||
np.timedelta64('NaT')]) | ||
res = td.__rfloordiv__(arr) | ||
expected = np.array([10, np.nan]) | ||
tm.assert_numpy_array_equal(res, expected) | ||
|
||
ser = pd.Series([1], dtype=np.int64) | ||
res = td.__rfloordiv__(ser) | ||
assert res is NotImplemented | ||
with pytest.raises(TypeError): | ||
ser // td | ||
|
||
|
||
class TestTimedeltaComparison(object): | ||
def test_comparison_object_array(self): | ||
|
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 what we do for division?
is it not tested now?
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.
Yes.
The status quo is... weird.
Not that I'm aware of.