|
27 | 27 | date_range,
|
28 | 28 | )
|
29 | 29 | import pandas._testing as tm
|
| 30 | +from pandas.core.arrays import DatetimeArray, TimedeltaArray |
30 | 31 | from pandas.core.ops import roperator
|
31 | 32 | from pandas.tests.arithmetic.common import (
|
32 | 33 | assert_invalid_addsub_type,
|
@@ -956,6 +957,18 @@ def test_dt64arr_sub_NaT(self, box_with_array):
|
956 | 957 | # -------------------------------------------------------------
|
957 | 958 | # Subtraction of datetime-like array-like
|
958 | 959 |
|
| 960 | + def test_dt64arr_sub_dt64object_array(self, box_with_array, tz_naive_fixture): |
| 961 | + dti = pd.date_range("2016-01-01", periods=3, tz=tz_naive_fixture) |
| 962 | + expected = dti - dti |
| 963 | + |
| 964 | + obj = tm.box_expected(dti, box_with_array) |
| 965 | + expected = tm.box_expected(expected, box_with_array) |
| 966 | + |
| 967 | + warn = PerformanceWarning if box_with_array is not pd.DataFrame else None |
| 968 | + with tm.assert_produces_warning(warn): |
| 969 | + result = obj - obj.astype(object) |
| 970 | + tm.assert_equal(result, expected) |
| 971 | + |
959 | 972 | def test_dt64arr_naive_sub_dt64ndarray(self, box_with_array):
|
960 | 973 | dti = pd.date_range("2016-01-01", periods=3, tz=None)
|
961 | 974 | dt64vals = dti.values
|
@@ -2395,3 +2408,31 @@ def test_shift_months(years, months):
|
2395 | 2408 | raw = [x + pd.offsets.DateOffset(years=years, months=months) for x in dti]
|
2396 | 2409 | expected = DatetimeIndex(raw)
|
2397 | 2410 | tm.assert_index_equal(actual, expected)
|
| 2411 | + |
| 2412 | + |
| 2413 | +def test_dt64arr_addsub_object_dtype_2d(): |
| 2414 | + # block-wise DataFrame operations will require operating on 2D |
| 2415 | + # DatetimeArray/TimedeltaArray, so check that specifically. |
| 2416 | + dti = pd.date_range("1994-02-13", freq="2W", periods=4) |
| 2417 | + dta = dti._data.reshape((4, 1)) |
| 2418 | + |
| 2419 | + other = np.array([[pd.offsets.Day(n)] for n in range(4)]) |
| 2420 | + assert other.shape == dta.shape |
| 2421 | + |
| 2422 | + with tm.assert_produces_warning(PerformanceWarning): |
| 2423 | + result = dta + other |
| 2424 | + with tm.assert_produces_warning(PerformanceWarning): |
| 2425 | + expected = (dta[:, 0] + other[:, 0]).reshape(-1, 1) |
| 2426 | + |
| 2427 | + assert isinstance(result, DatetimeArray) |
| 2428 | + assert result.freq is None |
| 2429 | + tm.assert_numpy_array_equal(result._data, expected._data) |
| 2430 | + |
| 2431 | + with tm.assert_produces_warning(PerformanceWarning): |
| 2432 | + # Case where we expect to get a TimedeltaArray back |
| 2433 | + result2 = dta - dta.astype(object) |
| 2434 | + |
| 2435 | + assert isinstance(result2, TimedeltaArray) |
| 2436 | + assert result2.shape == (4, 1) |
| 2437 | + assert result2.freq is None |
| 2438 | + assert (result2.asi8 == 0).all() |
0 commit comments