-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
API: pd.Timedelta(integer, unit=unit) give the requested unit #63302
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
base: main
Are you sure you want to change the base?
Changes from all commits
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -893,7 +893,7 @@ def create_data(constructor): | |||||
| [ | ||||||
| (lambda x: np.timedelta64(x, "D"), "m8[s]"), | ||||||
| (lambda x: timedelta(days=x), "m8[us]"), | ||||||
| (lambda x: Timedelta(x, "D"), "m8[ns]"), | ||||||
| (lambda x: Timedelta(x, "D"), "m8[s]"), | ||||||
| (lambda x: Timedelta(x, "D").as_unit("s"), "m8[s]"), | ||||||
|
Member
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.
Suggested change
Otherwise it is the same as the case above now. Or just remove the case alltogether |
||||||
| ], | ||||||
| ) | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1141,15 +1141,15 @@ def test_timedelta(self): | |
| ) | ||
| with tm.assert_produces_warning(Pandas4Warning, match=msg): | ||
| result = read_json(StringIO(ser.to_json()), typ="series").apply(converter) | ||
| tm.assert_series_equal(result, ser) | ||
| tm.assert_series_equal(result, ser.astype("m8[ms]")) | ||
|
Member
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. Not at all related to your change, but wondering: do you know why we apply this converter instead of checking the actual return value of |
||
|
|
||
| ser = Series( | ||
| [timedelta(23), timedelta(seconds=5)], index=Index([0, 1]), dtype="m8[ns]" | ||
| ) | ||
| assert ser.dtype == "timedelta64[ns]" | ||
| with tm.assert_produces_warning(Pandas4Warning, match=msg): | ||
| result = read_json(StringIO(ser.to_json()), typ="series").apply(converter) | ||
| tm.assert_series_equal(result, ser) | ||
| tm.assert_series_equal(result, ser.astype("m8[ms]")) | ||
|
|
||
| frame = DataFrame([timedelta(23), timedelta(seconds=5)], dtype="m8[ns]") | ||
| assert frame[0].dtype == "timedelta64[ns]" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -35,6 +35,17 @@ def test_noninteger_microseconds(self): | |
|
|
||
|
|
||
| class TestTimedeltaConstructorUnitKeyword: | ||
| def test_result_unit(self): | ||
|
Member
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. Do you have a similar explicit test for pd.to_timedelta ?
Member
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. Ah, that's because |
||
| # For supported units, we get result.unit == unit | ||
| for unit in ["s", "ms", "us", "ns"]: | ||
| td = Timedelta(1, unit=unit) | ||
| assert td.unit == unit | ||
|
|
||
| # For non-supported units we get the closest-supported unit | ||
| for unit in ["W", "D", "h", "m"]: | ||
| td = Timedelta(1, unit=unit) | ||
| assert td.unit == "s" | ||
|
|
||
| @pytest.mark.parametrize("unit", ["Y", "y", "M"]) | ||
| def test_unit_m_y_raises(self, unit): | ||
| msg = "Units 'M', 'Y', and 'y' are no longer supported" | ||
|
|
@@ -196,7 +207,8 @@ def test_construct_from_kwargs_overflow(): | |
|
|
||
| def test_construct_with_weeks_unit_overflow(): | ||
| # GH#47268 don't silently wrap around | ||
| with pytest.raises(OutOfBoundsTimedelta, match="without overflow"): | ||
| msg = "1000000000000000000 weeks" | ||
| with pytest.raises(OutOfBoundsTimedelta, match=msg): | ||
| Timedelta(1000000000000000000, unit="W") | ||
|
|
||
| with pytest.raises(OutOfBoundsTimedelta, match="without overflow"): | ||
|
|
@@ -284,7 +296,7 @@ def test_from_tick_reso(): | |
|
|
||
| def test_construction(): | ||
| expected = np.timedelta64(10, "D").astype("m8[ns]").view("i8") | ||
| assert Timedelta(10, unit="D")._value == expected | ||
| assert Timedelta(10, unit="D")._value == expected // 10**9 | ||
| assert Timedelta(10.0, unit="D")._value == expected | ||
| assert Timedelta("10 days")._value == expected // 1000 | ||
| assert Timedelta(days=10)._value == expected // 1000 | ||
|
|
@@ -464,9 +476,9 @@ def test_overflow_on_construction(): | |
| Timedelta(value) | ||
|
|
||
| # xref GH#17637 | ||
| msg = "Cannot cast 139993 from D to 'ns' without overflow" | ||
| with pytest.raises(OutOfBoundsTimedelta, match=msg): | ||
| Timedelta(7 * 19999, unit="D") | ||
| # used to overflows before we changed output unit to "s" | ||
| td = Timedelta(7 * 19999, unit="D") | ||
| assert td.unit == "s" | ||
|
|
||
| # used to overflow before non-ns support | ||
| td = Timedelta(timedelta(days=13 * 19999)) | ||
|
|
||
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 keep those if you make the timestamp(or timedelta) ns with adding a
.as_unit("ns")?