test_timedelta64.py 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179
  1. # Arithmetic tests for DataFrame/Series/Index/Array classes that should
  2. # behave identically.
  3. from datetime import (
  4. datetime,
  5. timedelta,
  6. )
  7. import numpy as np
  8. import pytest
  9. from pandas.errors import (
  10. OutOfBoundsDatetime,
  11. PerformanceWarning,
  12. )
  13. import pandas as pd
  14. from pandas import (
  15. DataFrame,
  16. DatetimeIndex,
  17. Index,
  18. NaT,
  19. Series,
  20. Timedelta,
  21. TimedeltaIndex,
  22. Timestamp,
  23. offsets,
  24. timedelta_range,
  25. )
  26. import pandas._testing as tm
  27. from pandas.core.arrays import NumpyExtensionArray
  28. from pandas.tests.arithmetic.common import (
  29. assert_invalid_addsub_type,
  30. assert_invalid_comparison,
  31. get_upcast_box,
  32. )
  33. def assert_dtype(obj, expected_dtype):
  34. """
  35. Helper to check the dtype for a Series, Index, or single-column DataFrame.
  36. """
  37. dtype = tm.get_dtype(obj)
  38. assert dtype == expected_dtype
  39. def get_expected_name(box, names):
  40. if box is DataFrame:
  41. # Since we are operating with a DataFrame and a non-DataFrame,
  42. # the non-DataFrame is cast to Series and its name ignored.
  43. exname = names[0]
  44. elif box in [tm.to_array, pd.array]:
  45. exname = names[1]
  46. else:
  47. exname = names[2]
  48. return exname
  49. # ------------------------------------------------------------------
  50. # Timedelta64[ns] dtype Comparisons
  51. class TestTimedelta64ArrayLikeComparisons:
  52. # Comparison tests for timedelta64[ns] vectors fully parametrized over
  53. # DataFrame/Series/TimedeltaIndex/TimedeltaArray. Ideally all comparison
  54. # tests will eventually end up here.
  55. def test_compare_timedelta64_zerodim(self, box_with_array):
  56. # GH#26689 should unbox when comparing with zerodim array
  57. box = box_with_array
  58. xbox = box_with_array if box_with_array not in [Index, pd.array] else np.ndarray
  59. tdi = timedelta_range("2h", periods=4)
  60. other = np.array(tdi.to_numpy()[0])
  61. tdi = tm.box_expected(tdi, box)
  62. res = tdi <= other
  63. expected = np.array([True, False, False, False])
  64. expected = tm.box_expected(expected, xbox)
  65. tm.assert_equal(res, expected)
  66. @pytest.mark.parametrize(
  67. "td_scalar",
  68. [
  69. timedelta(days=1),
  70. Timedelta(days=1),
  71. Timedelta(days=1).to_timedelta64(),
  72. offsets.Hour(24),
  73. ],
  74. )
  75. def test_compare_timedeltalike_scalar(self, box_with_array, td_scalar):
  76. # regression test for GH#5963
  77. box = box_with_array
  78. xbox = box if box not in [Index, pd.array] else np.ndarray
  79. ser = Series([timedelta(days=1), timedelta(days=2)])
  80. ser = tm.box_expected(ser, box)
  81. actual = ser > td_scalar
  82. expected = Series([False, True])
  83. expected = tm.box_expected(expected, xbox)
  84. tm.assert_equal(actual, expected)
  85. @pytest.mark.parametrize(
  86. "invalid",
  87. [
  88. 345600000000000,
  89. "a",
  90. Timestamp("2021-01-01"),
  91. Timestamp("2021-01-01").now("UTC"),
  92. Timestamp("2021-01-01").now().to_datetime64(),
  93. Timestamp("2021-01-01").now().to_pydatetime(),
  94. Timestamp("2021-01-01").date(),
  95. np.array(4), # zero-dim mismatched dtype
  96. ],
  97. )
  98. def test_td64_comparisons_invalid(self, box_with_array, invalid):
  99. # GH#13624 for str
  100. box = box_with_array
  101. rng = timedelta_range("1 days", periods=10)
  102. obj = tm.box_expected(rng, box)
  103. assert_invalid_comparison(obj, invalid, box)
  104. @pytest.mark.parametrize(
  105. "other",
  106. [
  107. list(range(10)),
  108. np.arange(10),
  109. np.arange(10).astype(np.float32),
  110. np.arange(10).astype(object),
  111. pd.date_range("1970-01-01", periods=10, tz="UTC").array,
  112. np.array(pd.date_range("1970-01-01", periods=10)),
  113. list(pd.date_range("1970-01-01", periods=10)),
  114. pd.date_range("1970-01-01", periods=10).astype(object),
  115. pd.period_range("1971-01-01", freq="D", periods=10).array,
  116. pd.period_range("1971-01-01", freq="D", periods=10).astype(object),
  117. ],
  118. )
  119. def test_td64arr_cmp_arraylike_invalid(self, other, box_with_array):
  120. # We don't parametrize this over box_with_array because listlike
  121. # other plays poorly with assert_invalid_comparison reversed checks
  122. rng = timedelta_range("1 days", periods=10)._data
  123. rng = tm.box_expected(rng, box_with_array)
  124. assert_invalid_comparison(rng, other, box_with_array)
  125. def test_td64arr_cmp_mixed_invalid(self):
  126. rng = timedelta_range("1 days", periods=5)._data
  127. other = np.array([0, 1, 2, rng[3], Timestamp("2021-01-01")])
  128. result = rng == other
  129. expected = np.array([False, False, False, True, False])
  130. tm.assert_numpy_array_equal(result, expected)
  131. result = rng != other
  132. tm.assert_numpy_array_equal(result, ~expected)
  133. msg = "Invalid comparison between|Cannot compare type|not supported between"
  134. with pytest.raises(TypeError, match=msg):
  135. rng < other
  136. with pytest.raises(TypeError, match=msg):
  137. rng > other
  138. with pytest.raises(TypeError, match=msg):
  139. rng <= other
  140. with pytest.raises(TypeError, match=msg):
  141. rng >= other
  142. class TestTimedelta64ArrayComparisons:
  143. # TODO: All of these need to be parametrized over box
  144. @pytest.mark.parametrize("dtype", [None, object])
  145. def test_comp_nat(self, dtype):
  146. left = TimedeltaIndex([Timedelta("1 days"), NaT, Timedelta("3 days")])
  147. right = TimedeltaIndex([NaT, NaT, Timedelta("3 days")])
  148. lhs, rhs = left, right
  149. if dtype is object:
  150. lhs, rhs = left.astype(object), right.astype(object)
  151. result = rhs == lhs
  152. expected = np.array([False, False, True])
  153. tm.assert_numpy_array_equal(result, expected)
  154. result = rhs != lhs
  155. expected = np.array([True, True, False])
  156. tm.assert_numpy_array_equal(result, expected)
  157. expected = np.array([False, False, False])
  158. tm.assert_numpy_array_equal(lhs == NaT, expected)
  159. tm.assert_numpy_array_equal(NaT == rhs, expected)
  160. expected = np.array([True, True, True])
  161. tm.assert_numpy_array_equal(lhs != NaT, expected)
  162. tm.assert_numpy_array_equal(NaT != lhs, expected)
  163. expected = np.array([False, False, False])
  164. tm.assert_numpy_array_equal(lhs < NaT, expected)
  165. tm.assert_numpy_array_equal(NaT > lhs, expected)
  166. @pytest.mark.parametrize(
  167. "idx2",
  168. [
  169. TimedeltaIndex(
  170. ["2 day", "2 day", NaT, NaT, "1 day 00:00:02", "5 days 00:00:03"]
  171. ),
  172. np.array(
  173. [
  174. np.timedelta64(2, "D"),
  175. np.timedelta64(2, "D"),
  176. np.timedelta64("nat"),
  177. np.timedelta64("nat"),
  178. np.timedelta64(1, "D") + np.timedelta64(2, "s"),
  179. np.timedelta64(5, "D") + np.timedelta64(3, "s"),
  180. ]
  181. ),
  182. ],
  183. )
  184. def test_comparisons_nat(self, idx2):
  185. idx1 = TimedeltaIndex(
  186. [
  187. "1 day",
  188. NaT,
  189. "1 day 00:00:01",
  190. NaT,
  191. "1 day 00:00:01",
  192. "5 day 00:00:03",
  193. ]
  194. )
  195. # Check pd.NaT is handles as the same as np.nan
  196. result = idx1 < idx2
  197. expected = np.array([True, False, False, False, True, False])
  198. tm.assert_numpy_array_equal(result, expected)
  199. result = idx2 > idx1
  200. expected = np.array([True, False, False, False, True, False])
  201. tm.assert_numpy_array_equal(result, expected)
  202. result = idx1 <= idx2
  203. expected = np.array([True, False, False, False, True, True])
  204. tm.assert_numpy_array_equal(result, expected)
  205. result = idx2 >= idx1
  206. expected = np.array([True, False, False, False, True, True])
  207. tm.assert_numpy_array_equal(result, expected)
  208. result = idx1 == idx2
  209. expected = np.array([False, False, False, False, False, True])
  210. tm.assert_numpy_array_equal(result, expected)
  211. result = idx1 != idx2
  212. expected = np.array([True, True, True, True, True, False])
  213. tm.assert_numpy_array_equal(result, expected)
  214. # TODO: better name
  215. def test_comparisons_coverage(self):
  216. rng = timedelta_range("1 days", periods=10)
  217. result = rng < rng[3]
  218. expected = np.array([True, True, True] + [False] * 7)
  219. tm.assert_numpy_array_equal(result, expected)
  220. result = rng == list(rng)
  221. exp = rng == rng
  222. tm.assert_numpy_array_equal(result, exp)
  223. # ------------------------------------------------------------------
  224. # Timedelta64[ns] dtype Arithmetic Operations
  225. class TestTimedelta64ArithmeticUnsorted:
  226. # Tests moved from type-specific test files but not
  227. # yet sorted/parametrized/de-duplicated
  228. def test_ufunc_coercions(self):
  229. # normal ops are also tested in tseries/test_timedeltas.py
  230. idx = TimedeltaIndex(["2h", "4h", "6h", "8h", "10h"], freq="2h", name="x")
  231. for result in [idx * 2, np.multiply(idx, 2)]:
  232. assert isinstance(result, TimedeltaIndex)
  233. exp = TimedeltaIndex(["4h", "8h", "12h", "16h", "20h"], freq="4h", name="x")
  234. tm.assert_index_equal(result, exp)
  235. assert result.freq == "4h"
  236. for result in [idx / 2, np.divide(idx, 2)]:
  237. assert isinstance(result, TimedeltaIndex)
  238. exp = TimedeltaIndex(["1h", "2h", "3h", "4h", "5h"], freq="h", name="x")
  239. tm.assert_index_equal(result, exp)
  240. assert result.freq == "h"
  241. for result in [-idx, np.negative(idx)]:
  242. assert isinstance(result, TimedeltaIndex)
  243. exp = TimedeltaIndex(
  244. ["-2h", "-4h", "-6h", "-8h", "-10h"], freq="-2h", name="x"
  245. )
  246. tm.assert_index_equal(result, exp)
  247. assert result.freq == "-2h"
  248. idx = TimedeltaIndex(["-2h", "-1h", "0h", "1h", "2h"], freq="h", name="x")
  249. for result in [abs(idx), np.absolute(idx)]:
  250. assert isinstance(result, TimedeltaIndex)
  251. exp = TimedeltaIndex(["2h", "1h", "0h", "1h", "2h"], freq=None, name="x")
  252. tm.assert_index_equal(result, exp)
  253. assert result.freq is None
  254. def test_subtraction_ops(self):
  255. # with datetimes/timedelta and tdi/dti
  256. tdi = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo")
  257. dti = pd.date_range("20130101", periods=3, name="bar")
  258. td = Timedelta("1 days")
  259. dt = Timestamp("20130101")
  260. msg = "cannot subtract a datelike from a TimedeltaArray"
  261. with pytest.raises(TypeError, match=msg):
  262. tdi - dt
  263. with pytest.raises(TypeError, match=msg):
  264. tdi - dti
  265. msg = r"unsupported operand type\(s\) for -"
  266. with pytest.raises(TypeError, match=msg):
  267. td - dt
  268. msg = "(bad|unsupported) operand type for unary"
  269. with pytest.raises(TypeError, match=msg):
  270. td - dti
  271. result = dt - dti
  272. expected = TimedeltaIndex(["0 days", "-1 days", "-2 days"], name="bar")
  273. tm.assert_index_equal(result, expected)
  274. result = dti - dt
  275. expected = TimedeltaIndex(["0 days", "1 days", "2 days"], name="bar")
  276. tm.assert_index_equal(result, expected)
  277. result = tdi - td
  278. expected = TimedeltaIndex(["0 days", NaT, "1 days"], name="foo")
  279. tm.assert_index_equal(result, expected)
  280. result = td - tdi
  281. expected = TimedeltaIndex(["0 days", NaT, "-1 days"], name="foo")
  282. tm.assert_index_equal(result, expected)
  283. result = dti - td
  284. expected = DatetimeIndex(
  285. ["20121231", "20130101", "20130102"], dtype="M8[ns]", freq="D", name="bar"
  286. )
  287. tm.assert_index_equal(result, expected)
  288. result = dt - tdi
  289. expected = DatetimeIndex(
  290. ["20121231", NaT, "20121230"], dtype="M8[ns]", name="foo"
  291. )
  292. tm.assert_index_equal(result, expected)
  293. def test_subtraction_ops_with_tz(self, box_with_array):
  294. # check that dt/dti subtraction ops with tz are validated
  295. dti = pd.date_range("20130101", periods=3)
  296. dti = tm.box_expected(dti, box_with_array)
  297. ts = Timestamp("20130101")
  298. dt = ts.to_pydatetime()
  299. dti_tz = pd.date_range("20130101", periods=3).tz_localize("US/Eastern")
  300. dti_tz = tm.box_expected(dti_tz, box_with_array)
  301. ts_tz = Timestamp("20130101").tz_localize("US/Eastern")
  302. ts_tz2 = Timestamp("20130101").tz_localize("CET")
  303. dt_tz = ts_tz.to_pydatetime()
  304. td = Timedelta("1 days")
  305. def _check(result, expected):
  306. assert result == expected
  307. assert isinstance(result, Timedelta)
  308. # scalars
  309. result = ts - ts
  310. expected = Timedelta("0 days")
  311. _check(result, expected)
  312. result = dt_tz - ts_tz
  313. expected = Timedelta("0 days")
  314. _check(result, expected)
  315. result = ts_tz - dt_tz
  316. expected = Timedelta("0 days")
  317. _check(result, expected)
  318. # tz mismatches
  319. msg = "Cannot subtract tz-naive and tz-aware datetime-like objects."
  320. with pytest.raises(TypeError, match=msg):
  321. dt_tz - ts
  322. msg = "can't subtract offset-naive and offset-aware datetimes"
  323. with pytest.raises(TypeError, match=msg):
  324. dt_tz - dt
  325. msg = "can't subtract offset-naive and offset-aware datetimes"
  326. with pytest.raises(TypeError, match=msg):
  327. dt - dt_tz
  328. msg = "Cannot subtract tz-naive and tz-aware datetime-like objects."
  329. with pytest.raises(TypeError, match=msg):
  330. ts - dt_tz
  331. with pytest.raises(TypeError, match=msg):
  332. ts_tz2 - ts
  333. with pytest.raises(TypeError, match=msg):
  334. ts_tz2 - dt
  335. msg = "Cannot subtract tz-naive and tz-aware"
  336. # with dti
  337. with pytest.raises(TypeError, match=msg):
  338. dti - ts_tz
  339. with pytest.raises(TypeError, match=msg):
  340. dti_tz - ts
  341. result = dti_tz - dt_tz
  342. expected = TimedeltaIndex(["0 days", "1 days", "2 days"])
  343. expected = tm.box_expected(expected, box_with_array)
  344. tm.assert_equal(result, expected)
  345. result = dt_tz - dti_tz
  346. expected = TimedeltaIndex(["0 days", "-1 days", "-2 days"])
  347. expected = tm.box_expected(expected, box_with_array)
  348. tm.assert_equal(result, expected)
  349. result = dti_tz - ts_tz
  350. expected = TimedeltaIndex(["0 days", "1 days", "2 days"])
  351. expected = tm.box_expected(expected, box_with_array)
  352. tm.assert_equal(result, expected)
  353. result = ts_tz - dti_tz
  354. expected = TimedeltaIndex(["0 days", "-1 days", "-2 days"])
  355. expected = tm.box_expected(expected, box_with_array)
  356. tm.assert_equal(result, expected)
  357. result = td - td
  358. expected = Timedelta("0 days")
  359. _check(result, expected)
  360. result = dti_tz - td
  361. expected = DatetimeIndex(
  362. ["20121231", "20130101", "20130102"], tz="US/Eastern"
  363. ).as_unit("ns")
  364. expected = tm.box_expected(expected, box_with_array)
  365. tm.assert_equal(result, expected)
  366. def test_dti_tdi_numeric_ops(self):
  367. # These are normally union/diff set-like ops
  368. tdi = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo")
  369. dti = pd.date_range("20130101", periods=3, name="bar")
  370. result = tdi - tdi
  371. expected = TimedeltaIndex(["0 days", NaT, "0 days"], name="foo")
  372. tm.assert_index_equal(result, expected)
  373. result = tdi + tdi
  374. expected = TimedeltaIndex(["2 days", NaT, "4 days"], name="foo")
  375. tm.assert_index_equal(result, expected)
  376. result = dti - tdi # name will be reset
  377. expected = DatetimeIndex(["20121231", NaT, "20130101"], dtype="M8[ns]")
  378. tm.assert_index_equal(result, expected)
  379. def test_addition_ops(self):
  380. # with datetimes/timedelta and tdi/dti
  381. tdi = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo")
  382. dti = pd.date_range("20130101", periods=3, name="bar")
  383. td = Timedelta("1 days")
  384. dt = Timestamp("20130101")
  385. result = tdi + dt
  386. expected = DatetimeIndex(
  387. ["20130102", NaT, "20130103"], dtype="M8[ns]", name="foo"
  388. )
  389. tm.assert_index_equal(result, expected)
  390. result = dt + tdi
  391. expected = DatetimeIndex(
  392. ["20130102", NaT, "20130103"], dtype="M8[ns]", name="foo"
  393. )
  394. tm.assert_index_equal(result, expected)
  395. result = td + tdi
  396. expected = TimedeltaIndex(["2 days", NaT, "3 days"], name="foo")
  397. tm.assert_index_equal(result, expected)
  398. result = tdi + td
  399. expected = TimedeltaIndex(["2 days", NaT, "3 days"], name="foo")
  400. tm.assert_index_equal(result, expected)
  401. # unequal length
  402. msg = "cannot add indices of unequal length"
  403. with pytest.raises(ValueError, match=msg):
  404. tdi + dti[0:1]
  405. with pytest.raises(ValueError, match=msg):
  406. tdi[0:1] + dti
  407. # random indexes
  408. msg = "Addition/subtraction of integers and integer-arrays"
  409. with pytest.raises(TypeError, match=msg):
  410. tdi + Index([1, 2, 3], dtype=np.int64)
  411. # this is a union!
  412. # FIXME: don't leave commented-out
  413. # pytest.raises(TypeError, lambda : Index([1,2,3]) + tdi)
  414. result = tdi + dti # name will be reset
  415. expected = DatetimeIndex(["20130102", NaT, "20130105"], dtype="M8[ns]")
  416. tm.assert_index_equal(result, expected)
  417. result = dti + tdi # name will be reset
  418. expected = DatetimeIndex(["20130102", NaT, "20130105"], dtype="M8[ns]")
  419. tm.assert_index_equal(result, expected)
  420. result = dt + td
  421. expected = Timestamp("20130102")
  422. assert result == expected
  423. result = td + dt
  424. expected = Timestamp("20130102")
  425. assert result == expected
  426. # TODO: Needs more informative name, probably split up into
  427. # more targeted tests
  428. @pytest.mark.parametrize("freq", ["D", "B"])
  429. def test_timedelta(self, freq):
  430. index = pd.date_range("1/1/2000", periods=50, freq=freq)
  431. shifted = index + timedelta(1)
  432. back = shifted + timedelta(-1)
  433. back = back._with_freq("infer")
  434. tm.assert_index_equal(index, back)
  435. if freq == "D":
  436. expected = pd.tseries.offsets.Day(1)
  437. assert index.freq == expected
  438. assert shifted.freq == expected
  439. assert back.freq == expected
  440. else: # freq == 'B'
  441. assert index.freq == pd.tseries.offsets.BusinessDay(1)
  442. assert shifted.freq is None
  443. assert back.freq == pd.tseries.offsets.BusinessDay(1)
  444. result = index - timedelta(1)
  445. expected = index + timedelta(-1)
  446. tm.assert_index_equal(result, expected)
  447. def test_timedelta_tick_arithmetic(self):
  448. # GH#4134, buggy with timedeltas
  449. rng = pd.date_range("2013", "2014")
  450. s = Series(rng)
  451. result1 = rng - offsets.Hour(1)
  452. result2 = DatetimeIndex(s - np.timedelta64(100000000))
  453. result3 = rng - np.timedelta64(100000000)
  454. result4 = DatetimeIndex(s - offsets.Hour(1))
  455. assert result1.freq == rng.freq
  456. result1 = result1._with_freq(None)
  457. tm.assert_index_equal(result1, result4)
  458. assert result3.freq == rng.freq
  459. result3 = result3._with_freq(None)
  460. tm.assert_index_equal(result2, result3)
  461. def test_tda_add_sub_index(self):
  462. # Check that TimedeltaArray defers to Index on arithmetic ops
  463. tdi = TimedeltaIndex(["1 days", NaT, "2 days"])
  464. tda = tdi.array
  465. dti = pd.date_range("1999-12-31", periods=3, freq="D")
  466. result = tda + dti
  467. expected = tdi + dti
  468. tm.assert_index_equal(result, expected)
  469. result = tda + tdi
  470. expected = tdi + tdi
  471. tm.assert_index_equal(result, expected)
  472. result = tda - tdi
  473. expected = tdi - tdi
  474. tm.assert_index_equal(result, expected)
  475. def test_tda_add_dt64_object_array(self, box_with_array, tz_naive_fixture):
  476. # Result should be cast back to DatetimeArray
  477. box = box_with_array
  478. dti = pd.date_range("2016-01-01", periods=3, tz=tz_naive_fixture)
  479. dti = dti._with_freq(None)
  480. tdi = dti - dti
  481. obj = tm.box_expected(tdi, box)
  482. other = tm.box_expected(dti, box)
  483. with tm.assert_produces_warning(PerformanceWarning):
  484. result = obj + other.astype(object)
  485. tm.assert_equal(result, other.astype(object))
  486. # -------------------------------------------------------------
  487. # Binary operations TimedeltaIndex and timedelta-like
  488. def test_tdi_iadd_timedeltalike(self, two_hours, box_with_array):
  489. # only test adding/sub offsets as + is now numeric
  490. rng = timedelta_range("1 days", "10 days")
  491. expected = timedelta_range("1 days 02:00:00", "10 days 02:00:00", freq="D")
  492. rng = tm.box_expected(rng, box_with_array)
  493. expected = tm.box_expected(expected, box_with_array)
  494. orig_rng = rng
  495. rng += two_hours
  496. tm.assert_equal(rng, expected)
  497. if box_with_array is not Index:
  498. # Check that operation is actually inplace
  499. tm.assert_equal(orig_rng, expected)
  500. def test_tdi_isub_timedeltalike(self, two_hours, box_with_array):
  501. # only test adding/sub offsets as - is now numeric
  502. rng = timedelta_range("1 days", "10 days")
  503. expected = timedelta_range("0 days 22:00:00", "9 days 22:00:00")
  504. rng = tm.box_expected(rng, box_with_array)
  505. expected = tm.box_expected(expected, box_with_array)
  506. orig_rng = rng
  507. rng -= two_hours
  508. tm.assert_equal(rng, expected)
  509. if box_with_array is not Index:
  510. # Check that operation is actually inplace
  511. tm.assert_equal(orig_rng, expected)
  512. # -------------------------------------------------------------
  513. def test_tdi_ops_attributes(self):
  514. rng = timedelta_range("2 days", periods=5, freq="2D", name="x")
  515. result = rng + 1 * rng.freq
  516. exp = timedelta_range("4 days", periods=5, freq="2D", name="x")
  517. tm.assert_index_equal(result, exp)
  518. assert result.freq == "2D"
  519. result = rng - 2 * rng.freq
  520. exp = timedelta_range("-2 days", periods=5, freq="2D", name="x")
  521. tm.assert_index_equal(result, exp)
  522. assert result.freq == "2D"
  523. result = rng * 2
  524. exp = timedelta_range("4 days", periods=5, freq="4D", name="x")
  525. tm.assert_index_equal(result, exp)
  526. assert result.freq == "4D"
  527. result = rng / 2
  528. exp = timedelta_range("1 days", periods=5, freq="D", name="x")
  529. tm.assert_index_equal(result, exp)
  530. assert result.freq == "D"
  531. result = -rng
  532. exp = timedelta_range("-2 days", periods=5, freq="-2D", name="x")
  533. tm.assert_index_equal(result, exp)
  534. assert result.freq == "-2D"
  535. rng = timedelta_range("-2 days", periods=5, freq="D", name="x")
  536. result = abs(rng)
  537. exp = TimedeltaIndex(
  538. ["2 days", "1 days", "0 days", "1 days", "2 days"], name="x"
  539. )
  540. tm.assert_index_equal(result, exp)
  541. assert result.freq is None
  542. class TestAddSubNaTMasking:
  543. # TODO: parametrize over boxes
  544. @pytest.mark.parametrize("str_ts", ["1950-01-01", "1980-01-01"])
  545. def test_tdarr_add_timestamp_nat_masking(self, box_with_array, str_ts):
  546. # GH#17991 checking for overflow-masking with NaT
  547. tdinat = pd.to_timedelta(["24658 days 11:15:00", "NaT"])
  548. tdobj = tm.box_expected(tdinat, box_with_array)
  549. ts = Timestamp(str_ts)
  550. ts_variants = [
  551. ts,
  552. ts.to_pydatetime(),
  553. ts.to_datetime64().astype("datetime64[ns]"),
  554. ts.to_datetime64().astype("datetime64[D]"),
  555. ]
  556. for variant in ts_variants:
  557. res = tdobj + variant
  558. if box_with_array is DataFrame:
  559. assert res.iloc[1, 1] is NaT
  560. else:
  561. assert res[1] is NaT
  562. def test_tdi_add_overflow(self):
  563. # See GH#14068
  564. # preliminary test scalar analogue of vectorized tests below
  565. # TODO: Make raised error message more informative and test
  566. with pytest.raises(OutOfBoundsDatetime, match="10155196800000000000"):
  567. pd.to_timedelta(106580, "D") + Timestamp("2000")
  568. with pytest.raises(OutOfBoundsDatetime, match="10155196800000000000"):
  569. Timestamp("2000") + pd.to_timedelta(106580, "D")
  570. _NaT = NaT._value + 1
  571. msg = "Overflow in int64 addition"
  572. with pytest.raises(OverflowError, match=msg):
  573. pd.to_timedelta([106580], "D") + Timestamp("2000")
  574. with pytest.raises(OverflowError, match=msg):
  575. Timestamp("2000") + pd.to_timedelta([106580], "D")
  576. with pytest.raises(OverflowError, match=msg):
  577. pd.to_timedelta([_NaT]) - Timedelta("1 days")
  578. with pytest.raises(OverflowError, match=msg):
  579. pd.to_timedelta(["5 days", _NaT]) - Timedelta("1 days")
  580. with pytest.raises(OverflowError, match=msg):
  581. (
  582. pd.to_timedelta([_NaT, "5 days", "1 hours"])
  583. - pd.to_timedelta(["7 seconds", _NaT, "4 hours"])
  584. )
  585. # These should not overflow!
  586. exp = TimedeltaIndex([NaT])
  587. result = pd.to_timedelta([NaT]) - Timedelta("1 days")
  588. tm.assert_index_equal(result, exp)
  589. exp = TimedeltaIndex(["4 days", NaT])
  590. result = pd.to_timedelta(["5 days", NaT]) - Timedelta("1 days")
  591. tm.assert_index_equal(result, exp)
  592. exp = TimedeltaIndex([NaT, NaT, "5 hours"])
  593. result = pd.to_timedelta([NaT, "5 days", "1 hours"]) + pd.to_timedelta(
  594. ["7 seconds", NaT, "4 hours"]
  595. )
  596. tm.assert_index_equal(result, exp)
  597. class TestTimedeltaArraylikeAddSubOps:
  598. # Tests for timedelta64[ns] __add__, __sub__, __radd__, __rsub__
  599. def test_sub_nat_retain_unit(self):
  600. ser = pd.to_timedelta(Series(["00:00:01"])).astype("m8[s]")
  601. result = ser - NaT
  602. expected = Series([NaT], dtype="m8[s]")
  603. tm.assert_series_equal(result, expected)
  604. # TODO: moved from tests.indexes.timedeltas.test_arithmetic; needs
  605. # parametrization+de-duplication
  606. def test_timedelta_ops_with_missing_values(self):
  607. # setup
  608. s1 = pd.to_timedelta(Series(["00:00:01"]))
  609. s2 = pd.to_timedelta(Series(["00:00:02"]))
  610. sn = pd.to_timedelta(Series([NaT], dtype="m8[ns]"))
  611. df1 = DataFrame(["00:00:01"]).apply(pd.to_timedelta)
  612. df2 = DataFrame(["00:00:02"]).apply(pd.to_timedelta)
  613. dfn = DataFrame([NaT._value]).apply(pd.to_timedelta)
  614. scalar1 = pd.to_timedelta("00:00:01")
  615. scalar2 = pd.to_timedelta("00:00:02")
  616. timedelta_NaT = pd.to_timedelta("NaT")
  617. actual = scalar1 + scalar1
  618. assert actual == scalar2
  619. actual = scalar2 - scalar1
  620. assert actual == scalar1
  621. actual = s1 + s1
  622. tm.assert_series_equal(actual, s2)
  623. actual = s2 - s1
  624. tm.assert_series_equal(actual, s1)
  625. actual = s1 + scalar1
  626. tm.assert_series_equal(actual, s2)
  627. actual = scalar1 + s1
  628. tm.assert_series_equal(actual, s2)
  629. actual = s2 - scalar1
  630. tm.assert_series_equal(actual, s1)
  631. actual = -scalar1 + s2
  632. tm.assert_series_equal(actual, s1)
  633. actual = s1 + timedelta_NaT
  634. tm.assert_series_equal(actual, sn)
  635. actual = timedelta_NaT + s1
  636. tm.assert_series_equal(actual, sn)
  637. actual = s1 - timedelta_NaT
  638. tm.assert_series_equal(actual, sn)
  639. actual = -timedelta_NaT + s1
  640. tm.assert_series_equal(actual, sn)
  641. msg = "unsupported operand type"
  642. with pytest.raises(TypeError, match=msg):
  643. s1 + np.nan
  644. with pytest.raises(TypeError, match=msg):
  645. np.nan + s1
  646. with pytest.raises(TypeError, match=msg):
  647. s1 - np.nan
  648. with pytest.raises(TypeError, match=msg):
  649. -np.nan + s1
  650. actual = s1 + NaT
  651. tm.assert_series_equal(actual, sn)
  652. actual = s2 - NaT
  653. tm.assert_series_equal(actual, sn)
  654. actual = s1 + df1
  655. tm.assert_frame_equal(actual, df2)
  656. actual = s2 - df1
  657. tm.assert_frame_equal(actual, df1)
  658. actual = df1 + s1
  659. tm.assert_frame_equal(actual, df2)
  660. actual = df2 - s1
  661. tm.assert_frame_equal(actual, df1)
  662. actual = df1 + df1
  663. tm.assert_frame_equal(actual, df2)
  664. actual = df2 - df1
  665. tm.assert_frame_equal(actual, df1)
  666. actual = df1 + scalar1
  667. tm.assert_frame_equal(actual, df2)
  668. actual = df2 - scalar1
  669. tm.assert_frame_equal(actual, df1)
  670. actual = df1 + timedelta_NaT
  671. tm.assert_frame_equal(actual, dfn)
  672. actual = df1 - timedelta_NaT
  673. tm.assert_frame_equal(actual, dfn)
  674. msg = "cannot subtract a datelike from|unsupported operand type"
  675. with pytest.raises(TypeError, match=msg):
  676. df1 + np.nan
  677. with pytest.raises(TypeError, match=msg):
  678. df1 - np.nan
  679. actual = df1 + NaT # NaT is datetime, not timedelta
  680. tm.assert_frame_equal(actual, dfn)
  681. actual = df1 - NaT
  682. tm.assert_frame_equal(actual, dfn)
  683. # TODO: moved from tests.series.test_operators, needs splitting, cleanup,
  684. # de-duplication, box-parametrization...
  685. def test_operators_timedelta64(self):
  686. # series ops
  687. v1 = pd.date_range("2012-1-1", periods=3, freq="D")
  688. v2 = pd.date_range("2012-1-2", periods=3, freq="D")
  689. rs = Series(v2) - Series(v1)
  690. xp = Series(1e9 * 3600 * 24, rs.index).astype("int64").astype("timedelta64[ns]")
  691. tm.assert_series_equal(rs, xp)
  692. assert rs.dtype == "timedelta64[ns]"
  693. df = DataFrame({"A": v1})
  694. td = Series([timedelta(days=i) for i in range(3)])
  695. assert td.dtype == "timedelta64[ns]"
  696. # series on the rhs
  697. result = df["A"] - df["A"].shift()
  698. assert result.dtype == "timedelta64[ns]"
  699. result = df["A"] + td
  700. assert result.dtype == "M8[ns]"
  701. # scalar Timestamp on rhs
  702. maxa = df["A"].max()
  703. assert isinstance(maxa, Timestamp)
  704. resultb = df["A"] - df["A"].max()
  705. assert resultb.dtype == "timedelta64[ns]"
  706. # timestamp on lhs
  707. result = resultb + df["A"]
  708. values = [Timestamp("20111230"), Timestamp("20120101"), Timestamp("20120103")]
  709. expected = Series(values, dtype="M8[ns]", name="A")
  710. tm.assert_series_equal(result, expected)
  711. # datetimes on rhs
  712. result = df["A"] - datetime(2001, 1, 1)
  713. expected = Series([timedelta(days=4017 + i) for i in range(3)], name="A")
  714. tm.assert_series_equal(result, expected)
  715. assert result.dtype == "m8[ns]"
  716. d = datetime(2001, 1, 1, 3, 4)
  717. resulta = df["A"] - d
  718. assert resulta.dtype == "m8[ns]"
  719. # roundtrip
  720. resultb = resulta + d
  721. tm.assert_series_equal(df["A"], resultb)
  722. # timedeltas on rhs
  723. td = timedelta(days=1)
  724. resulta = df["A"] + td
  725. resultb = resulta - td
  726. tm.assert_series_equal(resultb, df["A"])
  727. assert resultb.dtype == "M8[ns]"
  728. # roundtrip
  729. td = timedelta(minutes=5, seconds=3)
  730. resulta = df["A"] + td
  731. resultb = resulta - td
  732. tm.assert_series_equal(df["A"], resultb)
  733. assert resultb.dtype == "M8[ns]"
  734. # inplace
  735. value = rs[2] + np.timedelta64(timedelta(minutes=5, seconds=1))
  736. rs[2] += np.timedelta64(timedelta(minutes=5, seconds=1))
  737. assert rs[2] == value
  738. def test_timedelta64_ops_nat(self):
  739. # GH 11349
  740. timedelta_series = Series([NaT, Timedelta("1s")])
  741. nat_series_dtype_timedelta = Series([NaT, NaT], dtype="timedelta64[ns]")
  742. single_nat_dtype_timedelta = Series([NaT], dtype="timedelta64[ns]")
  743. # subtraction
  744. tm.assert_series_equal(timedelta_series - NaT, nat_series_dtype_timedelta)
  745. tm.assert_series_equal(-NaT + timedelta_series, nat_series_dtype_timedelta)
  746. tm.assert_series_equal(
  747. timedelta_series - single_nat_dtype_timedelta, nat_series_dtype_timedelta
  748. )
  749. tm.assert_series_equal(
  750. -single_nat_dtype_timedelta + timedelta_series, nat_series_dtype_timedelta
  751. )
  752. # addition
  753. tm.assert_series_equal(
  754. nat_series_dtype_timedelta + NaT, nat_series_dtype_timedelta
  755. )
  756. tm.assert_series_equal(
  757. NaT + nat_series_dtype_timedelta, nat_series_dtype_timedelta
  758. )
  759. tm.assert_series_equal(
  760. nat_series_dtype_timedelta + single_nat_dtype_timedelta,
  761. nat_series_dtype_timedelta,
  762. )
  763. tm.assert_series_equal(
  764. single_nat_dtype_timedelta + nat_series_dtype_timedelta,
  765. nat_series_dtype_timedelta,
  766. )
  767. tm.assert_series_equal(timedelta_series + NaT, nat_series_dtype_timedelta)
  768. tm.assert_series_equal(NaT + timedelta_series, nat_series_dtype_timedelta)
  769. tm.assert_series_equal(
  770. timedelta_series + single_nat_dtype_timedelta, nat_series_dtype_timedelta
  771. )
  772. tm.assert_series_equal(
  773. single_nat_dtype_timedelta + timedelta_series, nat_series_dtype_timedelta
  774. )
  775. tm.assert_series_equal(
  776. nat_series_dtype_timedelta + NaT, nat_series_dtype_timedelta
  777. )
  778. tm.assert_series_equal(
  779. NaT + nat_series_dtype_timedelta, nat_series_dtype_timedelta
  780. )
  781. tm.assert_series_equal(
  782. nat_series_dtype_timedelta + single_nat_dtype_timedelta,
  783. nat_series_dtype_timedelta,
  784. )
  785. tm.assert_series_equal(
  786. single_nat_dtype_timedelta + nat_series_dtype_timedelta,
  787. nat_series_dtype_timedelta,
  788. )
  789. # multiplication
  790. tm.assert_series_equal(
  791. nat_series_dtype_timedelta * 1.0, nat_series_dtype_timedelta
  792. )
  793. tm.assert_series_equal(
  794. 1.0 * nat_series_dtype_timedelta, nat_series_dtype_timedelta
  795. )
  796. tm.assert_series_equal(timedelta_series * 1, timedelta_series)
  797. tm.assert_series_equal(1 * timedelta_series, timedelta_series)
  798. tm.assert_series_equal(timedelta_series * 1.5, Series([NaT, Timedelta("1.5s")]))
  799. tm.assert_series_equal(1.5 * timedelta_series, Series([NaT, Timedelta("1.5s")]))
  800. tm.assert_series_equal(timedelta_series * np.nan, nat_series_dtype_timedelta)
  801. tm.assert_series_equal(np.nan * timedelta_series, nat_series_dtype_timedelta)
  802. # division
  803. tm.assert_series_equal(timedelta_series / 2, Series([NaT, Timedelta("0.5s")]))
  804. tm.assert_series_equal(timedelta_series / 2.0, Series([NaT, Timedelta("0.5s")]))
  805. tm.assert_series_equal(timedelta_series / np.nan, nat_series_dtype_timedelta)
  806. # -------------------------------------------------------------
  807. # Binary operations td64 arraylike and datetime-like
  808. @pytest.mark.parametrize("cls", [Timestamp, datetime, np.datetime64])
  809. def test_td64arr_add_sub_datetimelike_scalar(
  810. self, cls, box_with_array, tz_naive_fixture
  811. ):
  812. # GH#11925, GH#29558, GH#23215
  813. tz = tz_naive_fixture
  814. dt_scalar = Timestamp("2012-01-01", tz=tz)
  815. if cls is datetime:
  816. ts = dt_scalar.to_pydatetime()
  817. elif cls is np.datetime64:
  818. if tz_naive_fixture is not None:
  819. pytest.skip(f"{cls} doesn support {tz_naive_fixture}")
  820. ts = dt_scalar.to_datetime64()
  821. else:
  822. ts = dt_scalar
  823. tdi = timedelta_range("1 day", periods=3)
  824. expected = pd.date_range("2012-01-02", periods=3, tz=tz)
  825. tdarr = tm.box_expected(tdi, box_with_array)
  826. expected = tm.box_expected(expected, box_with_array)
  827. tm.assert_equal(ts + tdarr, expected)
  828. tm.assert_equal(tdarr + ts, expected)
  829. expected2 = pd.date_range("2011-12-31", periods=3, freq="-1D", tz=tz)
  830. expected2 = tm.box_expected(expected2, box_with_array)
  831. tm.assert_equal(ts - tdarr, expected2)
  832. tm.assert_equal(ts + (-tdarr), expected2)
  833. msg = "cannot subtract a datelike"
  834. with pytest.raises(TypeError, match=msg):
  835. tdarr - ts
  836. def test_td64arr_add_datetime64_nat(self, box_with_array):
  837. # GH#23215
  838. other = np.datetime64("NaT")
  839. tdi = timedelta_range("1 day", periods=3)
  840. expected = DatetimeIndex(["NaT", "NaT", "NaT"], dtype="M8[ns]")
  841. tdser = tm.box_expected(tdi, box_with_array)
  842. expected = tm.box_expected(expected, box_with_array)
  843. tm.assert_equal(tdser + other, expected)
  844. tm.assert_equal(other + tdser, expected)
  845. def test_td64arr_sub_dt64_array(self, box_with_array):
  846. dti = pd.date_range("2016-01-01", periods=3)
  847. tdi = TimedeltaIndex(["-1 Day"] * 3)
  848. dtarr = dti.values
  849. expected = DatetimeIndex(dtarr) - tdi
  850. tdi = tm.box_expected(tdi, box_with_array)
  851. expected = tm.box_expected(expected, box_with_array)
  852. msg = "cannot subtract a datelike from"
  853. with pytest.raises(TypeError, match=msg):
  854. tdi - dtarr
  855. # TimedeltaIndex.__rsub__
  856. result = dtarr - tdi
  857. tm.assert_equal(result, expected)
  858. def test_td64arr_add_dt64_array(self, box_with_array):
  859. dti = pd.date_range("2016-01-01", periods=3)
  860. tdi = TimedeltaIndex(["-1 Day"] * 3)
  861. dtarr = dti.values
  862. expected = DatetimeIndex(dtarr) + tdi
  863. tdi = tm.box_expected(tdi, box_with_array)
  864. expected = tm.box_expected(expected, box_with_array)
  865. result = tdi + dtarr
  866. tm.assert_equal(result, expected)
  867. result = dtarr + tdi
  868. tm.assert_equal(result, expected)
  869. # ------------------------------------------------------------------
  870. # Invalid __add__/__sub__ operations
  871. @pytest.mark.parametrize("pi_freq", ["D", "W", "Q", "h"])
  872. @pytest.mark.parametrize("tdi_freq", [None, "h"])
  873. def test_td64arr_sub_periodlike(
  874. self, box_with_array, box_with_array2, tdi_freq, pi_freq
  875. ):
  876. # GH#20049 subtracting PeriodIndex should raise TypeError
  877. tdi = TimedeltaIndex(["1 hours", "2 hours"], freq=tdi_freq)
  878. dti = Timestamp("2018-03-07 17:16:40") + tdi
  879. pi = dti.to_period(pi_freq)
  880. per = pi[0]
  881. tdi = tm.box_expected(tdi, box_with_array)
  882. pi = tm.box_expected(pi, box_with_array2)
  883. msg = "cannot subtract|unsupported operand type"
  884. with pytest.raises(TypeError, match=msg):
  885. tdi - pi
  886. # GH#13078 subtraction of Period scalar not supported
  887. with pytest.raises(TypeError, match=msg):
  888. tdi - per
  889. @pytest.mark.parametrize(
  890. "other",
  891. [
  892. # GH#12624 for str case
  893. "a",
  894. # GH#19123
  895. 1,
  896. 1.5,
  897. np.array(2),
  898. ],
  899. )
  900. def test_td64arr_addsub_numeric_scalar_invalid(self, box_with_array, other):
  901. # vector-like others are tested in test_td64arr_add_sub_numeric_arr_invalid
  902. tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
  903. tdarr = tm.box_expected(tdser, box_with_array)
  904. assert_invalid_addsub_type(tdarr, other)
  905. @pytest.mark.parametrize(
  906. "vec",
  907. [
  908. np.array([1, 2, 3]),
  909. Index([1, 2, 3]),
  910. Series([1, 2, 3]),
  911. DataFrame([[1, 2, 3]]),
  912. ],
  913. ids=lambda x: type(x).__name__,
  914. )
  915. def test_td64arr_addsub_numeric_arr_invalid(
  916. self, box_with_array, vec, any_real_numpy_dtype
  917. ):
  918. tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
  919. tdarr = tm.box_expected(tdser, box_with_array)
  920. vector = vec.astype(any_real_numpy_dtype)
  921. assert_invalid_addsub_type(tdarr, vector)
  922. def test_td64arr_add_sub_int(self, box_with_array, one):
  923. # Variants of `one` for #19012, deprecated GH#22535
  924. rng = timedelta_range("1 days 09:00:00", freq="h", periods=10)
  925. tdarr = tm.box_expected(rng, box_with_array)
  926. msg = "Addition/subtraction of integers"
  927. assert_invalid_addsub_type(tdarr, one, msg)
  928. # TODO: get inplace ops into assert_invalid_addsub_type
  929. with pytest.raises(TypeError, match=msg):
  930. tdarr += one
  931. with pytest.raises(TypeError, match=msg):
  932. tdarr -= one
  933. def test_td64arr_add_sub_integer_array(self, box_with_array):
  934. # GH#19959, deprecated GH#22535
  935. # GH#22696 for DataFrame case, check that we don't dispatch to numpy
  936. # implementation, which treats int64 as m8[ns]
  937. box = box_with_array
  938. xbox = np.ndarray if box is pd.array else box
  939. rng = timedelta_range("1 days 09:00:00", freq="h", periods=3)
  940. tdarr = tm.box_expected(rng, box)
  941. other = tm.box_expected([4, 3, 2], xbox)
  942. msg = "Addition/subtraction of integers and integer-arrays"
  943. assert_invalid_addsub_type(tdarr, other, msg)
  944. def test_td64arr_addsub_integer_array_no_freq(self, box_with_array):
  945. # GH#19959
  946. box = box_with_array
  947. xbox = np.ndarray if box is pd.array else box
  948. tdi = TimedeltaIndex(["1 Day", "NaT", "3 Hours"])
  949. tdarr = tm.box_expected(tdi, box)
  950. other = tm.box_expected([14, -1, 16], xbox)
  951. msg = "Addition/subtraction of integers"
  952. assert_invalid_addsub_type(tdarr, other, msg)
  953. # ------------------------------------------------------------------
  954. # Operations with timedelta-like others
  955. def test_td64arr_add_sub_td64_array(self, box_with_array):
  956. box = box_with_array
  957. dti = pd.date_range("2016-01-01", periods=3)
  958. tdi = dti - dti.shift(1)
  959. tdarr = tdi.values
  960. expected = 2 * tdi
  961. tdi = tm.box_expected(tdi, box)
  962. expected = tm.box_expected(expected, box)
  963. result = tdi + tdarr
  964. tm.assert_equal(result, expected)
  965. result = tdarr + tdi
  966. tm.assert_equal(result, expected)
  967. expected_sub = 0 * tdi
  968. result = tdi - tdarr
  969. tm.assert_equal(result, expected_sub)
  970. result = tdarr - tdi
  971. tm.assert_equal(result, expected_sub)
  972. def test_td64arr_add_sub_tdi(self, box_with_array, names):
  973. # GH#17250 make sure result dtype is correct
  974. # GH#19043 make sure names are propagated correctly
  975. box = box_with_array
  976. exname = get_expected_name(box, names)
  977. tdi = TimedeltaIndex(["0 days", "1 day"], name=names[1])
  978. tdi = np.array(tdi) if box in [tm.to_array, pd.array] else tdi
  979. ser = Series([Timedelta(hours=3), Timedelta(hours=4)], name=names[0])
  980. expected = Series([Timedelta(hours=3), Timedelta(days=1, hours=4)], name=exname)
  981. ser = tm.box_expected(ser, box)
  982. expected = tm.box_expected(expected, box)
  983. result = tdi + ser
  984. tm.assert_equal(result, expected)
  985. assert_dtype(result, "timedelta64[ns]")
  986. result = ser + tdi
  987. tm.assert_equal(result, expected)
  988. assert_dtype(result, "timedelta64[ns]")
  989. expected = Series(
  990. [Timedelta(hours=-3), Timedelta(days=1, hours=-4)], name=exname
  991. )
  992. expected = tm.box_expected(expected, box)
  993. result = tdi - ser
  994. tm.assert_equal(result, expected)
  995. assert_dtype(result, "timedelta64[ns]")
  996. result = ser - tdi
  997. tm.assert_equal(result, -expected)
  998. assert_dtype(result, "timedelta64[ns]")
  999. @pytest.mark.parametrize("tdnat", [np.timedelta64("NaT"), NaT])
  1000. def test_td64arr_add_sub_td64_nat(self, box_with_array, tdnat):
  1001. # GH#18808, GH#23320 special handling for timedelta64("NaT")
  1002. box = box_with_array
  1003. tdi = TimedeltaIndex([NaT, Timedelta("1s")])
  1004. expected = TimedeltaIndex(["NaT"] * 2)
  1005. obj = tm.box_expected(tdi, box)
  1006. expected = tm.box_expected(expected, box)
  1007. result = obj + tdnat
  1008. tm.assert_equal(result, expected)
  1009. result = tdnat + obj
  1010. tm.assert_equal(result, expected)
  1011. result = obj - tdnat
  1012. tm.assert_equal(result, expected)
  1013. result = tdnat - obj
  1014. tm.assert_equal(result, expected)
  1015. def test_td64arr_add_timedeltalike(self, two_hours, box_with_array):
  1016. # only test adding/sub offsets as + is now numeric
  1017. # GH#10699 for Tick cases
  1018. box = box_with_array
  1019. rng = timedelta_range("1 days", "10 days")
  1020. expected = timedelta_range("1 days 02:00:00", "10 days 02:00:00", freq="D")
  1021. rng = tm.box_expected(rng, box)
  1022. expected = tm.box_expected(expected, box)
  1023. result = rng + two_hours
  1024. tm.assert_equal(result, expected)
  1025. result = two_hours + rng
  1026. tm.assert_equal(result, expected)
  1027. def test_td64arr_sub_timedeltalike(self, two_hours, box_with_array):
  1028. # only test adding/sub offsets as - is now numeric
  1029. # GH#10699 for Tick cases
  1030. box = box_with_array
  1031. rng = timedelta_range("1 days", "10 days")
  1032. expected = timedelta_range("0 days 22:00:00", "9 days 22:00:00")
  1033. rng = tm.box_expected(rng, box)
  1034. expected = tm.box_expected(expected, box)
  1035. result = rng - two_hours
  1036. tm.assert_equal(result, expected)
  1037. result = two_hours - rng
  1038. tm.assert_equal(result, -expected)
  1039. # ------------------------------------------------------------------
  1040. # __add__/__sub__ with DateOffsets and arrays of DateOffsets
  1041. def test_td64arr_add_sub_offset_index(self, names, box_with_array):
  1042. # GH#18849, GH#19744
  1043. box = box_with_array
  1044. exname = get_expected_name(box, names)
  1045. tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"], name=names[0])
  1046. other = Index([offsets.Hour(n=1), offsets.Minute(n=-2)], name=names[1])
  1047. other = np.array(other) if box in [tm.to_array, pd.array] else other
  1048. expected = TimedeltaIndex(
  1049. [tdi[n] + other[n] for n in range(len(tdi))], freq="infer", name=exname
  1050. )
  1051. expected_sub = TimedeltaIndex(
  1052. [tdi[n] - other[n] for n in range(len(tdi))], freq="infer", name=exname
  1053. )
  1054. tdi = tm.box_expected(tdi, box)
  1055. expected = tm.box_expected(expected, box).astype(object, copy=False)
  1056. expected_sub = tm.box_expected(expected_sub, box).astype(object, copy=False)
  1057. with tm.assert_produces_warning(PerformanceWarning):
  1058. res = tdi + other
  1059. tm.assert_equal(res, expected)
  1060. with tm.assert_produces_warning(PerformanceWarning):
  1061. res2 = other + tdi
  1062. tm.assert_equal(res2, expected)
  1063. with tm.assert_produces_warning(PerformanceWarning):
  1064. res_sub = tdi - other
  1065. tm.assert_equal(res_sub, expected_sub)
  1066. def test_td64arr_add_sub_offset_array(self, box_with_array):
  1067. # GH#18849, GH#18824
  1068. box = box_with_array
  1069. tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"])
  1070. other = np.array([offsets.Hour(n=1), offsets.Minute(n=-2)])
  1071. expected = TimedeltaIndex(
  1072. [tdi[n] + other[n] for n in range(len(tdi))], freq="infer"
  1073. )
  1074. expected_sub = TimedeltaIndex(
  1075. [tdi[n] - other[n] for n in range(len(tdi))], freq="infer"
  1076. )
  1077. tdi = tm.box_expected(tdi, box)
  1078. expected = tm.box_expected(expected, box).astype(object)
  1079. with tm.assert_produces_warning(PerformanceWarning):
  1080. res = tdi + other
  1081. tm.assert_equal(res, expected)
  1082. with tm.assert_produces_warning(PerformanceWarning):
  1083. res2 = other + tdi
  1084. tm.assert_equal(res2, expected)
  1085. expected_sub = tm.box_expected(expected_sub, box_with_array).astype(object)
  1086. with tm.assert_produces_warning(PerformanceWarning):
  1087. res_sub = tdi - other
  1088. tm.assert_equal(res_sub, expected_sub)
  1089. def test_td64arr_with_offset_series(self, names, box_with_array):
  1090. # GH#18849
  1091. box = box_with_array
  1092. box2 = Series if box in [Index, tm.to_array, pd.array] else box
  1093. exname = get_expected_name(box, names)
  1094. tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"], name=names[0])
  1095. other = Series([offsets.Hour(n=1), offsets.Minute(n=-2)], name=names[1])
  1096. expected_add = Series(
  1097. [tdi[n] + other[n] for n in range(len(tdi))], name=exname, dtype=object
  1098. )
  1099. obj = tm.box_expected(tdi, box)
  1100. expected_add = tm.box_expected(expected_add, box2).astype(object)
  1101. with tm.assert_produces_warning(PerformanceWarning):
  1102. res = obj + other
  1103. tm.assert_equal(res, expected_add)
  1104. with tm.assert_produces_warning(PerformanceWarning):
  1105. res2 = other + obj
  1106. tm.assert_equal(res2, expected_add)
  1107. expected_sub = Series(
  1108. [tdi[n] - other[n] for n in range(len(tdi))], name=exname, dtype=object
  1109. )
  1110. expected_sub = tm.box_expected(expected_sub, box2).astype(object)
  1111. with tm.assert_produces_warning(PerformanceWarning):
  1112. res3 = obj - other
  1113. tm.assert_equal(res3, expected_sub)
  1114. @pytest.mark.parametrize("obox", [np.array, Index, Series])
  1115. def test_td64arr_addsub_anchored_offset_arraylike(self, obox, box_with_array):
  1116. # GH#18824
  1117. tdi = TimedeltaIndex(["1 days 00:00:00", "3 days 04:00:00"])
  1118. tdi = tm.box_expected(tdi, box_with_array)
  1119. anchored = obox([offsets.MonthEnd(), offsets.Day(n=2)])
  1120. # addition/subtraction ops with anchored offsets should issue
  1121. # a PerformanceWarning and _then_ raise a TypeError.
  1122. msg = "has incorrect type|cannot add the type MonthEnd"
  1123. with pytest.raises(TypeError, match=msg):
  1124. with tm.assert_produces_warning(PerformanceWarning):
  1125. tdi + anchored
  1126. with pytest.raises(TypeError, match=msg):
  1127. with tm.assert_produces_warning(PerformanceWarning):
  1128. anchored + tdi
  1129. with pytest.raises(TypeError, match=msg):
  1130. with tm.assert_produces_warning(PerformanceWarning):
  1131. tdi - anchored
  1132. with pytest.raises(TypeError, match=msg):
  1133. with tm.assert_produces_warning(PerformanceWarning):
  1134. anchored - tdi
  1135. # ------------------------------------------------------------------
  1136. # Unsorted
  1137. def test_td64arr_add_sub_object_array(self, box_with_array):
  1138. box = box_with_array
  1139. xbox = np.ndarray if box is pd.array else box
  1140. tdi = timedelta_range("1 day", periods=3, freq="D")
  1141. tdarr = tm.box_expected(tdi, box)
  1142. other = np.array([Timedelta(days=1), offsets.Day(2), Timestamp("2000-01-04")])
  1143. with tm.assert_produces_warning(PerformanceWarning):
  1144. result = tdarr + other
  1145. expected = Index(
  1146. [Timedelta(days=2), Timedelta(days=4), Timestamp("2000-01-07")]
  1147. )
  1148. expected = tm.box_expected(expected, xbox).astype(object)
  1149. tm.assert_equal(result, expected)
  1150. msg = "unsupported operand type|cannot subtract a datelike"
  1151. with pytest.raises(TypeError, match=msg):
  1152. with tm.assert_produces_warning(PerformanceWarning):
  1153. tdarr - other
  1154. with tm.assert_produces_warning(PerformanceWarning):
  1155. result = other - tdarr
  1156. expected = Index([Timedelta(0), Timedelta(0), Timestamp("2000-01-01")])
  1157. expected = tm.box_expected(expected, xbox).astype(object)
  1158. tm.assert_equal(result, expected)
  1159. class TestTimedeltaArraylikeMulDivOps:
  1160. # Tests for timedelta64[ns]
  1161. # __mul__, __rmul__, __div__, __rdiv__, __floordiv__, __rfloordiv__
  1162. # ------------------------------------------------------------------
  1163. # Multiplication
  1164. # organized with scalar others first, then array-like
  1165. def test_td64arr_mul_int(self, box_with_array):
  1166. idx = TimedeltaIndex(np.arange(5, dtype="int64"))
  1167. idx = tm.box_expected(idx, box_with_array)
  1168. result = idx * 1
  1169. tm.assert_equal(result, idx)
  1170. result = 1 * idx
  1171. tm.assert_equal(result, idx)
  1172. def test_td64arr_mul_tdlike_scalar_raises(self, two_hours, box_with_array):
  1173. rng = timedelta_range("1 days", "10 days", name="foo")
  1174. rng = tm.box_expected(rng, box_with_array)
  1175. msg = "|".join(
  1176. [
  1177. "argument must be an integer",
  1178. "cannot use operands with types dtype",
  1179. "Cannot multiply with",
  1180. ]
  1181. )
  1182. with pytest.raises(TypeError, match=msg):
  1183. rng * two_hours
  1184. def test_tdi_mul_int_array_zerodim(self, box_with_array):
  1185. rng5 = np.arange(5, dtype="int64")
  1186. idx = TimedeltaIndex(rng5)
  1187. expected = TimedeltaIndex(rng5 * 5)
  1188. idx = tm.box_expected(idx, box_with_array)
  1189. expected = tm.box_expected(expected, box_with_array)
  1190. result = idx * np.array(5, dtype="int64")
  1191. tm.assert_equal(result, expected)
  1192. def test_tdi_mul_int_array(self, box_with_array):
  1193. rng5 = np.arange(5, dtype="int64")
  1194. idx = TimedeltaIndex(rng5)
  1195. expected = TimedeltaIndex(rng5**2)
  1196. idx = tm.box_expected(idx, box_with_array)
  1197. expected = tm.box_expected(expected, box_with_array)
  1198. result = idx * rng5
  1199. tm.assert_equal(result, expected)
  1200. def test_tdi_mul_int_series(self, box_with_array):
  1201. box = box_with_array
  1202. xbox = Series if box in [Index, tm.to_array, pd.array] else box
  1203. idx = TimedeltaIndex(np.arange(5, dtype="int64"))
  1204. expected = TimedeltaIndex(np.arange(5, dtype="int64") ** 2)
  1205. idx = tm.box_expected(idx, box)
  1206. expected = tm.box_expected(expected, xbox)
  1207. result = idx * Series(np.arange(5, dtype="int64"))
  1208. tm.assert_equal(result, expected)
  1209. def test_tdi_mul_float_series(self, box_with_array):
  1210. box = box_with_array
  1211. xbox = Series if box in [Index, tm.to_array, pd.array] else box
  1212. idx = TimedeltaIndex(np.arange(5, dtype="int64"))
  1213. idx = tm.box_expected(idx, box)
  1214. rng5f = np.arange(5, dtype="float64")
  1215. expected = TimedeltaIndex(rng5f * (rng5f + 1.0))
  1216. expected = tm.box_expected(expected, xbox)
  1217. result = idx * Series(rng5f + 1.0)
  1218. tm.assert_equal(result, expected)
  1219. # TODO: Put Series/DataFrame in others?
  1220. @pytest.mark.parametrize(
  1221. "other",
  1222. [
  1223. np.arange(1, 11),
  1224. Index(np.arange(1, 11), np.int64),
  1225. Index(range(1, 11), np.uint64),
  1226. Index(range(1, 11), np.float64),
  1227. pd.RangeIndex(1, 11),
  1228. ],
  1229. ids=lambda x: type(x).__name__,
  1230. )
  1231. def test_tdi_rmul_arraylike(self, other, box_with_array):
  1232. box = box_with_array
  1233. tdi = TimedeltaIndex(["1 Day"] * 10)
  1234. expected = timedelta_range("1 days", "10 days")._with_freq(None)
  1235. tdi = tm.box_expected(tdi, box)
  1236. xbox = get_upcast_box(tdi, other)
  1237. expected = tm.box_expected(expected, xbox)
  1238. result = other * tdi
  1239. tm.assert_equal(result, expected)
  1240. commute = tdi * other
  1241. tm.assert_equal(commute, expected)
  1242. # ------------------------------------------------------------------
  1243. # __div__, __rdiv__
  1244. def test_td64arr_div_nat_invalid(self, box_with_array):
  1245. # don't allow division by NaT (maybe could in the future)
  1246. rng = timedelta_range("1 days", "10 days", name="foo")
  1247. rng = tm.box_expected(rng, box_with_array)
  1248. with pytest.raises(TypeError, match="unsupported operand type"):
  1249. rng / NaT
  1250. with pytest.raises(TypeError, match="Cannot divide NaTType by"):
  1251. NaT / rng
  1252. dt64nat = np.datetime64("NaT", "ns")
  1253. msg = "|".join(
  1254. [
  1255. # 'divide' on npdev as of 2021-12-18
  1256. "ufunc '(true_divide|divide)' cannot use operands",
  1257. "cannot perform __r?truediv__",
  1258. "Cannot divide datetime64 by TimedeltaArray",
  1259. ]
  1260. )
  1261. with pytest.raises(TypeError, match=msg):
  1262. rng / dt64nat
  1263. with pytest.raises(TypeError, match=msg):
  1264. dt64nat / rng
  1265. def test_td64arr_div_td64nat(self, box_with_array):
  1266. # GH#23829
  1267. box = box_with_array
  1268. xbox = np.ndarray if box is pd.array else box
  1269. rng = timedelta_range("1 days", "10 days")
  1270. rng = tm.box_expected(rng, box)
  1271. other = np.timedelta64("NaT")
  1272. expected = np.array([np.nan] * 10)
  1273. expected = tm.box_expected(expected, xbox)
  1274. result = rng / other
  1275. tm.assert_equal(result, expected)
  1276. result = other / rng
  1277. tm.assert_equal(result, expected)
  1278. def test_td64arr_div_int(self, box_with_array):
  1279. idx = TimedeltaIndex(np.arange(5, dtype="int64"))
  1280. idx = tm.box_expected(idx, box_with_array)
  1281. result = idx / 1
  1282. tm.assert_equal(result, idx)
  1283. with pytest.raises(TypeError, match="Cannot divide"):
  1284. # GH#23829
  1285. 1 / idx
  1286. def test_td64arr_div_tdlike_scalar(self, two_hours, box_with_array):
  1287. # GH#20088, GH#22163 ensure DataFrame returns correct dtype
  1288. box = box_with_array
  1289. xbox = np.ndarray if box is pd.array else box
  1290. rng = timedelta_range("1 days", "10 days", name="foo")
  1291. expected = Index((np.arange(10) + 1) * 12, dtype=np.float64, name="foo")
  1292. rng = tm.box_expected(rng, box)
  1293. expected = tm.box_expected(expected, xbox)
  1294. result = rng / two_hours
  1295. tm.assert_equal(result, expected)
  1296. result = two_hours / rng
  1297. expected = 1 / expected
  1298. tm.assert_equal(result, expected)
  1299. @pytest.mark.parametrize("m", [1, 3, 10])
  1300. @pytest.mark.parametrize("unit", ["D", "h", "m", "s", "ms", "us", "ns"])
  1301. def test_td64arr_div_td64_scalar(self, m, unit, box_with_array):
  1302. box = box_with_array
  1303. xbox = np.ndarray if box is pd.array else box
  1304. ser = Series([Timedelta(days=59)] * 3)
  1305. ser[2] = np.nan
  1306. flat = ser
  1307. ser = tm.box_expected(ser, box)
  1308. # op
  1309. expected = Series([x / np.timedelta64(m, unit) for x in flat])
  1310. expected = tm.box_expected(expected, xbox)
  1311. result = ser / np.timedelta64(m, unit)
  1312. tm.assert_equal(result, expected)
  1313. # reverse op
  1314. expected = Series([Timedelta(np.timedelta64(m, unit)) / x for x in flat])
  1315. expected = tm.box_expected(expected, xbox)
  1316. result = np.timedelta64(m, unit) / ser
  1317. tm.assert_equal(result, expected)
  1318. def test_td64arr_div_tdlike_scalar_with_nat(self, two_hours, box_with_array):
  1319. box = box_with_array
  1320. xbox = np.ndarray if box is pd.array else box
  1321. rng = TimedeltaIndex(["1 days", NaT, "2 days"], name="foo")
  1322. expected = Index([12, np.nan, 24], dtype=np.float64, name="foo")
  1323. rng = tm.box_expected(rng, box)
  1324. expected = tm.box_expected(expected, xbox)
  1325. result = rng / two_hours
  1326. tm.assert_equal(result, expected)
  1327. result = two_hours / rng
  1328. expected = 1 / expected
  1329. tm.assert_equal(result, expected)
  1330. def test_td64arr_div_td64_ndarray(self, box_with_array):
  1331. # GH#22631
  1332. box = box_with_array
  1333. xbox = np.ndarray if box is pd.array else box
  1334. rng = TimedeltaIndex(["1 days", NaT, "2 days"])
  1335. expected = Index([12, np.nan, 24], dtype=np.float64)
  1336. rng = tm.box_expected(rng, box)
  1337. expected = tm.box_expected(expected, xbox)
  1338. other = np.array([2, 4, 2], dtype="m8[h]")
  1339. result = rng / other
  1340. tm.assert_equal(result, expected)
  1341. result = rng / tm.box_expected(other, box)
  1342. tm.assert_equal(result, expected)
  1343. result = rng / other.astype(object)
  1344. tm.assert_equal(result, expected.astype(object))
  1345. result = rng / list(other)
  1346. tm.assert_equal(result, expected)
  1347. # reversed op
  1348. expected = 1 / expected
  1349. result = other / rng
  1350. tm.assert_equal(result, expected)
  1351. result = tm.box_expected(other, box) / rng
  1352. tm.assert_equal(result, expected)
  1353. result = other.astype(object) / rng
  1354. tm.assert_equal(result, expected)
  1355. result = list(other) / rng
  1356. tm.assert_equal(result, expected)
  1357. def test_tdarr_div_length_mismatch(self, box_with_array):
  1358. rng = TimedeltaIndex(["1 days", NaT, "2 days"])
  1359. mismatched = [1, 2, 3, 4]
  1360. rng = tm.box_expected(rng, box_with_array)
  1361. msg = "Cannot divide vectors|Unable to coerce to Series"
  1362. for obj in [mismatched, mismatched[:2]]:
  1363. # one shorter, one longer
  1364. for other in [obj, np.array(obj), Index(obj)]:
  1365. with pytest.raises(ValueError, match=msg):
  1366. rng / other
  1367. with pytest.raises(ValueError, match=msg):
  1368. other / rng
  1369. def test_td64_div_object_mixed_result(self, box_with_array):
  1370. # Case where we having a NaT in the result inseat of timedelta64("NaT")
  1371. # is misleading
  1372. orig = timedelta_range("1 Day", periods=3).insert(1, NaT)
  1373. tdi = tm.box_expected(orig, box_with_array, transpose=False)
  1374. other = np.array([orig[0], 1.5, 2.0, orig[2]], dtype=object)
  1375. other = tm.box_expected(other, box_with_array, transpose=False)
  1376. res = tdi / other
  1377. expected = Index([1.0, np.timedelta64("NaT", "ns"), orig[0], 1.5], dtype=object)
  1378. expected = tm.box_expected(expected, box_with_array, transpose=False)
  1379. if isinstance(expected, NumpyExtensionArray):
  1380. expected = expected.to_numpy()
  1381. tm.assert_equal(res, expected)
  1382. if box_with_array is DataFrame:
  1383. # We have a np.timedelta64(NaT), not pd.NaT
  1384. assert isinstance(res.iloc[1, 0], np.timedelta64)
  1385. res = tdi // other
  1386. expected = Index([1, np.timedelta64("NaT", "ns"), orig[0], 1], dtype=object)
  1387. expected = tm.box_expected(expected, box_with_array, transpose=False)
  1388. if isinstance(expected, NumpyExtensionArray):
  1389. expected = expected.to_numpy()
  1390. tm.assert_equal(res, expected)
  1391. if box_with_array is DataFrame:
  1392. # We have a np.timedelta64(NaT), not pd.NaT
  1393. assert isinstance(res.iloc[1, 0], np.timedelta64)
  1394. # ------------------------------------------------------------------
  1395. # __floordiv__, __rfloordiv__
  1396. def test_td64arr_floordiv_td64arr_with_nat(
  1397. self, box_with_array, using_array_manager
  1398. ):
  1399. # GH#35529
  1400. box = box_with_array
  1401. xbox = np.ndarray if box is pd.array else box
  1402. left = Series([1000, 222330, 30], dtype="timedelta64[ns]")
  1403. right = Series([1000, 222330, None], dtype="timedelta64[ns]")
  1404. left = tm.box_expected(left, box)
  1405. right = tm.box_expected(right, box)
  1406. expected = np.array([1.0, 1.0, np.nan], dtype=np.float64)
  1407. expected = tm.box_expected(expected, xbox)
  1408. if box is DataFrame and using_array_manager:
  1409. # INFO(ArrayManager) floordiv returns integer, and ArrayManager
  1410. # performs ops column-wise and thus preserves int64 dtype for
  1411. # columns without missing values
  1412. expected[[0, 1]] = expected[[0, 1]].astype("int64")
  1413. with tm.maybe_produces_warning(
  1414. RuntimeWarning, box is pd.array, check_stacklevel=False
  1415. ):
  1416. result = left // right
  1417. tm.assert_equal(result, expected)
  1418. # case that goes through __rfloordiv__ with arraylike
  1419. with tm.maybe_produces_warning(
  1420. RuntimeWarning, box is pd.array, check_stacklevel=False
  1421. ):
  1422. result = np.asarray(left) // right
  1423. tm.assert_equal(result, expected)
  1424. @pytest.mark.filterwarnings("ignore:invalid value encountered:RuntimeWarning")
  1425. def test_td64arr_floordiv_tdscalar(self, box_with_array, scalar_td):
  1426. # GH#18831, GH#19125
  1427. box = box_with_array
  1428. xbox = np.ndarray if box is pd.array else box
  1429. td = Timedelta("5m3s") # i.e. (scalar_td - 1sec) / 2
  1430. td1 = Series([td, td, NaT], dtype="m8[ns]")
  1431. td1 = tm.box_expected(td1, box, transpose=False)
  1432. expected = Series([0, 0, np.nan])
  1433. expected = tm.box_expected(expected, xbox, transpose=False)
  1434. result = td1 // scalar_td
  1435. tm.assert_equal(result, expected)
  1436. # Reversed op
  1437. expected = Series([2, 2, np.nan])
  1438. expected = tm.box_expected(expected, xbox, transpose=False)
  1439. result = scalar_td // td1
  1440. tm.assert_equal(result, expected)
  1441. # same thing buts let's be explicit about calling __rfloordiv__
  1442. result = td1.__rfloordiv__(scalar_td)
  1443. tm.assert_equal(result, expected)
  1444. def test_td64arr_floordiv_int(self, box_with_array):
  1445. idx = TimedeltaIndex(np.arange(5, dtype="int64"))
  1446. idx = tm.box_expected(idx, box_with_array)
  1447. result = idx // 1
  1448. tm.assert_equal(result, idx)
  1449. pattern = "floor_divide cannot use operands|Cannot divide int by Timedelta*"
  1450. with pytest.raises(TypeError, match=pattern):
  1451. 1 // idx
  1452. # ------------------------------------------------------------------
  1453. # mod, divmod
  1454. # TODO: operations with timedelta-like arrays, numeric arrays,
  1455. # reversed ops
  1456. def test_td64arr_mod_tdscalar(self, box_with_array, three_days):
  1457. tdi = timedelta_range("1 Day", "9 days")
  1458. tdarr = tm.box_expected(tdi, box_with_array)
  1459. expected = TimedeltaIndex(["1 Day", "2 Days", "0 Days"] * 3)
  1460. expected = tm.box_expected(expected, box_with_array)
  1461. result = tdarr % three_days
  1462. tm.assert_equal(result, expected)
  1463. warn = None
  1464. if box_with_array is DataFrame and isinstance(three_days, pd.DateOffset):
  1465. warn = PerformanceWarning
  1466. # TODO: making expected be object here a result of DataFrame.__divmod__
  1467. # being defined in a naive way that does not dispatch to the underlying
  1468. # array's __divmod__
  1469. expected = expected.astype(object)
  1470. with tm.assert_produces_warning(warn):
  1471. result = divmod(tdarr, three_days)
  1472. tm.assert_equal(result[1], expected)
  1473. tm.assert_equal(result[0], tdarr // three_days)
  1474. def test_td64arr_mod_int(self, box_with_array):
  1475. tdi = timedelta_range("1 ns", "10 ns", periods=10)
  1476. tdarr = tm.box_expected(tdi, box_with_array)
  1477. expected = TimedeltaIndex(["1 ns", "0 ns"] * 5)
  1478. expected = tm.box_expected(expected, box_with_array)
  1479. result = tdarr % 2
  1480. tm.assert_equal(result, expected)
  1481. msg = "Cannot divide int by"
  1482. with pytest.raises(TypeError, match=msg):
  1483. 2 % tdarr
  1484. result = divmod(tdarr, 2)
  1485. tm.assert_equal(result[1], expected)
  1486. tm.assert_equal(result[0], tdarr // 2)
  1487. def test_td64arr_rmod_tdscalar(self, box_with_array, three_days):
  1488. tdi = timedelta_range("1 Day", "9 days")
  1489. tdarr = tm.box_expected(tdi, box_with_array)
  1490. expected = ["0 Days", "1 Day", "0 Days"] + ["3 Days"] * 6
  1491. expected = TimedeltaIndex(expected)
  1492. expected = tm.box_expected(expected, box_with_array)
  1493. result = three_days % tdarr
  1494. tm.assert_equal(result, expected)
  1495. result = divmod(three_days, tdarr)
  1496. tm.assert_equal(result[1], expected)
  1497. tm.assert_equal(result[0], three_days // tdarr)
  1498. # ------------------------------------------------------------------
  1499. # Operations with invalid others
  1500. def test_td64arr_mul_tdscalar_invalid(self, box_with_array, scalar_td):
  1501. td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
  1502. td1.iloc[2] = np.nan
  1503. td1 = tm.box_expected(td1, box_with_array)
  1504. # check that we are getting a TypeError
  1505. # with 'operate' (from core/ops.py) for the ops that are not
  1506. # defined
  1507. pattern = "operate|unsupported|cannot|not supported"
  1508. with pytest.raises(TypeError, match=pattern):
  1509. td1 * scalar_td
  1510. with pytest.raises(TypeError, match=pattern):
  1511. scalar_td * td1
  1512. def test_td64arr_mul_too_short_raises(self, box_with_array):
  1513. idx = TimedeltaIndex(np.arange(5, dtype="int64"))
  1514. idx = tm.box_expected(idx, box_with_array)
  1515. msg = "|".join(
  1516. [
  1517. "cannot use operands with types dtype",
  1518. "Cannot multiply with unequal lengths",
  1519. "Unable to coerce to Series",
  1520. ]
  1521. )
  1522. with pytest.raises(TypeError, match=msg):
  1523. # length check before dtype check
  1524. idx * idx[:3]
  1525. with pytest.raises(ValueError, match=msg):
  1526. idx * np.array([1, 2])
  1527. def test_td64arr_mul_td64arr_raises(self, box_with_array):
  1528. idx = TimedeltaIndex(np.arange(5, dtype="int64"))
  1529. idx = tm.box_expected(idx, box_with_array)
  1530. msg = "cannot use operands with types dtype"
  1531. with pytest.raises(TypeError, match=msg):
  1532. idx * idx
  1533. # ------------------------------------------------------------------
  1534. # Operations with numeric others
  1535. def test_td64arr_mul_numeric_scalar(self, box_with_array, one):
  1536. # GH#4521
  1537. # divide/multiply by integers
  1538. tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
  1539. expected = Series(["-59 Days", "-59 Days", "NaT"], dtype="timedelta64[ns]")
  1540. tdser = tm.box_expected(tdser, box_with_array)
  1541. expected = tm.box_expected(expected, box_with_array)
  1542. result = tdser * (-one)
  1543. tm.assert_equal(result, expected)
  1544. result = (-one) * tdser
  1545. tm.assert_equal(result, expected)
  1546. expected = Series(["118 Days", "118 Days", "NaT"], dtype="timedelta64[ns]")
  1547. expected = tm.box_expected(expected, box_with_array)
  1548. result = tdser * (2 * one)
  1549. tm.assert_equal(result, expected)
  1550. result = (2 * one) * tdser
  1551. tm.assert_equal(result, expected)
  1552. @pytest.mark.parametrize("two", [2, 2.0, np.array(2), np.array(2.0)])
  1553. def test_td64arr_div_numeric_scalar(self, box_with_array, two):
  1554. # GH#4521
  1555. # divide/multiply by integers
  1556. tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
  1557. expected = Series(["29.5D", "29.5D", "NaT"], dtype="timedelta64[ns]")
  1558. tdser = tm.box_expected(tdser, box_with_array)
  1559. expected = tm.box_expected(expected, box_with_array)
  1560. result = tdser / two
  1561. tm.assert_equal(result, expected)
  1562. with pytest.raises(TypeError, match="Cannot divide"):
  1563. two / tdser
  1564. @pytest.mark.parametrize("two", [2, 2.0, np.array(2), np.array(2.0)])
  1565. def test_td64arr_floordiv_numeric_scalar(self, box_with_array, two):
  1566. tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
  1567. expected = Series(["29.5D", "29.5D", "NaT"], dtype="timedelta64[ns]")
  1568. tdser = tm.box_expected(tdser, box_with_array)
  1569. expected = tm.box_expected(expected, box_with_array)
  1570. result = tdser // two
  1571. tm.assert_equal(result, expected)
  1572. with pytest.raises(TypeError, match="Cannot divide"):
  1573. two // tdser
  1574. @pytest.mark.parametrize(
  1575. "vector",
  1576. [np.array([20, 30, 40]), Index([20, 30, 40]), Series([20, 30, 40])],
  1577. ids=lambda x: type(x).__name__,
  1578. )
  1579. def test_td64arr_rmul_numeric_array(
  1580. self,
  1581. box_with_array,
  1582. vector,
  1583. any_real_numpy_dtype,
  1584. ):
  1585. # GH#4521
  1586. # divide/multiply by integers
  1587. tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
  1588. vector = vector.astype(any_real_numpy_dtype)
  1589. expected = Series(["1180 Days", "1770 Days", "NaT"], dtype="timedelta64[ns]")
  1590. tdser = tm.box_expected(tdser, box_with_array)
  1591. xbox = get_upcast_box(tdser, vector)
  1592. expected = tm.box_expected(expected, xbox)
  1593. result = tdser * vector
  1594. tm.assert_equal(result, expected)
  1595. result = vector * tdser
  1596. tm.assert_equal(result, expected)
  1597. @pytest.mark.parametrize(
  1598. "vector",
  1599. [np.array([20, 30, 40]), Index([20, 30, 40]), Series([20, 30, 40])],
  1600. ids=lambda x: type(x).__name__,
  1601. )
  1602. def test_td64arr_div_numeric_array(
  1603. self, box_with_array, vector, any_real_numpy_dtype
  1604. ):
  1605. # GH#4521
  1606. # divide/multiply by integers
  1607. tdser = Series(["59 Days", "59 Days", "NaT"], dtype="m8[ns]")
  1608. vector = vector.astype(any_real_numpy_dtype)
  1609. expected = Series(["2.95D", "1D 23h 12m", "NaT"], dtype="timedelta64[ns]")
  1610. tdser = tm.box_expected(tdser, box_with_array)
  1611. xbox = get_upcast_box(tdser, vector)
  1612. expected = tm.box_expected(expected, xbox)
  1613. result = tdser / vector
  1614. tm.assert_equal(result, expected)
  1615. pattern = "|".join(
  1616. [
  1617. "true_divide'? cannot use operands",
  1618. "cannot perform __div__",
  1619. "cannot perform __truediv__",
  1620. "unsupported operand",
  1621. "Cannot divide",
  1622. "ufunc 'divide' cannot use operands with types",
  1623. ]
  1624. )
  1625. with pytest.raises(TypeError, match=pattern):
  1626. vector / tdser
  1627. result = tdser / vector.astype(object)
  1628. if box_with_array is DataFrame:
  1629. expected = [tdser.iloc[0, n] / vector[n] for n in range(len(vector))]
  1630. expected = tm.box_expected(expected, xbox).astype(object)
  1631. # We specifically expect timedelta64("NaT") here, not pd.NA
  1632. msg = "The 'downcast' keyword in fillna"
  1633. with tm.assert_produces_warning(FutureWarning, match=msg):
  1634. expected[2] = expected[2].fillna(
  1635. np.timedelta64("NaT", "ns"), downcast=False
  1636. )
  1637. else:
  1638. expected = [tdser[n] / vector[n] for n in range(len(tdser))]
  1639. expected = [
  1640. x if x is not NaT else np.timedelta64("NaT", "ns") for x in expected
  1641. ]
  1642. if xbox is tm.to_array:
  1643. expected = tm.to_array(expected).astype(object)
  1644. else:
  1645. expected = xbox(expected, dtype=object)
  1646. tm.assert_equal(result, expected)
  1647. with pytest.raises(TypeError, match=pattern):
  1648. vector.astype(object) / tdser
  1649. def test_td64arr_mul_int_series(self, box_with_array, names):
  1650. # GH#19042 test for correct name attachment
  1651. box = box_with_array
  1652. exname = get_expected_name(box, names)
  1653. tdi = TimedeltaIndex(
  1654. ["0days", "1day", "2days", "3days", "4days"], name=names[0]
  1655. )
  1656. # TODO: Should we be parametrizing over types for `ser` too?
  1657. ser = Series([0, 1, 2, 3, 4], dtype=np.int64, name=names[1])
  1658. expected = Series(
  1659. ["0days", "1day", "4days", "9days", "16days"],
  1660. dtype="timedelta64[ns]",
  1661. name=exname,
  1662. )
  1663. tdi = tm.box_expected(tdi, box)
  1664. xbox = get_upcast_box(tdi, ser)
  1665. expected = tm.box_expected(expected, xbox)
  1666. result = ser * tdi
  1667. tm.assert_equal(result, expected)
  1668. result = tdi * ser
  1669. tm.assert_equal(result, expected)
  1670. # TODO: Should we be parametrizing over types for `ser` too?
  1671. def test_float_series_rdiv_td64arr(self, box_with_array, names):
  1672. # GH#19042 test for correct name attachment
  1673. box = box_with_array
  1674. tdi = TimedeltaIndex(
  1675. ["0days", "1day", "2days", "3days", "4days"], name=names[0]
  1676. )
  1677. ser = Series([1.5, 3, 4.5, 6, 7.5], dtype=np.float64, name=names[1])
  1678. xname = names[2] if box not in [tm.to_array, pd.array] else names[1]
  1679. expected = Series(
  1680. [tdi[n] / ser[n] for n in range(len(ser))],
  1681. dtype="timedelta64[ns]",
  1682. name=xname,
  1683. )
  1684. tdi = tm.box_expected(tdi, box)
  1685. xbox = get_upcast_box(tdi, ser)
  1686. expected = tm.box_expected(expected, xbox)
  1687. result = ser.__rtruediv__(tdi)
  1688. if box is DataFrame:
  1689. assert result is NotImplemented
  1690. else:
  1691. tm.assert_equal(result, expected)
  1692. def test_td64arr_all_nat_div_object_dtype_numeric(self, box_with_array):
  1693. # GH#39750 make sure we infer the result as td64
  1694. tdi = TimedeltaIndex([NaT, NaT])
  1695. left = tm.box_expected(tdi, box_with_array)
  1696. right = np.array([2, 2.0], dtype=object)
  1697. tdnat = np.timedelta64("NaT", "ns")
  1698. expected = Index([tdnat] * 2, dtype=object)
  1699. if box_with_array is not Index:
  1700. expected = tm.box_expected(expected, box_with_array).astype(object)
  1701. if box_with_array in [Series, DataFrame]:
  1702. msg = "The 'downcast' keyword in fillna is deprecated"
  1703. with tm.assert_produces_warning(FutureWarning, match=msg):
  1704. expected = expected.fillna(tdnat, downcast=False) # GH#18463
  1705. result = left / right
  1706. tm.assert_equal(result, expected)
  1707. result = left // right
  1708. tm.assert_equal(result, expected)
  1709. class TestTimedelta64ArrayLikeArithmetic:
  1710. # Arithmetic tests for timedelta64[ns] vectors fully parametrized over
  1711. # DataFrame/Series/TimedeltaIndex/TimedeltaArray. Ideally all arithmetic
  1712. # tests will eventually end up here.
  1713. def test_td64arr_pow_invalid(self, scalar_td, box_with_array):
  1714. td1 = Series([timedelta(minutes=5, seconds=3)] * 3)
  1715. td1.iloc[2] = np.nan
  1716. td1 = tm.box_expected(td1, box_with_array)
  1717. # check that we are getting a TypeError
  1718. # with 'operate' (from core/ops.py) for the ops that are not
  1719. # defined
  1720. pattern = "operate|unsupported|cannot|not supported"
  1721. with pytest.raises(TypeError, match=pattern):
  1722. scalar_td**td1
  1723. with pytest.raises(TypeError, match=pattern):
  1724. td1**scalar_td
  1725. def test_add_timestamp_to_timedelta():
  1726. # GH: 35897
  1727. timestamp = Timestamp("2021-01-01")
  1728. result = timestamp + timedelta_range("0s", "1s", periods=31)
  1729. expected = DatetimeIndex(
  1730. [
  1731. timestamp
  1732. + (
  1733. pd.to_timedelta("0.033333333s") * i
  1734. + pd.to_timedelta("0.000000001s") * divmod(i, 3)[0]
  1735. )
  1736. for i in range(31)
  1737. ]
  1738. )
  1739. tm.assert_index_equal(result, expected)