|
3 | 3 | import numpy as np
|
4 | 4 | import pytest
|
5 | 5 |
|
| 6 | +from pandas.errors import OutOfBoundsDatetime |
| 7 | + |
6 | 8 | import pandas as pd
|
7 | 9 | from pandas import (
|
8 | 10 | Series,
|
@@ -131,12 +133,30 @@ def test_clip_with_datetimes(self):
|
131 | 133 | )
|
132 | 134 | tm.assert_series_equal(result, expected)
|
133 | 135 |
|
134 |
| - @pytest.mark.parametrize("dtype", [object, "M8[us]"]) |
135 |
| - def test_clip_with_timestamps_and_oob_datetimes(self, dtype): |
| 136 | + def test_clip_with_timestamps_and_oob_datetimes_object(self): |
136 | 137 | # GH-42794
|
137 |
| - ser = Series([datetime(1, 1, 1), datetime(9999, 9, 9)], dtype=dtype) |
| 138 | + ser = Series([datetime(1, 1, 1), datetime(9999, 9, 9)], dtype=object) |
138 | 139 |
|
139 | 140 | result = ser.clip(lower=Timestamp.min, upper=Timestamp.max)
|
140 |
| - expected = Series([Timestamp.min, Timestamp.max], dtype=dtype) |
| 141 | + expected = Series([Timestamp.min, Timestamp.max], dtype=object) |
| 142 | + |
| 143 | + tm.assert_series_equal(result, expected) |
| 144 | + |
| 145 | + def test_clip_with_timestamps_and_oob_datetimes_non_nano(self): |
| 146 | + # GH#56410 |
| 147 | + dtype = "M8[us]" |
| 148 | + ser = Series([datetime(1, 1, 1), datetime(9999, 9, 9)], dtype=dtype) |
| 149 | + |
| 150 | + msg = ( |
| 151 | + r"Incompatible \(high-resolution\) value for dtype='datetime64\[us\]'. " |
| 152 | + "Explicitly cast before operating" |
| 153 | + ) |
| 154 | + with pytest.raises(OutOfBoundsDatetime, match=msg): |
| 155 | + ser.clip(lower=Timestamp.min, upper=Timestamp.max) |
| 156 | + |
| 157 | + lower = Timestamp.min.as_unit("us") |
| 158 | + upper = Timestamp.max.as_unit("us") |
| 159 | + result = ser.clip(lower=lower, upper=upper) |
| 160 | + expected = Series([lower, upper], dtype=dtype) |
141 | 161 |
|
142 | 162 | tm.assert_series_equal(result, expected)
|
0 commit comments