-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: 2D DTA/TDA arithmetic with object-dtype #32185
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 8 commits
4afc0ac
4d69be8
63cd78e
c9cea5f
52d189e
a45d6e2
6279270
ffa221c
e160bdc
04d10eb
0600bcf
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 |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
date_range, | ||
) | ||
import pandas._testing as tm | ||
from pandas.core.arrays import DatetimeArray, TimedeltaArray | ||
from pandas.core.ops import roperator | ||
from pandas.tests.arithmetic.common import ( | ||
assert_invalid_addsub_type, | ||
|
@@ -956,6 +957,18 @@ def test_dt64arr_sub_NaT(self, box_with_array): | |
# ------------------------------------------------------------- | ||
# Subtraction of datetime-like array-like | ||
|
||
def test_dt64arr_sub_dt64object_array(self, box_with_array, tz_naive_fixture): | ||
dti = pd.date_range("2016-01-01", periods=3, tz=tz_naive_fixture) | ||
expected = dti - dti | ||
|
||
obj = tm.box_expected(dti, box_with_array) | ||
expected = tm.box_expected(expected, box_with_array) | ||
|
||
warn = PerformanceWarning if box_with_array is not pd.DataFrame else None | ||
with tm.assert_produces_warning(warn): | ||
result = obj - obj.astype(object) | ||
tm.assert_equal(result, expected) | ||
|
||
def test_dt64arr_naive_sub_dt64ndarray(self, box_with_array): | ||
dti = pd.date_range("2016-01-01", periods=3, tz=None) | ||
dt64vals = dti.values | ||
|
@@ -2395,3 +2408,31 @@ def test_shift_months(years, months): | |
raw = [x + pd.offsets.DateOffset(years=years, months=months) for x in dti] | ||
expected = DatetimeIndex(raw) | ||
tm.assert_index_equal(actual, expected) | ||
|
||
|
||
def test_dt64arr_addsub_object_dtype_2d(): | ||
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. ok with either class based or free functions, mixing is slightly odd. 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. yah, this is a bit of an odd-man-out because the class-based tests are all trying to use box_with_array, whereas this is specifically aiming at the 2D array case |
||
# block-wise DataFrame operations will require operating on 2D | ||
# DatetimeArray/TimedeltaArray, so check that specifically. | ||
dti = pd.date_range("1994-02-13", freq="2W", periods=4) | ||
dta = dti._data.reshape((4, 1)) | ||
|
||
other = np.array([[pd.offsets.Day(n)] for n in range(4)]) | ||
assert other.shape == dta.shape | ||
|
||
with tm.assert_produces_warning(PerformanceWarning): | ||
result = dta + other | ||
with tm.assert_produces_warning(PerformanceWarning): | ||
expected = (dta[:, 0] + other[:, 0]).reshape(-1, 1) | ||
|
||
assert isinstance(result, DatetimeArray) | ||
assert result.freq is None | ||
tm.assert_numpy_array_equal(result._data, expected._data) | ||
|
||
with tm.assert_produces_warning(PerformanceWarning): | ||
# Case where we expect to get a TimedeltaArray back | ||
result2 = dta - dta.astype(object) | ||
|
||
assert isinstance(result2, TimedeltaArray) | ||
assert result2.shape == (4, 1) | ||
assert result2.freq is None | ||
assert (result2.asi8 == 0).all() |
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 add an assert then