test_datetime_index.py 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225
  1. from datetime import datetime
  2. from functools import partial
  3. import numpy as np
  4. import pytest
  5. import pytz
  6. from pandas._libs import lib
  7. from pandas._typing import DatetimeNaTType
  8. from pandas.compat import is_platform_windows
  9. import pandas.util._test_decorators as td
  10. import pandas as pd
  11. from pandas import (
  12. DataFrame,
  13. Index,
  14. Series,
  15. Timedelta,
  16. Timestamp,
  17. isna,
  18. notna,
  19. )
  20. import pandas._testing as tm
  21. from pandas.core.groupby.grouper import Grouper
  22. from pandas.core.indexes.datetimes import date_range
  23. from pandas.core.indexes.period import (
  24. Period,
  25. period_range,
  26. )
  27. from pandas.core.resample import (
  28. DatetimeIndex,
  29. _get_timestamp_range_edges,
  30. )
  31. from pandas.tseries import offsets
  32. from pandas.tseries.offsets import Minute
  33. @pytest.fixture()
  34. def _index_factory():
  35. return date_range
  36. @pytest.fixture
  37. def _index_freq():
  38. return "Min"
  39. @pytest.fixture
  40. def _static_values(index):
  41. return np.random.default_rng(2).random(len(index))
  42. @pytest.fixture(params=["s", "ms", "us", "ns"])
  43. def unit(request):
  44. return request.param
  45. @pytest.fixture
  46. def simple_date_range_series():
  47. """
  48. Series with date range index and random data for test purposes.
  49. """
  50. def _simple_date_range_series(start, end, freq="D"):
  51. rng = date_range(start, end, freq=freq)
  52. return Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  53. return _simple_date_range_series
  54. def test_custom_grouper(index, unit):
  55. dti = index.as_unit(unit)
  56. s = Series(np.array([1] * len(dti)), index=dti, dtype="int64")
  57. b = Grouper(freq=Minute(5))
  58. g = s.groupby(b)
  59. # check all cython functions work
  60. g.ohlc() # doesn't use _cython_agg_general
  61. funcs = ["sum", "mean", "prod", "min", "max", "var"]
  62. for f in funcs:
  63. g._cython_agg_general(f, alt=None, numeric_only=True)
  64. b = Grouper(freq=Minute(5), closed="right", label="right")
  65. g = s.groupby(b)
  66. # check all cython functions work
  67. g.ohlc() # doesn't use _cython_agg_general
  68. funcs = ["sum", "mean", "prod", "min", "max", "var"]
  69. for f in funcs:
  70. g._cython_agg_general(f, alt=None, numeric_only=True)
  71. assert g.ngroups == 2593
  72. assert notna(g.mean()).all()
  73. # construct expected val
  74. arr = [1] + [5] * 2592
  75. idx = dti[0:-1:5]
  76. idx = idx.append(dti[-1:])
  77. idx = DatetimeIndex(idx, freq="5min").as_unit(unit)
  78. expect = Series(arr, index=idx)
  79. # GH2763 - return input dtype if we can
  80. result = g.agg("sum")
  81. tm.assert_series_equal(result, expect)
  82. def test_custom_grouper_df(index, unit):
  83. b = Grouper(freq=Minute(5), closed="right", label="right")
  84. dti = index.as_unit(unit)
  85. df = DataFrame(
  86. np.random.default_rng(2).random((len(dti), 10)), index=dti, dtype="float64"
  87. )
  88. r = df.groupby(b).agg("sum")
  89. assert len(r.columns) == 10
  90. assert len(r.index) == 2593
  91. @pytest.mark.parametrize(
  92. "_index_start,_index_end,_index_name",
  93. [("1/1/2000 00:00:00", "1/1/2000 00:13:00", "index")],
  94. )
  95. @pytest.mark.parametrize(
  96. "closed, expected",
  97. [
  98. (
  99. "right",
  100. lambda s: Series(
  101. [s.iloc[0], s[1:6].mean(), s[6:11].mean(), s[11:].mean()],
  102. index=date_range("1/1/2000", periods=4, freq="5min", name="index"),
  103. ),
  104. ),
  105. (
  106. "left",
  107. lambda s: Series(
  108. [s[:5].mean(), s[5:10].mean(), s[10:].mean()],
  109. index=date_range(
  110. "1/1/2000 00:05", periods=3, freq="5min", name="index"
  111. ),
  112. ),
  113. ),
  114. ],
  115. )
  116. def test_resample_basic(series, closed, expected, unit):
  117. s = series
  118. s.index = s.index.as_unit(unit)
  119. expected = expected(s)
  120. expected.index = expected.index.as_unit(unit)
  121. result = s.resample("5min", closed=closed, label="right").mean()
  122. tm.assert_series_equal(result, expected)
  123. def test_resample_integerarray(unit):
  124. # GH 25580, resample on IntegerArray
  125. ts = Series(
  126. range(9),
  127. index=date_range("1/1/2000", periods=9, freq="min").as_unit(unit),
  128. dtype="Int64",
  129. )
  130. result = ts.resample("3min").sum()
  131. expected = Series(
  132. [3, 12, 21],
  133. index=date_range("1/1/2000", periods=3, freq="3min").as_unit(unit),
  134. dtype="Int64",
  135. )
  136. tm.assert_series_equal(result, expected)
  137. result = ts.resample("3min").mean()
  138. expected = Series(
  139. [1, 4, 7],
  140. index=date_range("1/1/2000", periods=3, freq="3min").as_unit(unit),
  141. dtype="Float64",
  142. )
  143. tm.assert_series_equal(result, expected)
  144. def test_resample_basic_grouper(series, unit):
  145. s = series
  146. s.index = s.index.as_unit(unit)
  147. result = s.resample("5Min").last()
  148. grouper = Grouper(freq=Minute(5), closed="left", label="left")
  149. expected = s.groupby(grouper).agg(lambda x: x.iloc[-1])
  150. tm.assert_series_equal(result, expected)
  151. @pytest.mark.parametrize(
  152. "_index_start,_index_end,_index_name",
  153. [("1/1/2000 00:00:00", "1/1/2000 00:13:00", "index")],
  154. )
  155. @pytest.mark.parametrize(
  156. "keyword,value",
  157. [("label", "righttt"), ("closed", "righttt"), ("convention", "starttt")],
  158. )
  159. def test_resample_string_kwargs(series, keyword, value, unit):
  160. # see gh-19303
  161. # Check that wrong keyword argument strings raise an error
  162. series.index = series.index.as_unit(unit)
  163. msg = f"Unsupported value {value} for `{keyword}`"
  164. with pytest.raises(ValueError, match=msg):
  165. series.resample("5min", **({keyword: value}))
  166. @pytest.mark.parametrize(
  167. "_index_start,_index_end,_index_name",
  168. [("1/1/2000 00:00:00", "1/1/2000 00:13:00", "index")],
  169. )
  170. def test_resample_how(series, downsample_method, unit):
  171. if downsample_method == "ohlc":
  172. pytest.skip("covered by test_resample_how_ohlc")
  173. s = series
  174. s.index = s.index.as_unit(unit)
  175. grouplist = np.ones_like(s)
  176. grouplist[0] = 0
  177. grouplist[1:6] = 1
  178. grouplist[6:11] = 2
  179. grouplist[11:] = 3
  180. expected = s.groupby(grouplist).agg(downsample_method)
  181. expected.index = date_range(
  182. "1/1/2000", periods=4, freq="5min", name="index"
  183. ).as_unit(unit)
  184. result = getattr(
  185. s.resample("5min", closed="right", label="right"), downsample_method
  186. )()
  187. tm.assert_series_equal(result, expected)
  188. @pytest.mark.parametrize(
  189. "_index_start,_index_end,_index_name",
  190. [("1/1/2000 00:00:00", "1/1/2000 00:13:00", "index")],
  191. )
  192. def test_resample_how_ohlc(series, unit):
  193. s = series
  194. s.index = s.index.as_unit(unit)
  195. grouplist = np.ones_like(s)
  196. grouplist[0] = 0
  197. grouplist[1:6] = 1
  198. grouplist[6:11] = 2
  199. grouplist[11:] = 3
  200. def _ohlc(group):
  201. if isna(group).all():
  202. return np.repeat(np.nan, 4)
  203. return [group.iloc[0], group.max(), group.min(), group.iloc[-1]]
  204. expected = DataFrame(
  205. s.groupby(grouplist).agg(_ohlc).values.tolist(),
  206. index=date_range("1/1/2000", periods=4, freq="5min", name="index").as_unit(
  207. unit
  208. ),
  209. columns=["open", "high", "low", "close"],
  210. )
  211. result = s.resample("5min", closed="right", label="right").ohlc()
  212. tm.assert_frame_equal(result, expected)
  213. def test_resample_how_callables(unit):
  214. # GH#7929
  215. data = np.arange(5, dtype=np.int64)
  216. ind = date_range(start="2014-01-01", periods=len(data), freq="d").as_unit(unit)
  217. df = DataFrame({"A": data, "B": data}, index=ind)
  218. def fn(x, a=1):
  219. return str(type(x))
  220. class FnClass:
  221. def __call__(self, x):
  222. return str(type(x))
  223. df_standard = df.resample("ME").apply(fn)
  224. df_lambda = df.resample("ME").apply(lambda x: str(type(x)))
  225. df_partial = df.resample("ME").apply(partial(fn))
  226. df_partial2 = df.resample("ME").apply(partial(fn, a=2))
  227. df_class = df.resample("ME").apply(FnClass())
  228. tm.assert_frame_equal(df_standard, df_lambda)
  229. tm.assert_frame_equal(df_standard, df_partial)
  230. tm.assert_frame_equal(df_standard, df_partial2)
  231. tm.assert_frame_equal(df_standard, df_class)
  232. def test_resample_rounding(unit):
  233. # GH 8371
  234. # odd results when rounding is needed
  235. ts = [
  236. "2014-11-08 00:00:01",
  237. "2014-11-08 00:00:02",
  238. "2014-11-08 00:00:02",
  239. "2014-11-08 00:00:03",
  240. "2014-11-08 00:00:07",
  241. "2014-11-08 00:00:07",
  242. "2014-11-08 00:00:08",
  243. "2014-11-08 00:00:08",
  244. "2014-11-08 00:00:08",
  245. "2014-11-08 00:00:09",
  246. "2014-11-08 00:00:10",
  247. "2014-11-08 00:00:11",
  248. "2014-11-08 00:00:11",
  249. "2014-11-08 00:00:13",
  250. "2014-11-08 00:00:14",
  251. "2014-11-08 00:00:15",
  252. "2014-11-08 00:00:17",
  253. "2014-11-08 00:00:20",
  254. "2014-11-08 00:00:21",
  255. ]
  256. df = DataFrame({"value": [1] * 19}, index=pd.to_datetime(ts))
  257. df.index = df.index.as_unit(unit)
  258. result = df.resample("6s").sum()
  259. expected = DataFrame(
  260. {"value": [4, 9, 4, 2]},
  261. index=date_range("2014-11-08", freq="6s", periods=4).as_unit(unit),
  262. )
  263. tm.assert_frame_equal(result, expected)
  264. result = df.resample("7s").sum()
  265. expected = DataFrame(
  266. {"value": [4, 10, 4, 1]},
  267. index=date_range("2014-11-08", freq="7s", periods=4).as_unit(unit),
  268. )
  269. tm.assert_frame_equal(result, expected)
  270. result = df.resample("11s").sum()
  271. expected = DataFrame(
  272. {"value": [11, 8]},
  273. index=date_range("2014-11-08", freq="11s", periods=2).as_unit(unit),
  274. )
  275. tm.assert_frame_equal(result, expected)
  276. result = df.resample("13s").sum()
  277. expected = DataFrame(
  278. {"value": [13, 6]},
  279. index=date_range("2014-11-08", freq="13s", periods=2).as_unit(unit),
  280. )
  281. tm.assert_frame_equal(result, expected)
  282. result = df.resample("17s").sum()
  283. expected = DataFrame(
  284. {"value": [16, 3]},
  285. index=date_range("2014-11-08", freq="17s", periods=2).as_unit(unit),
  286. )
  287. tm.assert_frame_equal(result, expected)
  288. def test_resample_basic_from_daily(unit):
  289. # from daily
  290. dti = date_range(
  291. start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq="D", name="index"
  292. ).as_unit(unit)
  293. s = Series(np.random.default_rng(2).random(len(dti)), dti)
  294. # to weekly
  295. result = s.resample("w-sun").last()
  296. assert len(result) == 3
  297. assert (result.index.dayofweek == [6, 6, 6]).all()
  298. assert result.iloc[0] == s["1/2/2005"]
  299. assert result.iloc[1] == s["1/9/2005"]
  300. assert result.iloc[2] == s.iloc[-1]
  301. result = s.resample("W-MON").last()
  302. assert len(result) == 2
  303. assert (result.index.dayofweek == [0, 0]).all()
  304. assert result.iloc[0] == s["1/3/2005"]
  305. assert result.iloc[1] == s["1/10/2005"]
  306. result = s.resample("W-TUE").last()
  307. assert len(result) == 2
  308. assert (result.index.dayofweek == [1, 1]).all()
  309. assert result.iloc[0] == s["1/4/2005"]
  310. assert result.iloc[1] == s["1/10/2005"]
  311. result = s.resample("W-WED").last()
  312. assert len(result) == 2
  313. assert (result.index.dayofweek == [2, 2]).all()
  314. assert result.iloc[0] == s["1/5/2005"]
  315. assert result.iloc[1] == s["1/10/2005"]
  316. result = s.resample("W-THU").last()
  317. assert len(result) == 2
  318. assert (result.index.dayofweek == [3, 3]).all()
  319. assert result.iloc[0] == s["1/6/2005"]
  320. assert result.iloc[1] == s["1/10/2005"]
  321. result = s.resample("W-FRI").last()
  322. assert len(result) == 2
  323. assert (result.index.dayofweek == [4, 4]).all()
  324. assert result.iloc[0] == s["1/7/2005"]
  325. assert result.iloc[1] == s["1/10/2005"]
  326. # to biz day
  327. result = s.resample("B").last()
  328. assert len(result) == 7
  329. assert (result.index.dayofweek == [4, 0, 1, 2, 3, 4, 0]).all()
  330. assert result.iloc[0] == s["1/2/2005"]
  331. assert result.iloc[1] == s["1/3/2005"]
  332. assert result.iloc[5] == s["1/9/2005"]
  333. assert result.index.name == "index"
  334. def test_resample_upsampling_picked_but_not_correct(unit):
  335. # Test for issue #3020
  336. dates = date_range("01-Jan-2014", "05-Jan-2014", freq="D").as_unit(unit)
  337. series = Series(1, index=dates)
  338. result = series.resample("D").mean()
  339. assert result.index[0] == dates[0]
  340. # GH 5955
  341. # incorrect deciding to upsample when the axis frequency matches the
  342. # resample frequency
  343. s = Series(
  344. np.arange(1.0, 6), index=[datetime(1975, 1, i, 12, 0) for i in range(1, 6)]
  345. )
  346. s.index = s.index.as_unit(unit)
  347. expected = Series(
  348. np.arange(1.0, 6),
  349. index=date_range("19750101", periods=5, freq="D").as_unit(unit),
  350. )
  351. result = s.resample("D").count()
  352. tm.assert_series_equal(result, Series(1, index=expected.index))
  353. result1 = s.resample("D").sum()
  354. result2 = s.resample("D").mean()
  355. tm.assert_series_equal(result1, expected)
  356. tm.assert_series_equal(result2, expected)
  357. @pytest.mark.parametrize("f", ["sum", "mean", "prod", "min", "max", "var"])
  358. def test_resample_frame_basic_cy_funcs(f, unit):
  359. df = DataFrame(
  360. np.random.default_rng(2).standard_normal((50, 4)),
  361. columns=Index(list("ABCD"), dtype=object),
  362. index=date_range("2000-01-01", periods=50, freq="B"),
  363. )
  364. df.index = df.index.as_unit(unit)
  365. b = Grouper(freq="ME")
  366. g = df.groupby(b)
  367. # check all cython functions work
  368. g._cython_agg_general(f, alt=None, numeric_only=True)
  369. @pytest.mark.parametrize("freq", ["YE", "ME"])
  370. def test_resample_frame_basic_M_A(freq, unit):
  371. df = DataFrame(
  372. np.random.default_rng(2).standard_normal((50, 4)),
  373. columns=Index(list("ABCD"), dtype=object),
  374. index=date_range("2000-01-01", periods=50, freq="B"),
  375. )
  376. df.index = df.index.as_unit(unit)
  377. result = df.resample(freq).mean()
  378. tm.assert_series_equal(result["A"], df["A"].resample(freq).mean())
  379. @pytest.mark.parametrize("freq", ["W-WED", "ME"])
  380. def test_resample_frame_basic_kind(freq, unit):
  381. df = DataFrame(
  382. np.random.default_rng(2).standard_normal((10, 4)),
  383. columns=Index(list("ABCD"), dtype=object),
  384. index=date_range("2000-01-01", periods=10, freq="B"),
  385. )
  386. df.index = df.index.as_unit(unit)
  387. msg = "The 'kind' keyword in DataFrame.resample is deprecated"
  388. with tm.assert_produces_warning(FutureWarning, match=msg):
  389. df.resample(freq, kind="period").mean()
  390. def test_resample_upsample(unit):
  391. # from daily
  392. dti = date_range(
  393. start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq="D", name="index"
  394. ).as_unit(unit)
  395. s = Series(np.random.default_rng(2).random(len(dti)), dti)
  396. # to minutely, by padding
  397. result = s.resample("Min").ffill()
  398. assert len(result) == 12961
  399. assert result.iloc[0] == s.iloc[0]
  400. assert result.iloc[-1] == s.iloc[-1]
  401. assert result.index.name == "index"
  402. def test_resample_how_method(unit):
  403. # GH9915
  404. s = Series(
  405. [11, 22],
  406. index=[
  407. Timestamp("2015-03-31 21:48:52.672000"),
  408. Timestamp("2015-03-31 21:49:52.739000"),
  409. ],
  410. )
  411. s.index = s.index.as_unit(unit)
  412. expected = Series(
  413. [11, np.nan, np.nan, np.nan, np.nan, np.nan, 22],
  414. index=DatetimeIndex(
  415. [
  416. Timestamp("2015-03-31 21:48:50"),
  417. Timestamp("2015-03-31 21:49:00"),
  418. Timestamp("2015-03-31 21:49:10"),
  419. Timestamp("2015-03-31 21:49:20"),
  420. Timestamp("2015-03-31 21:49:30"),
  421. Timestamp("2015-03-31 21:49:40"),
  422. Timestamp("2015-03-31 21:49:50"),
  423. ],
  424. freq="10s",
  425. ),
  426. )
  427. expected.index = expected.index.as_unit(unit)
  428. tm.assert_series_equal(s.resample("10s").mean(), expected)
  429. def test_resample_extra_index_point(unit):
  430. # GH#9756
  431. index = date_range(start="20150101", end="20150331", freq="BME").as_unit(unit)
  432. expected = DataFrame({"A": Series([21, 41, 63], index=index)})
  433. index = date_range(start="20150101", end="20150331", freq="B").as_unit(unit)
  434. df = DataFrame({"A": Series(range(len(index)), index=index)}, dtype="int64")
  435. result = df.resample("BME").last()
  436. tm.assert_frame_equal(result, expected)
  437. def test_upsample_with_limit(unit):
  438. rng = date_range("1/1/2000", periods=3, freq="5min").as_unit(unit)
  439. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), rng)
  440. result = ts.resample("min").ffill(limit=2)
  441. expected = ts.reindex(result.index, method="ffill", limit=2)
  442. tm.assert_series_equal(result, expected)
  443. @pytest.mark.parametrize("freq", ["1D", "10h", "5Min", "10s"])
  444. @pytest.mark.parametrize("rule", ["YE", "3ME", "15D", "30h", "15Min", "30s"])
  445. def test_nearest_upsample_with_limit(tz_aware_fixture, freq, rule, unit):
  446. # GH 33939
  447. rng = date_range("1/1/2000", periods=3, freq=freq, tz=tz_aware_fixture).as_unit(
  448. unit
  449. )
  450. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), rng)
  451. result = ts.resample(rule).nearest(limit=2)
  452. expected = ts.reindex(result.index, method="nearest", limit=2)
  453. tm.assert_series_equal(result, expected)
  454. def test_resample_ohlc(series, unit):
  455. s = series
  456. s.index = s.index.as_unit(unit)
  457. grouper = Grouper(freq=Minute(5))
  458. expect = s.groupby(grouper).agg(lambda x: x.iloc[-1])
  459. result = s.resample("5Min").ohlc()
  460. assert len(result) == len(expect)
  461. assert len(result.columns) == 4
  462. xs = result.iloc[-2]
  463. assert xs["open"] == s.iloc[-6]
  464. assert xs["high"] == s[-6:-1].max()
  465. assert xs["low"] == s[-6:-1].min()
  466. assert xs["close"] == s.iloc[-2]
  467. xs = result.iloc[0]
  468. assert xs["open"] == s.iloc[0]
  469. assert xs["high"] == s[:5].max()
  470. assert xs["low"] == s[:5].min()
  471. assert xs["close"] == s.iloc[4]
  472. def test_resample_ohlc_result(unit):
  473. # GH 12332
  474. index = date_range("1-1-2000", "2-15-2000", freq="h").as_unit(unit)
  475. index = index.union(date_range("4-15-2000", "5-15-2000", freq="h").as_unit(unit))
  476. s = Series(range(len(index)), index=index)
  477. a = s.loc[:"4-15-2000"].resample("30min").ohlc()
  478. assert isinstance(a, DataFrame)
  479. b = s.loc[:"4-14-2000"].resample("30min").ohlc()
  480. assert isinstance(b, DataFrame)
  481. def test_resample_ohlc_result_odd_period(unit):
  482. # GH12348
  483. # raising on odd period
  484. rng = date_range("2013-12-30", "2014-01-07").as_unit(unit)
  485. index = rng.drop(
  486. [
  487. Timestamp("2014-01-01"),
  488. Timestamp("2013-12-31"),
  489. Timestamp("2014-01-04"),
  490. Timestamp("2014-01-05"),
  491. ]
  492. )
  493. df = DataFrame(data=np.arange(len(index)), index=index)
  494. result = df.resample("B").mean()
  495. expected = df.reindex(index=date_range(rng[0], rng[-1], freq="B").as_unit(unit))
  496. tm.assert_frame_equal(result, expected)
  497. def test_resample_ohlc_dataframe(unit):
  498. df = (
  499. DataFrame(
  500. {
  501. "PRICE": {
  502. Timestamp("2011-01-06 10:59:05", tz=None): 24990,
  503. Timestamp("2011-01-06 12:43:33", tz=None): 25499,
  504. Timestamp("2011-01-06 12:54:09", tz=None): 25499,
  505. },
  506. "VOLUME": {
  507. Timestamp("2011-01-06 10:59:05", tz=None): 1500000000,
  508. Timestamp("2011-01-06 12:43:33", tz=None): 5000000000,
  509. Timestamp("2011-01-06 12:54:09", tz=None): 100000000,
  510. },
  511. }
  512. )
  513. ).reindex(["VOLUME", "PRICE"], axis=1)
  514. df.index = df.index.as_unit(unit)
  515. df.columns.name = "Cols"
  516. res = df.resample("h").ohlc()
  517. exp = pd.concat(
  518. [df["VOLUME"].resample("h").ohlc(), df["PRICE"].resample("h").ohlc()],
  519. axis=1,
  520. keys=df.columns,
  521. )
  522. assert exp.columns.names[0] == "Cols"
  523. tm.assert_frame_equal(exp, res)
  524. df.columns = [["a", "b"], ["c", "d"]]
  525. res = df.resample("h").ohlc()
  526. exp.columns = pd.MultiIndex.from_tuples(
  527. [
  528. ("a", "c", "open"),
  529. ("a", "c", "high"),
  530. ("a", "c", "low"),
  531. ("a", "c", "close"),
  532. ("b", "d", "open"),
  533. ("b", "d", "high"),
  534. ("b", "d", "low"),
  535. ("b", "d", "close"),
  536. ]
  537. )
  538. tm.assert_frame_equal(exp, res)
  539. # dupe columns fail atm
  540. # df.columns = ['PRICE', 'PRICE']
  541. def test_resample_dup_index():
  542. # GH 4812
  543. # dup columns with resample raising
  544. df = DataFrame(
  545. np.random.default_rng(2).standard_normal((4, 12)),
  546. index=[2000, 2000, 2000, 2000],
  547. columns=[Period(year=2000, month=i + 1, freq="M") for i in range(12)],
  548. )
  549. df.iloc[3, :] = np.nan
  550. warning_msg = "DataFrame.resample with axis=1 is deprecated."
  551. with tm.assert_produces_warning(FutureWarning, match=warning_msg):
  552. result = df.resample("QE", axis=1).mean()
  553. msg = "DataFrame.groupby with axis=1 is deprecated"
  554. with tm.assert_produces_warning(FutureWarning, match=msg):
  555. expected = df.groupby(lambda x: int((x.month - 1) / 3), axis=1).mean()
  556. expected.columns = [Period(year=2000, quarter=i + 1, freq="Q") for i in range(4)]
  557. tm.assert_frame_equal(result, expected)
  558. def test_resample_reresample(unit):
  559. dti = date_range(
  560. start=datetime(2005, 1, 1), end=datetime(2005, 1, 10), freq="D"
  561. ).as_unit(unit)
  562. s = Series(np.random.default_rng(2).random(len(dti)), dti)
  563. bs = s.resample("B", closed="right", label="right").mean()
  564. result = bs.resample("8h").mean()
  565. assert len(result) == 25
  566. assert isinstance(result.index.freq, offsets.DateOffset)
  567. assert result.index.freq == offsets.Hour(8)
  568. @pytest.mark.parametrize(
  569. "freq, expected_kwargs",
  570. [
  571. ["YE-DEC", {"start": "1990", "end": "2000", "freq": "Y-DEC"}],
  572. ["YE-JUN", {"start": "1990", "end": "2000", "freq": "Y-JUN"}],
  573. ["ME", {"start": "1990-01", "end": "2000-01", "freq": "M"}],
  574. ],
  575. )
  576. def test_resample_timestamp_to_period(
  577. simple_date_range_series, freq, expected_kwargs, unit
  578. ):
  579. ts = simple_date_range_series("1/1/1990", "1/1/2000")
  580. ts.index = ts.index.as_unit(unit)
  581. msg = "The 'kind' keyword in Series.resample is deprecated"
  582. with tm.assert_produces_warning(FutureWarning, match=msg):
  583. result = ts.resample(freq, kind="period").mean()
  584. expected = ts.resample(freq).mean()
  585. expected.index = period_range(**expected_kwargs)
  586. tm.assert_series_equal(result, expected)
  587. def test_ohlc_5min(unit):
  588. def _ohlc(group):
  589. if isna(group).all():
  590. return np.repeat(np.nan, 4)
  591. return [group.iloc[0], group.max(), group.min(), group.iloc[-1]]
  592. rng = date_range("1/1/2000 00:00:00", "1/1/2000 5:59:50", freq="10s").as_unit(unit)
  593. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  594. resampled = ts.resample("5min", closed="right", label="right").ohlc()
  595. assert (resampled.loc["1/1/2000 00:00"] == ts.iloc[0]).all()
  596. exp = _ohlc(ts[1:31])
  597. assert (resampled.loc["1/1/2000 00:05"] == exp).all()
  598. exp = _ohlc(ts["1/1/2000 5:55:01":])
  599. assert (resampled.loc["1/1/2000 6:00:00"] == exp).all()
  600. def test_downsample_non_unique(unit):
  601. rng = date_range("1/1/2000", "2/29/2000").as_unit(unit)
  602. rng2 = rng.repeat(5).values
  603. ts = Series(np.random.default_rng(2).standard_normal(len(rng2)), index=rng2)
  604. result = ts.resample("ME").mean()
  605. expected = ts.groupby(lambda x: x.month).mean()
  606. assert len(result) == 2
  607. tm.assert_almost_equal(result.iloc[0], expected[1])
  608. tm.assert_almost_equal(result.iloc[1], expected[2])
  609. def test_asfreq_non_unique(unit):
  610. # GH #1077
  611. rng = date_range("1/1/2000", "2/29/2000").as_unit(unit)
  612. rng2 = rng.repeat(2).values
  613. ts = Series(np.random.default_rng(2).standard_normal(len(rng2)), index=rng2)
  614. msg = "cannot reindex on an axis with duplicate labels"
  615. with pytest.raises(ValueError, match=msg):
  616. ts.asfreq("B")
  617. def test_resample_axis1(unit):
  618. rng = date_range("1/1/2000", "2/29/2000").as_unit(unit)
  619. df = DataFrame(
  620. np.random.default_rng(2).standard_normal((3, len(rng))),
  621. columns=rng,
  622. index=["a", "b", "c"],
  623. )
  624. warning_msg = "DataFrame.resample with axis=1 is deprecated."
  625. with tm.assert_produces_warning(FutureWarning, match=warning_msg):
  626. result = df.resample("ME", axis=1).mean()
  627. expected = df.T.resample("ME").mean().T
  628. tm.assert_frame_equal(result, expected)
  629. @pytest.mark.parametrize("freq", ["min", "5min", "15min", "30min", "4h", "12h"])
  630. def test_resample_anchored_ticks(freq, unit):
  631. # If a fixed delta (5 minute, 4 hour) evenly divides a day, we should
  632. # "anchor" the origin at midnight so we get regular intervals rather
  633. # than starting from the first timestamp which might start in the
  634. # middle of a desired interval
  635. rng = date_range("1/1/2000 04:00:00", periods=86400, freq="s").as_unit(unit)
  636. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  637. ts[:2] = np.nan # so results are the same
  638. result = ts[2:].resample(freq, closed="left", label="left").mean()
  639. expected = ts.resample(freq, closed="left", label="left").mean()
  640. tm.assert_series_equal(result, expected)
  641. @pytest.mark.parametrize("end", [1, 2])
  642. def test_resample_single_group(end, unit):
  643. mysum = lambda x: x.sum()
  644. rng = date_range("2000-1-1", f"2000-{end}-10", freq="D").as_unit(unit)
  645. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  646. tm.assert_series_equal(ts.resample("ME").sum(), ts.resample("ME").apply(mysum))
  647. def test_resample_single_group_std(unit):
  648. # GH 3849
  649. s = Series(
  650. [30.1, 31.6],
  651. index=[Timestamp("20070915 15:30:00"), Timestamp("20070915 15:40:00")],
  652. )
  653. s.index = s.index.as_unit(unit)
  654. expected = Series(
  655. [0.75], index=DatetimeIndex([Timestamp("20070915")], freq="D").as_unit(unit)
  656. )
  657. result = s.resample("D").apply(lambda x: np.std(x))
  658. tm.assert_series_equal(result, expected)
  659. def test_resample_offset(unit):
  660. # GH 31809
  661. rng = date_range("1/1/2000 00:00:00", "1/1/2000 02:00", freq="s").as_unit(unit)
  662. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  663. resampled = ts.resample("5min", offset="2min").mean()
  664. exp_rng = date_range("12/31/1999 23:57:00", "1/1/2000 01:57", freq="5min").as_unit(
  665. unit
  666. )
  667. tm.assert_index_equal(resampled.index, exp_rng)
  668. @pytest.mark.parametrize(
  669. "kwargs",
  670. [
  671. {"origin": "1999-12-31 23:57:00"},
  672. {"origin": Timestamp("1970-01-01 00:02:00")},
  673. {"origin": "epoch", "offset": "2m"},
  674. # origin of '1999-31-12 12:02:00' should be equivalent for this case
  675. {"origin": "1999-12-31 12:02:00"},
  676. {"offset": "-3m"},
  677. ],
  678. )
  679. def test_resample_origin(kwargs, unit):
  680. # GH 31809
  681. rng = date_range("2000-01-01 00:00:00", "2000-01-01 02:00", freq="s").as_unit(unit)
  682. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  683. exp_rng = date_range(
  684. "1999-12-31 23:57:00", "2000-01-01 01:57", freq="5min"
  685. ).as_unit(unit)
  686. resampled = ts.resample("5min", **kwargs).mean()
  687. tm.assert_index_equal(resampled.index, exp_rng)
  688. @pytest.mark.parametrize(
  689. "origin", ["invalid_value", "epch", "startday", "startt", "2000-30-30", object()]
  690. )
  691. def test_resample_bad_origin(origin, unit):
  692. rng = date_range("2000-01-01 00:00:00", "2000-01-01 02:00", freq="s").as_unit(unit)
  693. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  694. msg = (
  695. "'origin' should be equal to 'epoch', 'start', 'start_day', "
  696. "'end', 'end_day' or should be a Timestamp convertible type. Got "
  697. f"'{origin}' instead."
  698. )
  699. with pytest.raises(ValueError, match=msg):
  700. ts.resample("5min", origin=origin)
  701. @pytest.mark.parametrize("offset", ["invalid_value", "12dayys", "2000-30-30", object()])
  702. def test_resample_bad_offset(offset, unit):
  703. rng = date_range("2000-01-01 00:00:00", "2000-01-01 02:00", freq="s").as_unit(unit)
  704. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  705. msg = f"'offset' should be a Timedelta convertible type. Got '{offset}' instead."
  706. with pytest.raises(ValueError, match=msg):
  707. ts.resample("5min", offset=offset)
  708. def test_resample_origin_prime_freq(unit):
  709. # GH 31809
  710. start, end = "2000-10-01 23:30:00", "2000-10-02 00:30:00"
  711. rng = date_range(start, end, freq="7min").as_unit(unit)
  712. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  713. exp_rng = date_range(
  714. "2000-10-01 23:14:00", "2000-10-02 00:22:00", freq="17min"
  715. ).as_unit(unit)
  716. resampled = ts.resample("17min").mean()
  717. tm.assert_index_equal(resampled.index, exp_rng)
  718. resampled = ts.resample("17min", origin="start_day").mean()
  719. tm.assert_index_equal(resampled.index, exp_rng)
  720. exp_rng = date_range(
  721. "2000-10-01 23:30:00", "2000-10-02 00:21:00", freq="17min"
  722. ).as_unit(unit)
  723. resampled = ts.resample("17min", origin="start").mean()
  724. tm.assert_index_equal(resampled.index, exp_rng)
  725. resampled = ts.resample("17min", offset="23h30min").mean()
  726. tm.assert_index_equal(resampled.index, exp_rng)
  727. resampled = ts.resample("17min", origin="start_day", offset="23h30min").mean()
  728. tm.assert_index_equal(resampled.index, exp_rng)
  729. exp_rng = date_range(
  730. "2000-10-01 23:18:00", "2000-10-02 00:26:00", freq="17min"
  731. ).as_unit(unit)
  732. resampled = ts.resample("17min", origin="epoch").mean()
  733. tm.assert_index_equal(resampled.index, exp_rng)
  734. exp_rng = date_range(
  735. "2000-10-01 23:24:00", "2000-10-02 00:15:00", freq="17min"
  736. ).as_unit(unit)
  737. resampled = ts.resample("17min", origin="2000-01-01").mean()
  738. tm.assert_index_equal(resampled.index, exp_rng)
  739. def test_resample_origin_with_tz(unit):
  740. # GH 31809
  741. msg = "The origin must have the same timezone as the index."
  742. tz = "Europe/Paris"
  743. rng = date_range(
  744. "2000-01-01 00:00:00", "2000-01-01 02:00", freq="s", tz=tz
  745. ).as_unit(unit)
  746. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  747. exp_rng = date_range(
  748. "1999-12-31 23:57:00", "2000-01-01 01:57", freq="5min", tz=tz
  749. ).as_unit(unit)
  750. resampled = ts.resample("5min", origin="1999-12-31 23:57:00+00:00").mean()
  751. tm.assert_index_equal(resampled.index, exp_rng)
  752. # origin of '1999-31-12 12:02:00+03:00' should be equivalent for this case
  753. resampled = ts.resample("5min", origin="1999-12-31 12:02:00+03:00").mean()
  754. tm.assert_index_equal(resampled.index, exp_rng)
  755. resampled = ts.resample("5min", origin="epoch", offset="2m").mean()
  756. tm.assert_index_equal(resampled.index, exp_rng)
  757. with pytest.raises(ValueError, match=msg):
  758. ts.resample("5min", origin="12/31/1999 23:57:00").mean()
  759. # if the series is not tz aware, origin should not be tz aware
  760. rng = date_range("2000-01-01 00:00:00", "2000-01-01 02:00", freq="s").as_unit(unit)
  761. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  762. with pytest.raises(ValueError, match=msg):
  763. ts.resample("5min", origin="12/31/1999 23:57:00+03:00").mean()
  764. def test_resample_origin_epoch_with_tz_day_vs_24h(unit):
  765. # GH 34474
  766. start, end = "2000-10-01 23:30:00+0500", "2000-12-02 00:30:00+0500"
  767. rng = date_range(start, end, freq="7min").as_unit(unit)
  768. random_values = np.random.default_rng(2).standard_normal(len(rng))
  769. ts_1 = Series(random_values, index=rng)
  770. result_1 = ts_1.resample("D", origin="epoch").mean()
  771. result_2 = ts_1.resample("24h", origin="epoch").mean()
  772. tm.assert_series_equal(result_1, result_2)
  773. # check that we have the same behavior with epoch even if we are not timezone aware
  774. ts_no_tz = ts_1.tz_localize(None)
  775. result_3 = ts_no_tz.resample("D", origin="epoch").mean()
  776. result_4 = ts_no_tz.resample("24h", origin="epoch").mean()
  777. tm.assert_series_equal(result_1, result_3.tz_localize(rng.tz), check_freq=False)
  778. tm.assert_series_equal(result_1, result_4.tz_localize(rng.tz), check_freq=False)
  779. # check that we have the similar results with two different timezones (+2H and +5H)
  780. start, end = "2000-10-01 23:30:00+0200", "2000-12-02 00:30:00+0200"
  781. rng = date_range(start, end, freq="7min").as_unit(unit)
  782. ts_2 = Series(random_values, index=rng)
  783. result_5 = ts_2.resample("D", origin="epoch").mean()
  784. result_6 = ts_2.resample("24h", origin="epoch").mean()
  785. tm.assert_series_equal(result_1.tz_localize(None), result_5.tz_localize(None))
  786. tm.assert_series_equal(result_1.tz_localize(None), result_6.tz_localize(None))
  787. def test_resample_origin_with_day_freq_on_dst(unit):
  788. # GH 31809
  789. tz = "America/Chicago"
  790. def _create_series(values, timestamps, freq="D"):
  791. return Series(
  792. values,
  793. index=DatetimeIndex(
  794. [Timestamp(t, tz=tz) for t in timestamps], freq=freq, ambiguous=True
  795. ).as_unit(unit),
  796. )
  797. # test classical behavior of origin in a DST context
  798. start = Timestamp("2013-11-02", tz=tz)
  799. end = Timestamp("2013-11-03 23:59", tz=tz)
  800. rng = date_range(start, end, freq="1h").as_unit(unit)
  801. ts = Series(np.ones(len(rng)), index=rng)
  802. expected = _create_series([24.0, 25.0], ["2013-11-02", "2013-11-03"])
  803. for origin in ["epoch", "start", "start_day", start, None]:
  804. result = ts.resample("D", origin=origin).sum()
  805. tm.assert_series_equal(result, expected)
  806. # test complex behavior of origin/offset in a DST context
  807. start = Timestamp("2013-11-03", tz=tz)
  808. end = Timestamp("2013-11-03 23:59", tz=tz)
  809. rng = date_range(start, end, freq="1h").as_unit(unit)
  810. ts = Series(np.ones(len(rng)), index=rng)
  811. expected_ts = ["2013-11-02 22:00-05:00", "2013-11-03 22:00-06:00"]
  812. expected = _create_series([23.0, 2.0], expected_ts)
  813. result = ts.resample("D", origin="start", offset="-2h").sum()
  814. tm.assert_series_equal(result, expected)
  815. expected_ts = ["2013-11-02 22:00-05:00", "2013-11-03 21:00-06:00"]
  816. expected = _create_series([22.0, 3.0], expected_ts, freq="24h")
  817. result = ts.resample("24h", origin="start", offset="-2h").sum()
  818. tm.assert_series_equal(result, expected)
  819. expected_ts = ["2013-11-02 02:00-05:00", "2013-11-03 02:00-06:00"]
  820. expected = _create_series([3.0, 22.0], expected_ts)
  821. result = ts.resample("D", origin="start", offset="2h").sum()
  822. tm.assert_series_equal(result, expected)
  823. expected_ts = ["2013-11-02 23:00-05:00", "2013-11-03 23:00-06:00"]
  824. expected = _create_series([24.0, 1.0], expected_ts)
  825. result = ts.resample("D", origin="start", offset="-1h").sum()
  826. tm.assert_series_equal(result, expected)
  827. expected_ts = ["2013-11-02 01:00-05:00", "2013-11-03 01:00:00-0500"]
  828. expected = _create_series([1.0, 24.0], expected_ts)
  829. result = ts.resample("D", origin="start", offset="1h").sum()
  830. tm.assert_series_equal(result, expected)
  831. def test_resample_daily_anchored(unit):
  832. rng = date_range("1/1/2000 0:00:00", periods=10000, freq="min").as_unit(unit)
  833. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  834. ts[:2] = np.nan # so results are the same
  835. result = ts[2:].resample("D", closed="left", label="left").mean()
  836. expected = ts.resample("D", closed="left", label="left").mean()
  837. tm.assert_series_equal(result, expected)
  838. def test_resample_to_period_monthly_buglet(unit):
  839. # GH #1259
  840. rng = date_range("1/1/2000", "12/31/2000").as_unit(unit)
  841. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  842. msg = "The 'kind' keyword in Series.resample is deprecated"
  843. with tm.assert_produces_warning(FutureWarning, match=msg):
  844. result = ts.resample("ME", kind="period").mean()
  845. exp_index = period_range("Jan-2000", "Dec-2000", freq="M")
  846. tm.assert_index_equal(result.index, exp_index)
  847. def test_period_with_agg():
  848. # aggregate a period resampler with a lambda
  849. s2 = Series(
  850. np.random.default_rng(2).integers(0, 5, 50),
  851. index=period_range("2012-01-01", freq="h", periods=50),
  852. dtype="float64",
  853. )
  854. expected = s2.to_timestamp().resample("D").mean().to_period()
  855. result = s2.resample("D").agg(lambda x: x.mean())
  856. tm.assert_series_equal(result, expected)
  857. def test_resample_segfault(unit):
  858. # GH 8573
  859. # segfaulting in older versions
  860. all_wins_and_wagers = [
  861. (1, datetime(2013, 10, 1, 16, 20), 1, 0),
  862. (2, datetime(2013, 10, 1, 16, 10), 1, 0),
  863. (2, datetime(2013, 10, 1, 18, 15), 1, 0),
  864. (2, datetime(2013, 10, 1, 16, 10, 31), 1, 0),
  865. ]
  866. df = DataFrame.from_records(
  867. all_wins_and_wagers, columns=("ID", "timestamp", "A", "B")
  868. ).set_index("timestamp")
  869. df.index = df.index.as_unit(unit)
  870. msg = "DataFrameGroupBy.resample operated on the grouping columns"
  871. with tm.assert_produces_warning(FutureWarning, match=msg):
  872. result = df.groupby("ID").resample("5min").sum()
  873. msg = "DataFrameGroupBy.apply operated on the grouping columns"
  874. with tm.assert_produces_warning(FutureWarning, match=msg):
  875. expected = df.groupby("ID").apply(lambda x: x.resample("5min").sum())
  876. tm.assert_frame_equal(result, expected)
  877. def test_resample_dtype_preservation(unit):
  878. # GH 12202
  879. # validation tests for dtype preservation
  880. df = DataFrame(
  881. {
  882. "date": date_range(start="2016-01-01", periods=4, freq="W").as_unit(unit),
  883. "group": [1, 1, 2, 2],
  884. "val": Series([5, 6, 7, 8], dtype="int32"),
  885. }
  886. ).set_index("date")
  887. result = df.resample("1D").ffill()
  888. assert result.val.dtype == np.int32
  889. msg = "DataFrameGroupBy.resample operated on the grouping columns"
  890. with tm.assert_produces_warning(FutureWarning, match=msg):
  891. result = df.groupby("group").resample("1D").ffill()
  892. assert result.val.dtype == np.int32
  893. def test_resample_dtype_coercion(unit):
  894. pytest.importorskip("scipy.interpolate")
  895. # GH 16361
  896. df = {"a": [1, 3, 1, 4]}
  897. df = DataFrame(df, index=date_range("2017-01-01", "2017-01-04").as_unit(unit))
  898. expected = df.astype("float64").resample("h").mean()["a"].interpolate("cubic")
  899. result = df.resample("h")["a"].mean().interpolate("cubic")
  900. tm.assert_series_equal(result, expected)
  901. result = df.resample("h").mean()["a"].interpolate("cubic")
  902. tm.assert_series_equal(result, expected)
  903. def test_weekly_resample_buglet(unit):
  904. # #1327
  905. rng = date_range("1/1/2000", freq="B", periods=20).as_unit(unit)
  906. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  907. resampled = ts.resample("W").mean()
  908. expected = ts.resample("W-SUN").mean()
  909. tm.assert_series_equal(resampled, expected)
  910. def test_monthly_resample_error(unit):
  911. # #1451
  912. dates = date_range("4/16/2012 20:00", periods=5000, freq="h").as_unit(unit)
  913. ts = Series(np.random.default_rng(2).standard_normal(len(dates)), index=dates)
  914. # it works!
  915. ts.resample("ME")
  916. def test_nanosecond_resample_error():
  917. # GH 12307 - Values falls after last bin when
  918. # Resampling using pd.tseries.offsets.Nano as period
  919. start = 1443707890427
  920. exp_start = 1443707890400
  921. indx = date_range(start=pd.to_datetime(start), periods=10, freq="100ns")
  922. ts = Series(range(len(indx)), index=indx)
  923. r = ts.resample(pd.tseries.offsets.Nano(100))
  924. result = r.agg("mean")
  925. exp_indx = date_range(start=pd.to_datetime(exp_start), periods=10, freq="100ns")
  926. exp = Series(range(len(exp_indx)), index=exp_indx, dtype=float)
  927. tm.assert_series_equal(result, exp)
  928. def test_resample_anchored_intraday(unit):
  929. # #1471, #1458
  930. rng = date_range("1/1/2012", "4/1/2012", freq="100min").as_unit(unit)
  931. df = DataFrame(rng.month, index=rng)
  932. result = df.resample("ME").mean()
  933. msg = "The 'kind' keyword in DataFrame.resample is deprecated"
  934. with tm.assert_produces_warning(FutureWarning, match=msg):
  935. expected = df.resample("ME", kind="period").mean().to_timestamp(how="end")
  936. expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
  937. expected.index = expected.index.as_unit(unit)._with_freq("infer")
  938. assert expected.index.freq == "ME"
  939. tm.assert_frame_equal(result, expected)
  940. result = df.resample("ME", closed="left").mean()
  941. msg = "The 'kind' keyword in DataFrame.resample is deprecated"
  942. with tm.assert_produces_warning(FutureWarning, match=msg):
  943. exp = df.shift(1, freq="D").resample("ME", kind="period").mean()
  944. exp = exp.to_timestamp(how="end")
  945. exp.index = exp.index + Timedelta(1, "ns") - Timedelta(1, "D")
  946. exp.index = exp.index.as_unit(unit)._with_freq("infer")
  947. assert exp.index.freq == "ME"
  948. tm.assert_frame_equal(result, exp)
  949. def test_resample_anchored_intraday2(unit):
  950. rng = date_range("1/1/2012", "4/1/2012", freq="100min").as_unit(unit)
  951. df = DataFrame(rng.month, index=rng)
  952. result = df.resample("QE").mean()
  953. msg = "The 'kind' keyword in DataFrame.resample is deprecated"
  954. with tm.assert_produces_warning(FutureWarning, match=msg):
  955. expected = df.resample("QE", kind="period").mean().to_timestamp(how="end")
  956. expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
  957. expected.index._data.freq = "QE"
  958. expected.index._freq = lib.no_default
  959. expected.index = expected.index.as_unit(unit)
  960. tm.assert_frame_equal(result, expected)
  961. result = df.resample("QE", closed="left").mean()
  962. msg = "The 'kind' keyword in DataFrame.resample is deprecated"
  963. with tm.assert_produces_warning(FutureWarning, match=msg):
  964. expected = (
  965. df.shift(1, freq="D").resample("QE", kind="period", closed="left").mean()
  966. )
  967. expected = expected.to_timestamp(how="end")
  968. expected.index += Timedelta(1, "ns") - Timedelta(1, "D")
  969. expected.index._data.freq = "QE"
  970. expected.index._freq = lib.no_default
  971. expected.index = expected.index.as_unit(unit)
  972. tm.assert_frame_equal(result, expected)
  973. def test_resample_anchored_intraday3(simple_date_range_series, unit):
  974. ts = simple_date_range_series("2012-04-29 23:00", "2012-04-30 5:00", freq="h")
  975. ts.index = ts.index.as_unit(unit)
  976. resampled = ts.resample("ME").mean()
  977. assert len(resampled) == 1
  978. @pytest.mark.parametrize("freq", ["MS", "BMS", "QS-MAR", "YS-DEC", "YS-JUN"])
  979. def test_resample_anchored_monthstart(simple_date_range_series, freq, unit):
  980. ts = simple_date_range_series("1/1/2000", "12/31/2002")
  981. ts.index = ts.index.as_unit(unit)
  982. ts.resample(freq).mean()
  983. @pytest.mark.parametrize("label, sec", [[None, 2.0], ["right", "4.2"]])
  984. def test_resample_anchored_multiday(label, sec):
  985. # When resampling a range spanning multiple days, ensure that the
  986. # start date gets used to determine the offset. Fixes issue where
  987. # a one day period is not a multiple of the frequency.
  988. #
  989. # See: https://github.com/pandas-dev/pandas/issues/8683
  990. index1 = date_range("2014-10-14 23:06:23.206", periods=3, freq="400ms")
  991. index2 = date_range("2014-10-15 23:00:00", periods=2, freq="2200ms")
  992. index = index1.union(index2)
  993. s = Series(np.random.default_rng(2).standard_normal(5), index=index)
  994. # Ensure left closing works
  995. result = s.resample("2200ms", label=label).mean()
  996. assert result.index[-1] == Timestamp(f"2014-10-15 23:00:{sec}00")
  997. def test_corner_cases(unit):
  998. # miscellaneous test coverage
  999. rng = date_range("1/1/2000", periods=12, freq="min").as_unit(unit)
  1000. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  1001. result = ts.resample("5min", closed="right", label="left").mean()
  1002. ex_index = date_range("1999-12-31 23:55", periods=4, freq="5min").as_unit(unit)
  1003. tm.assert_index_equal(result.index, ex_index)
  1004. def test_corner_cases_date(simple_date_range_series, unit):
  1005. # resample to periods
  1006. ts = simple_date_range_series("2000-04-28", "2000-04-30 11:00", freq="h")
  1007. ts.index = ts.index.as_unit(unit)
  1008. msg = "The 'kind' keyword in Series.resample is deprecated"
  1009. with tm.assert_produces_warning(FutureWarning, match=msg):
  1010. result = ts.resample("ME", kind="period").mean()
  1011. assert len(result) == 1
  1012. assert result.index[0] == Period("2000-04", freq="M")
  1013. def test_anchored_lowercase_buglet(unit):
  1014. dates = date_range("4/16/2012 20:00", periods=50000, freq="s").as_unit(unit)
  1015. ts = Series(np.random.default_rng(2).standard_normal(len(dates)), index=dates)
  1016. # it works!
  1017. ts.resample("d").mean()
  1018. def test_upsample_apply_functions(unit):
  1019. # #1596
  1020. rng = date_range("2012-06-12", periods=4, freq="h").as_unit(unit)
  1021. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  1022. result = ts.resample("20min").aggregate(["mean", "sum"])
  1023. assert isinstance(result, DataFrame)
  1024. def test_resample_not_monotonic(unit):
  1025. rng = date_range("2012-06-12", periods=200, freq="h").as_unit(unit)
  1026. ts = Series(np.random.default_rng(2).standard_normal(len(rng)), index=rng)
  1027. ts = ts.take(np.random.default_rng(2).permutation(len(ts)))
  1028. result = ts.resample("D").sum()
  1029. exp = ts.sort_index().resample("D").sum()
  1030. tm.assert_series_equal(result, exp)
  1031. @pytest.mark.parametrize(
  1032. "dtype",
  1033. [
  1034. "int64",
  1035. "int32",
  1036. "float64",
  1037. pytest.param(
  1038. "float32",
  1039. marks=pytest.mark.xfail(
  1040. reason="Empty groups cause x.mean() to return float64"
  1041. ),
  1042. ),
  1043. ],
  1044. )
  1045. def test_resample_median_bug_1688(dtype, unit):
  1046. # GH#55958
  1047. dti = DatetimeIndex(
  1048. [datetime(2012, 1, 1, 0, 0, 0), datetime(2012, 1, 1, 0, 5, 0)]
  1049. ).as_unit(unit)
  1050. df = DataFrame(
  1051. [1, 2],
  1052. index=dti,
  1053. dtype=dtype,
  1054. )
  1055. result = df.resample("min").apply(lambda x: x.mean())
  1056. exp = df.asfreq("min")
  1057. tm.assert_frame_equal(result, exp)
  1058. result = df.resample("min").median()
  1059. exp = df.asfreq("min")
  1060. tm.assert_frame_equal(result, exp)
  1061. def test_how_lambda_functions(simple_date_range_series, unit):
  1062. ts = simple_date_range_series("1/1/2000", "4/1/2000")
  1063. ts.index = ts.index.as_unit(unit)
  1064. result = ts.resample("ME").apply(lambda x: x.mean())
  1065. exp = ts.resample("ME").mean()
  1066. tm.assert_series_equal(result, exp)
  1067. foo_exp = ts.resample("ME").mean()
  1068. foo_exp.name = "foo"
  1069. bar_exp = ts.resample("ME").std()
  1070. bar_exp.name = "bar"
  1071. result = ts.resample("ME").apply([lambda x: x.mean(), lambda x: x.std(ddof=1)])
  1072. result.columns = ["foo", "bar"]
  1073. tm.assert_series_equal(result["foo"], foo_exp)
  1074. tm.assert_series_equal(result["bar"], bar_exp)
  1075. # this is a MI Series, so comparing the names of the results
  1076. # doesn't make sense
  1077. result = ts.resample("ME").aggregate(
  1078. {"foo": lambda x: x.mean(), "bar": lambda x: x.std(ddof=1)}
  1079. )
  1080. tm.assert_series_equal(result["foo"], foo_exp, check_names=False)
  1081. tm.assert_series_equal(result["bar"], bar_exp, check_names=False)
  1082. def test_resample_unequal_times(unit):
  1083. # #1772
  1084. start = datetime(1999, 3, 1, 5)
  1085. # end hour is less than start
  1086. end = datetime(2012, 7, 31, 4)
  1087. bad_ind = date_range(start, end, freq="30min").as_unit(unit)
  1088. df = DataFrame({"close": 1}, index=bad_ind)
  1089. # it works!
  1090. df.resample("YS").sum()
  1091. def test_resample_consistency(unit):
  1092. # GH 6418
  1093. # resample with bfill / limit / reindex consistency
  1094. i30 = date_range("2002-02-02", periods=4, freq="30min").as_unit(unit)
  1095. s = Series(np.arange(4.0), index=i30)
  1096. s.iloc[2] = np.nan
  1097. # Upsample by factor 3 with reindex() and resample() methods:
  1098. i10 = date_range(i30[0], i30[-1], freq="10min").as_unit(unit)
  1099. s10 = s.reindex(index=i10, method="bfill")
  1100. s10_2 = s.reindex(index=i10, method="bfill", limit=2)
  1101. rl = s.reindex_like(s10, method="bfill", limit=2)
  1102. r10_2 = s.resample("10Min").bfill(limit=2)
  1103. r10 = s.resample("10Min").bfill()
  1104. # s10_2, r10, r10_2, rl should all be equal
  1105. tm.assert_series_equal(s10_2, r10)
  1106. tm.assert_series_equal(s10_2, r10_2)
  1107. tm.assert_series_equal(s10_2, rl)
  1108. dates1: list[DatetimeNaTType] = [
  1109. datetime(2014, 10, 1),
  1110. datetime(2014, 9, 3),
  1111. datetime(2014, 11, 5),
  1112. datetime(2014, 9, 5),
  1113. datetime(2014, 10, 8),
  1114. datetime(2014, 7, 15),
  1115. ]
  1116. dates2: list[DatetimeNaTType] = (
  1117. dates1[:2] + [pd.NaT] + dates1[2:4] + [pd.NaT] + dates1[4:]
  1118. )
  1119. dates3 = [pd.NaT] + dates1 + [pd.NaT]
  1120. @pytest.mark.parametrize("dates", [dates1, dates2, dates3])
  1121. def test_resample_timegrouper(dates, unit):
  1122. # GH 7227
  1123. dates = DatetimeIndex(dates).as_unit(unit)
  1124. df = DataFrame({"A": dates, "B": np.arange(len(dates))})
  1125. result = df.set_index("A").resample("ME").count()
  1126. exp_idx = DatetimeIndex(
  1127. ["2014-07-31", "2014-08-31", "2014-09-30", "2014-10-31", "2014-11-30"],
  1128. freq="ME",
  1129. name="A",
  1130. ).as_unit(unit)
  1131. expected = DataFrame({"B": [1, 0, 2, 2, 1]}, index=exp_idx)
  1132. if df["A"].isna().any():
  1133. expected.index = expected.index._with_freq(None)
  1134. tm.assert_frame_equal(result, expected)
  1135. result = df.groupby(Grouper(freq="ME", key="A")).count()
  1136. tm.assert_frame_equal(result, expected)
  1137. @pytest.mark.parametrize("dates", [dates1, dates2, dates3])
  1138. def test_resample_timegrouper2(dates, unit):
  1139. dates = DatetimeIndex(dates).as_unit(unit)
  1140. df = DataFrame({"A": dates, "B": np.arange(len(dates)), "C": np.arange(len(dates))})
  1141. result = df.set_index("A").resample("ME").count()
  1142. exp_idx = DatetimeIndex(
  1143. ["2014-07-31", "2014-08-31", "2014-09-30", "2014-10-31", "2014-11-30"],
  1144. freq="ME",
  1145. name="A",
  1146. ).as_unit(unit)
  1147. expected = DataFrame(
  1148. {"B": [1, 0, 2, 2, 1], "C": [1, 0, 2, 2, 1]},
  1149. index=exp_idx,
  1150. columns=["B", "C"],
  1151. )
  1152. if df["A"].isna().any():
  1153. expected.index = expected.index._with_freq(None)
  1154. tm.assert_frame_equal(result, expected)
  1155. result = df.groupby(Grouper(freq="ME", key="A")).count()
  1156. tm.assert_frame_equal(result, expected)
  1157. def test_resample_nunique(unit):
  1158. # GH 12352
  1159. df = DataFrame(
  1160. {
  1161. "ID": {
  1162. Timestamp("2015-06-05 00:00:00"): "0010100903",
  1163. Timestamp("2015-06-08 00:00:00"): "0010150847",
  1164. },
  1165. "DATE": {
  1166. Timestamp("2015-06-05 00:00:00"): "2015-06-05",
  1167. Timestamp("2015-06-08 00:00:00"): "2015-06-08",
  1168. },
  1169. }
  1170. )
  1171. df.index = df.index.as_unit(unit)
  1172. r = df.resample("D")
  1173. g = df.groupby(Grouper(freq="D"))
  1174. expected = df.groupby(Grouper(freq="D")).ID.apply(lambda x: x.nunique())
  1175. assert expected.name == "ID"
  1176. for t in [r, g]:
  1177. result = t.ID.nunique()
  1178. tm.assert_series_equal(result, expected)
  1179. result = df.ID.resample("D").nunique()
  1180. tm.assert_series_equal(result, expected)
  1181. result = df.ID.groupby(Grouper(freq="D")).nunique()
  1182. tm.assert_series_equal(result, expected)
  1183. def test_resample_nunique_preserves_column_level_names(unit):
  1184. # see gh-23222
  1185. df = DataFrame(
  1186. np.random.default_rng(2).standard_normal((5, 4)),
  1187. columns=Index(list("ABCD"), dtype=object),
  1188. index=date_range("2000-01-01", periods=5, freq="D"),
  1189. ).abs()
  1190. df.index = df.index.as_unit(unit)
  1191. df.columns = pd.MultiIndex.from_arrays(
  1192. [df.columns.tolist()] * 2, names=["lev0", "lev1"]
  1193. )
  1194. result = df.resample("1h").nunique()
  1195. tm.assert_index_equal(df.columns, result.columns)
  1196. @pytest.mark.parametrize(
  1197. "func",
  1198. [
  1199. lambda x: x.nunique(),
  1200. lambda x: x.agg(Series.nunique),
  1201. lambda x: x.agg("nunique"),
  1202. ],
  1203. ids=["nunique", "series_nunique", "nunique_str"],
  1204. )
  1205. def test_resample_nunique_with_date_gap(func, unit):
  1206. # GH 13453
  1207. # Since all elements are unique, these should all be the same
  1208. index = date_range("1-1-2000", "2-15-2000", freq="h").as_unit(unit)
  1209. index2 = date_range("4-15-2000", "5-15-2000", freq="h").as_unit(unit)
  1210. index3 = index.append(index2)
  1211. s = Series(range(len(index3)), index=index3, dtype="int64")
  1212. r = s.resample("ME")
  1213. result = r.count()
  1214. expected = func(r)
  1215. tm.assert_series_equal(result, expected)
  1216. @pytest.mark.parametrize("n", [10000, 100000])
  1217. @pytest.mark.parametrize("k", [10, 100, 1000])
  1218. def test_resample_group_info(n, k, unit):
  1219. # GH10914
  1220. # use a fixed seed to always have the same uniques
  1221. prng = np.random.default_rng(2)
  1222. dr = date_range(start="2015-08-27", periods=n // 10, freq="min").as_unit(unit)
  1223. ts = Series(prng.integers(0, n // k, n).astype("int64"), index=prng.choice(dr, n))
  1224. left = ts.resample("30min").nunique()
  1225. ix = date_range(start=ts.index.min(), end=ts.index.max(), freq="30min").as_unit(
  1226. unit
  1227. )
  1228. vals = ts.values
  1229. bins = np.searchsorted(ix.values, ts.index, side="right")
  1230. sorter = np.lexsort((vals, bins))
  1231. vals, bins = vals[sorter], bins[sorter]
  1232. mask = np.r_[True, vals[1:] != vals[:-1]]
  1233. mask |= np.r_[True, bins[1:] != bins[:-1]]
  1234. arr = np.bincount(bins[mask] - 1, minlength=len(ix)).astype("int64", copy=False)
  1235. right = Series(arr, index=ix)
  1236. tm.assert_series_equal(left, right)
  1237. def test_resample_size(unit):
  1238. n = 10000
  1239. dr = date_range("2015-09-19", periods=n, freq="min").as_unit(unit)
  1240. ts = Series(
  1241. np.random.default_rng(2).standard_normal(n),
  1242. index=np.random.default_rng(2).choice(dr, n),
  1243. )
  1244. left = ts.resample("7min").size()
  1245. ix = date_range(start=left.index.min(), end=ts.index.max(), freq="7min").as_unit(
  1246. unit
  1247. )
  1248. bins = np.searchsorted(ix.values, ts.index.values, side="right")
  1249. val = np.bincount(bins, minlength=len(ix) + 1)[1:].astype("int64", copy=False)
  1250. right = Series(val, index=ix)
  1251. tm.assert_series_equal(left, right)
  1252. def test_resample_across_dst():
  1253. # The test resamples a DatetimeIndex with values before and after a
  1254. # DST change
  1255. # Issue: 14682
  1256. # The DatetimeIndex we will start with
  1257. # (note that DST happens at 03:00+02:00 -> 02:00+01:00)
  1258. # 2016-10-30 02:23:00+02:00, 2016-10-30 02:23:00+01:00
  1259. df1 = DataFrame([1477786980, 1477790580], columns=["ts"])
  1260. dti1 = DatetimeIndex(
  1261. pd.to_datetime(df1.ts, unit="s")
  1262. .dt.tz_localize("UTC")
  1263. .dt.tz_convert("Europe/Madrid")
  1264. )
  1265. # The expected DatetimeIndex after resampling.
  1266. # 2016-10-30 02:00:00+02:00, 2016-10-30 02:00:00+01:00
  1267. df2 = DataFrame([1477785600, 1477789200], columns=["ts"])
  1268. dti2 = DatetimeIndex(
  1269. pd.to_datetime(df2.ts, unit="s")
  1270. .dt.tz_localize("UTC")
  1271. .dt.tz_convert("Europe/Madrid"),
  1272. freq="h",
  1273. )
  1274. df = DataFrame([5, 5], index=dti1)
  1275. result = df.resample(rule="h").sum()
  1276. expected = DataFrame([5, 5], index=dti2)
  1277. tm.assert_frame_equal(result, expected)
  1278. def test_groupby_with_dst_time_change(unit):
  1279. # GH 24972
  1280. index = (
  1281. DatetimeIndex([1478064900001000000, 1480037118776792000], tz="UTC")
  1282. .tz_convert("America/Chicago")
  1283. .as_unit(unit)
  1284. )
  1285. df = DataFrame([1, 2], index=index)
  1286. result = df.groupby(Grouper(freq="1d")).last()
  1287. expected_index_values = date_range(
  1288. "2016-11-02", "2016-11-24", freq="d", tz="America/Chicago"
  1289. ).as_unit(unit)
  1290. index = DatetimeIndex(expected_index_values)
  1291. expected = DataFrame([1.0] + ([np.nan] * 21) + [2.0], index=index)
  1292. tm.assert_frame_equal(result, expected)
  1293. def test_resample_dst_anchor(unit):
  1294. # 5172
  1295. dti = DatetimeIndex([datetime(2012, 11, 4, 23)], tz="US/Eastern").as_unit(unit)
  1296. df = DataFrame([5], index=dti)
  1297. dti = DatetimeIndex(df.index.normalize(), freq="D").as_unit(unit)
  1298. expected = DataFrame([5], index=dti)
  1299. tm.assert_frame_equal(df.resample(rule="D").sum(), expected)
  1300. df.resample(rule="MS").sum()
  1301. tm.assert_frame_equal(
  1302. df.resample(rule="MS").sum(),
  1303. DataFrame(
  1304. [5],
  1305. index=DatetimeIndex(
  1306. [datetime(2012, 11, 1)], tz="US/Eastern", freq="MS"
  1307. ).as_unit(unit),
  1308. ),
  1309. )
  1310. def test_resample_dst_anchor2(unit):
  1311. dti = date_range(
  1312. "2013-09-30", "2013-11-02", freq="30Min", tz="Europe/Paris"
  1313. ).as_unit(unit)
  1314. values = range(dti.size)
  1315. df = DataFrame({"a": values, "b": values, "c": values}, index=dti, dtype="int64")
  1316. how = {"a": "min", "b": "max", "c": "count"}
  1317. rs = df.resample("W-MON")
  1318. result = rs.agg(how)[["a", "b", "c"]]
  1319. expected = DataFrame(
  1320. {
  1321. "a": [0, 48, 384, 720, 1056, 1394],
  1322. "b": [47, 383, 719, 1055, 1393, 1586],
  1323. "c": [48, 336, 336, 336, 338, 193],
  1324. },
  1325. index=date_range(
  1326. "9/30/2013", "11/4/2013", freq="W-MON", tz="Europe/Paris"
  1327. ).as_unit(unit),
  1328. )
  1329. tm.assert_frame_equal(
  1330. result,
  1331. expected,
  1332. "W-MON Frequency",
  1333. )
  1334. rs2 = df.resample("2W-MON")
  1335. result2 = rs2.agg(how)[["a", "b", "c"]]
  1336. expected2 = DataFrame(
  1337. {
  1338. "a": [0, 48, 720, 1394],
  1339. "b": [47, 719, 1393, 1586],
  1340. "c": [48, 672, 674, 193],
  1341. },
  1342. index=date_range(
  1343. "9/30/2013", "11/11/2013", freq="2W-MON", tz="Europe/Paris"
  1344. ).as_unit(unit),
  1345. )
  1346. tm.assert_frame_equal(
  1347. result2,
  1348. expected2,
  1349. "2W-MON Frequency",
  1350. )
  1351. rs3 = df.resample("MS")
  1352. result3 = rs3.agg(how)[["a", "b", "c"]]
  1353. expected3 = DataFrame(
  1354. {"a": [0, 48, 1538], "b": [47, 1537, 1586], "c": [48, 1490, 49]},
  1355. index=date_range("9/1/2013", "11/1/2013", freq="MS", tz="Europe/Paris").as_unit(
  1356. unit
  1357. ),
  1358. )
  1359. tm.assert_frame_equal(
  1360. result3,
  1361. expected3,
  1362. "MS Frequency",
  1363. )
  1364. rs4 = df.resample("2MS")
  1365. result4 = rs4.agg(how)[["a", "b", "c"]]
  1366. expected4 = DataFrame(
  1367. {"a": [0, 1538], "b": [1537, 1586], "c": [1538, 49]},
  1368. index=date_range(
  1369. "9/1/2013", "11/1/2013", freq="2MS", tz="Europe/Paris"
  1370. ).as_unit(unit),
  1371. )
  1372. tm.assert_frame_equal(
  1373. result4,
  1374. expected4,
  1375. "2MS Frequency",
  1376. )
  1377. df_daily = df["10/26/2013":"10/29/2013"]
  1378. rs_d = df_daily.resample("D")
  1379. result_d = rs_d.agg({"a": "min", "b": "max", "c": "count"})[["a", "b", "c"]]
  1380. expected_d = DataFrame(
  1381. {
  1382. "a": [1248, 1296, 1346, 1394],
  1383. "b": [1295, 1345, 1393, 1441],
  1384. "c": [48, 50, 48, 48],
  1385. },
  1386. index=date_range(
  1387. "10/26/2013", "10/29/2013", freq="D", tz="Europe/Paris"
  1388. ).as_unit(unit),
  1389. )
  1390. tm.assert_frame_equal(
  1391. result_d,
  1392. expected_d,
  1393. "D Frequency",
  1394. )
  1395. def test_downsample_across_dst(unit):
  1396. # GH 8531
  1397. tz = pytz.timezone("Europe/Berlin")
  1398. dt = datetime(2014, 10, 26)
  1399. dates = date_range(tz.localize(dt), periods=4, freq="2h").as_unit(unit)
  1400. result = Series(5, index=dates).resample("h").mean()
  1401. expected = Series(
  1402. [5.0, np.nan] * 3 + [5.0],
  1403. index=date_range(tz.localize(dt), periods=7, freq="h").as_unit(unit),
  1404. )
  1405. tm.assert_series_equal(result, expected)
  1406. def test_downsample_across_dst_weekly(unit):
  1407. # GH 9119, GH 21459
  1408. df = DataFrame(
  1409. index=DatetimeIndex(
  1410. ["2017-03-25", "2017-03-26", "2017-03-27", "2017-03-28", "2017-03-29"],
  1411. tz="Europe/Amsterdam",
  1412. ).as_unit(unit),
  1413. data=[11, 12, 13, 14, 15],
  1414. )
  1415. result = df.resample("1W").sum()
  1416. expected = DataFrame(
  1417. [23, 42],
  1418. index=DatetimeIndex(
  1419. ["2017-03-26", "2017-04-02"], tz="Europe/Amsterdam", freq="W"
  1420. ).as_unit(unit),
  1421. )
  1422. tm.assert_frame_equal(result, expected)
  1423. def test_downsample_across_dst_weekly_2(unit):
  1424. # GH 9119, GH 21459
  1425. idx = date_range("2013-04-01", "2013-05-01", tz="Europe/London", freq="h").as_unit(
  1426. unit
  1427. )
  1428. s = Series(index=idx, dtype=np.float64)
  1429. result = s.resample("W").mean()
  1430. expected = Series(
  1431. index=date_range("2013-04-07", freq="W", periods=5, tz="Europe/London").as_unit(
  1432. unit
  1433. ),
  1434. dtype=np.float64,
  1435. )
  1436. tm.assert_series_equal(result, expected)
  1437. def test_downsample_dst_at_midnight(unit):
  1438. # GH 25758
  1439. start = datetime(2018, 11, 3, 12)
  1440. end = datetime(2018, 11, 5, 12)
  1441. index = date_range(start, end, freq="1h").as_unit(unit)
  1442. index = index.tz_localize("UTC").tz_convert("America/Havana")
  1443. data = list(range(len(index)))
  1444. dataframe = DataFrame(data, index=index)
  1445. result = dataframe.groupby(Grouper(freq="1D")).mean()
  1446. dti = date_range("2018-11-03", periods=3).tz_localize(
  1447. "America/Havana", ambiguous=True
  1448. )
  1449. dti = DatetimeIndex(dti, freq="D").as_unit(unit)
  1450. expected = DataFrame([7.5, 28.0, 44.5], index=dti)
  1451. tm.assert_frame_equal(result, expected)
  1452. def test_resample_with_nat(unit):
  1453. # GH 13020
  1454. index = DatetimeIndex(
  1455. [
  1456. pd.NaT,
  1457. "1970-01-01 00:00:00",
  1458. pd.NaT,
  1459. "1970-01-01 00:00:01",
  1460. "1970-01-01 00:00:02",
  1461. ]
  1462. ).as_unit(unit)
  1463. frame = DataFrame([2, 3, 5, 7, 11], index=index)
  1464. index_1s = DatetimeIndex(
  1465. ["1970-01-01 00:00:00", "1970-01-01 00:00:01", "1970-01-01 00:00:02"]
  1466. ).as_unit(unit)
  1467. frame_1s = DataFrame([3.0, 7.0, 11.0], index=index_1s)
  1468. tm.assert_frame_equal(frame.resample("1s").mean(), frame_1s)
  1469. index_2s = DatetimeIndex(["1970-01-01 00:00:00", "1970-01-01 00:00:02"]).as_unit(
  1470. unit
  1471. )
  1472. frame_2s = DataFrame([5.0, 11.0], index=index_2s)
  1473. tm.assert_frame_equal(frame.resample("2s").mean(), frame_2s)
  1474. index_3s = DatetimeIndex(["1970-01-01 00:00:00"]).as_unit(unit)
  1475. frame_3s = DataFrame([7.0], index=index_3s)
  1476. tm.assert_frame_equal(frame.resample("3s").mean(), frame_3s)
  1477. tm.assert_frame_equal(frame.resample("60s").mean(), frame_3s)
  1478. def test_resample_datetime_values(unit):
  1479. # GH 13119
  1480. # check that datetime dtype is preserved when NaT values are
  1481. # introduced by the resampling
  1482. dates = [datetime(2016, 1, 15), datetime(2016, 1, 19)]
  1483. df = DataFrame({"timestamp": dates}, index=dates)
  1484. df.index = df.index.as_unit(unit)
  1485. exp = Series(
  1486. [datetime(2016, 1, 15), pd.NaT, datetime(2016, 1, 19)],
  1487. index=date_range("2016-01-15", periods=3, freq="2D").as_unit(unit),
  1488. name="timestamp",
  1489. )
  1490. res = df.resample("2D").first()["timestamp"]
  1491. tm.assert_series_equal(res, exp)
  1492. res = df["timestamp"].resample("2D").first()
  1493. tm.assert_series_equal(res, exp)
  1494. def test_resample_apply_with_additional_args(series, unit):
  1495. # GH 14615
  1496. def f(data, add_arg):
  1497. return np.mean(data) * add_arg
  1498. series.index = series.index.as_unit(unit)
  1499. multiplier = 10
  1500. result = series.resample("D").apply(f, multiplier)
  1501. expected = series.resample("D").mean().multiply(multiplier)
  1502. tm.assert_series_equal(result, expected)
  1503. # Testing as kwarg
  1504. result = series.resample("D").apply(f, add_arg=multiplier)
  1505. expected = series.resample("D").mean().multiply(multiplier)
  1506. tm.assert_series_equal(result, expected)
  1507. def test_resample_apply_with_additional_args2():
  1508. # Testing dataframe
  1509. def f(data, add_arg):
  1510. return np.mean(data) * add_arg
  1511. multiplier = 10
  1512. df = DataFrame({"A": 1, "B": 2}, index=date_range("2017", periods=10))
  1513. msg = "DataFrameGroupBy.resample operated on the grouping columns"
  1514. with tm.assert_produces_warning(FutureWarning, match=msg):
  1515. result = df.groupby("A").resample("D").agg(f, multiplier).astype(float)
  1516. msg = "DataFrameGroupBy.resample operated on the grouping columns"
  1517. with tm.assert_produces_warning(FutureWarning, match=msg):
  1518. expected = df.groupby("A").resample("D").mean().multiply(multiplier)
  1519. tm.assert_frame_equal(result, expected)
  1520. @pytest.mark.parametrize("k", [1, 2, 3])
  1521. @pytest.mark.parametrize(
  1522. "n1, freq1, n2, freq2",
  1523. [
  1524. (30, "s", 0.5, "Min"),
  1525. (60, "s", 1, "Min"),
  1526. (3600, "s", 1, "h"),
  1527. (60, "Min", 1, "h"),
  1528. (21600, "s", 0.25, "D"),
  1529. (86400, "s", 1, "D"),
  1530. (43200, "s", 0.5, "D"),
  1531. (1440, "Min", 1, "D"),
  1532. (12, "h", 0.5, "D"),
  1533. (24, "h", 1, "D"),
  1534. ],
  1535. )
  1536. def test_resample_equivalent_offsets(n1, freq1, n2, freq2, k, unit):
  1537. # GH 24127
  1538. n1_ = n1 * k
  1539. n2_ = n2 * k
  1540. dti = date_range("1991-09-05", "1991-09-12", freq=freq1).as_unit(unit)
  1541. ser = Series(range(len(dti)), index=dti)
  1542. result1 = ser.resample(str(n1_) + freq1).mean()
  1543. result2 = ser.resample(str(n2_) + freq2).mean()
  1544. tm.assert_series_equal(result1, result2)
  1545. @pytest.mark.parametrize(
  1546. "first,last,freq,exp_first,exp_last",
  1547. [
  1548. ("19910905", "19920406", "D", "19910905", "19920407"),
  1549. ("19910905 00:00", "19920406 06:00", "D", "19910905", "19920407"),
  1550. ("19910905 06:00", "19920406 06:00", "h", "19910905 06:00", "19920406 07:00"),
  1551. ("19910906", "19920406", "ME", "19910831", "19920430"),
  1552. ("19910831", "19920430", "ME", "19910831", "19920531"),
  1553. ("1991-08", "1992-04", "ME", "19910831", "19920531"),
  1554. ],
  1555. )
  1556. def test_get_timestamp_range_edges(first, last, freq, exp_first, exp_last, unit):
  1557. first = Period(first)
  1558. first = first.to_timestamp(first.freq).as_unit(unit)
  1559. last = Period(last)
  1560. last = last.to_timestamp(last.freq).as_unit(unit)
  1561. exp_first = Timestamp(exp_first)
  1562. exp_last = Timestamp(exp_last)
  1563. freq = pd.tseries.frequencies.to_offset(freq)
  1564. result = _get_timestamp_range_edges(first, last, freq, unit="ns")
  1565. expected = (exp_first, exp_last)
  1566. assert result == expected
  1567. @pytest.mark.parametrize("duplicates", [True, False])
  1568. def test_resample_apply_product(duplicates, unit):
  1569. # GH 5586
  1570. index = date_range(start="2012-01-31", freq="ME", periods=12).as_unit(unit)
  1571. ts = Series(range(12), index=index)
  1572. df = DataFrame({"A": ts, "B": ts + 2})
  1573. if duplicates:
  1574. df.columns = ["A", "A"]
  1575. msg = "using DatetimeIndexResampler.prod"
  1576. with tm.assert_produces_warning(FutureWarning, match=msg):
  1577. result = df.resample("QE").apply(np.prod)
  1578. expected = DataFrame(
  1579. np.array([[0, 24], [60, 210], [336, 720], [990, 1716]], dtype=np.int64),
  1580. index=DatetimeIndex(
  1581. ["2012-03-31", "2012-06-30", "2012-09-30", "2012-12-31"], freq="QE-DEC"
  1582. ).as_unit(unit),
  1583. columns=df.columns,
  1584. )
  1585. tm.assert_frame_equal(result, expected)
  1586. @pytest.mark.parametrize(
  1587. "first,last,freq_in,freq_out,exp_last",
  1588. [
  1589. (
  1590. "2020-03-28",
  1591. "2020-03-31",
  1592. "D",
  1593. "24h",
  1594. "2020-03-30 01:00",
  1595. ), # includes transition into DST
  1596. (
  1597. "2020-03-28",
  1598. "2020-10-27",
  1599. "D",
  1600. "24h",
  1601. "2020-10-27 00:00",
  1602. ), # includes transition into and out of DST
  1603. (
  1604. "2020-10-25",
  1605. "2020-10-27",
  1606. "D",
  1607. "24h",
  1608. "2020-10-26 23:00",
  1609. ), # includes transition out of DST
  1610. (
  1611. "2020-03-28",
  1612. "2020-03-31",
  1613. "24h",
  1614. "D",
  1615. "2020-03-30 00:00",
  1616. ), # same as above, but from 24H to D
  1617. ("2020-03-28", "2020-10-27", "24h", "D", "2020-10-27 00:00"),
  1618. ("2020-10-25", "2020-10-27", "24h", "D", "2020-10-26 00:00"),
  1619. ],
  1620. )
  1621. def test_resample_calendar_day_with_dst(
  1622. first: str, last: str, freq_in: str, freq_out: str, exp_last: str, unit
  1623. ):
  1624. # GH 35219
  1625. ts = Series(
  1626. 1.0, date_range(first, last, freq=freq_in, tz="Europe/Amsterdam").as_unit(unit)
  1627. )
  1628. result = ts.resample(freq_out).ffill()
  1629. expected = Series(
  1630. 1.0,
  1631. date_range(first, exp_last, freq=freq_out, tz="Europe/Amsterdam").as_unit(unit),
  1632. )
  1633. tm.assert_series_equal(result, expected)
  1634. @pytest.mark.parametrize("func", ["min", "max", "first", "last"])
  1635. def test_resample_aggregate_functions_min_count(func, unit):
  1636. # GH#37768
  1637. index = date_range(start="2020", freq="ME", periods=3).as_unit(unit)
  1638. ser = Series([1, np.nan, np.nan], index)
  1639. result = getattr(ser.resample("QE"), func)(min_count=2)
  1640. expected = Series(
  1641. [np.nan],
  1642. index=DatetimeIndex(["2020-03-31"], freq="QE-DEC").as_unit(unit),
  1643. )
  1644. tm.assert_series_equal(result, expected)
  1645. def test_resample_unsigned_int(any_unsigned_int_numpy_dtype, unit):
  1646. # gh-43329
  1647. df = DataFrame(
  1648. index=date_range(start="2000-01-01", end="2000-01-03 23", freq="12h").as_unit(
  1649. unit
  1650. ),
  1651. columns=["x"],
  1652. data=[0, 1, 0] * 2,
  1653. dtype=any_unsigned_int_numpy_dtype,
  1654. )
  1655. df = df.loc[(df.index < "2000-01-02") | (df.index > "2000-01-03"), :]
  1656. result = df.resample("D").max()
  1657. expected = DataFrame(
  1658. [1, np.nan, 0],
  1659. columns=["x"],
  1660. index=date_range(start="2000-01-01", end="2000-01-03 23", freq="D").as_unit(
  1661. unit
  1662. ),
  1663. )
  1664. tm.assert_frame_equal(result, expected)
  1665. def test_long_rule_non_nano():
  1666. # https://github.com/pandas-dev/pandas/issues/51024
  1667. idx = date_range("0300-01-01", "2000-01-01", unit="s", freq="100YE")
  1668. ser = Series([1, 4, 2, 8, 5, 7, 1, 4, 2, 8, 5, 7, 1, 4, 2, 8, 5], index=idx)
  1669. result = ser.resample("200YE").mean()
  1670. expected_idx = DatetimeIndex(
  1671. np.array(
  1672. [
  1673. "0300-12-31",
  1674. "0500-12-31",
  1675. "0700-12-31",
  1676. "0900-12-31",
  1677. "1100-12-31",
  1678. "1300-12-31",
  1679. "1500-12-31",
  1680. "1700-12-31",
  1681. "1900-12-31",
  1682. ]
  1683. ).astype("datetime64[s]"),
  1684. freq="200YE-DEC",
  1685. )
  1686. expected = Series([1.0, 3.0, 6.5, 4.0, 3.0, 6.5, 4.0, 3.0, 6.5], index=expected_idx)
  1687. tm.assert_series_equal(result, expected)
  1688. def test_resample_empty_series_with_tz():
  1689. # GH#53664
  1690. df = DataFrame({"ts": [], "values": []}).astype(
  1691. {"ts": "datetime64[ns, Atlantic/Faroe]"}
  1692. )
  1693. result = df.resample("2MS", on="ts", closed="left", label="left", origin="start")[
  1694. "values"
  1695. ].sum()
  1696. expected_idx = DatetimeIndex(
  1697. [], freq="2MS", name="ts", dtype="datetime64[ns, Atlantic/Faroe]"
  1698. )
  1699. expected = Series([], index=expected_idx, name="values", dtype="float64")
  1700. tm.assert_series_equal(result, expected)
  1701. @pytest.mark.parametrize(
  1702. "freq, freq_depr",
  1703. [
  1704. ("2ME", "2M"),
  1705. ("2QE", "2Q"),
  1706. ("2QE-SEP", "2Q-SEP"),
  1707. ("1YE", "1Y"),
  1708. ("2YE-MAR", "2Y-MAR"),
  1709. ("1YE", "1A"),
  1710. ("2YE-MAR", "2A-MAR"),
  1711. ],
  1712. )
  1713. def test_resample_M_Q_Y_A_deprecated(freq, freq_depr):
  1714. # GH#9586
  1715. depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
  1716. f"in a future version, please use '{freq[1:]}' instead."
  1717. s = Series(range(10), index=date_range("20130101", freq="d", periods=10))
  1718. expected = s.resample(freq).mean()
  1719. with tm.assert_produces_warning(FutureWarning, match=depr_msg):
  1720. result = s.resample(freq_depr).mean()
  1721. tm.assert_series_equal(result, expected)
  1722. @pytest.mark.parametrize(
  1723. "freq, freq_depr",
  1724. [
  1725. ("2BME", "2BM"),
  1726. ("2BQE", "2BQ"),
  1727. ("2BQE-MAR", "2BQ-MAR"),
  1728. ],
  1729. )
  1730. def test_resample_BM_BQ_deprecated(freq, freq_depr):
  1731. # GH#52064
  1732. depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed "
  1733. f"in a future version, please use '{freq[1:]}' instead."
  1734. s = Series(range(10), index=date_range("20130101", freq="d", periods=10))
  1735. expected = s.resample(freq).mean()
  1736. with tm.assert_produces_warning(FutureWarning, match=depr_msg):
  1737. result = s.resample(freq_depr).mean()
  1738. tm.assert_series_equal(result, expected)
  1739. def test_resample_ms_closed_right(unit):
  1740. # https://github.com/pandas-dev/pandas/issues/55271
  1741. dti = date_range(start="2020-01-31", freq="1min", periods=6000, unit=unit)
  1742. df = DataFrame({"ts": dti}, index=dti)
  1743. grouped = df.resample("MS", closed="right")
  1744. result = grouped.last()
  1745. exp_dti = DatetimeIndex(
  1746. [datetime(2020, 1, 1), datetime(2020, 2, 1)], freq="MS"
  1747. ).as_unit(unit)
  1748. expected = DataFrame(
  1749. {"ts": [datetime(2020, 2, 1), datetime(2020, 2, 4, 3, 59)]},
  1750. index=exp_dti,
  1751. ).astype(f"M8[{unit}]")
  1752. tm.assert_frame_equal(result, expected)
  1753. @pytest.mark.parametrize("freq", ["B", "C"])
  1754. def test_resample_c_b_closed_right(freq: str, unit):
  1755. # https://github.com/pandas-dev/pandas/issues/55281
  1756. dti = date_range(start="2020-01-31", freq="1min", periods=6000, unit=unit)
  1757. df = DataFrame({"ts": dti}, index=dti)
  1758. grouped = df.resample(freq, closed="right")
  1759. result = grouped.last()
  1760. exp_dti = DatetimeIndex(
  1761. [
  1762. datetime(2020, 1, 30),
  1763. datetime(2020, 1, 31),
  1764. datetime(2020, 2, 3),
  1765. datetime(2020, 2, 4),
  1766. ],
  1767. freq=freq,
  1768. ).as_unit(unit)
  1769. expected = DataFrame(
  1770. {
  1771. "ts": [
  1772. datetime(2020, 1, 31),
  1773. datetime(2020, 2, 3),
  1774. datetime(2020, 2, 4),
  1775. datetime(2020, 2, 4, 3, 59),
  1776. ]
  1777. },
  1778. index=exp_dti,
  1779. ).astype(f"M8[{unit}]")
  1780. tm.assert_frame_equal(result, expected)
  1781. def test_resample_b_55282(unit):
  1782. # https://github.com/pandas-dev/pandas/issues/55282
  1783. dti = date_range("2023-09-26", periods=6, freq="12h", unit=unit)
  1784. ser = Series([1, 2, 3, 4, 5, 6], index=dti)
  1785. result = ser.resample("B", closed="right", label="right").mean()
  1786. exp_dti = DatetimeIndex(
  1787. [
  1788. datetime(2023, 9, 26),
  1789. datetime(2023, 9, 27),
  1790. datetime(2023, 9, 28),
  1791. datetime(2023, 9, 29),
  1792. ],
  1793. freq="B",
  1794. ).as_unit(unit)
  1795. expected = Series(
  1796. [1.0, 2.5, 4.5, 6.0],
  1797. index=exp_dti,
  1798. )
  1799. tm.assert_series_equal(result, expected)
  1800. @td.skip_if_no("pyarrow")
  1801. @pytest.mark.parametrize(
  1802. "tz",
  1803. [
  1804. None,
  1805. pytest.param(
  1806. "UTC",
  1807. marks=pytest.mark.xfail(
  1808. condition=is_platform_windows(),
  1809. reason="TODO: Set ARROW_TIMEZONE_DATABASE env var in CI",
  1810. ),
  1811. ),
  1812. ],
  1813. )
  1814. def test_arrow_timestamp_resample(tz):
  1815. # GH 56371
  1816. idx = Series(date_range("2020-01-01", periods=5), dtype="timestamp[ns][pyarrow]")
  1817. if tz is not None:
  1818. idx = idx.dt.tz_localize(tz)
  1819. expected = Series(np.arange(5, dtype=np.float64), index=idx)
  1820. result = expected.resample("1D").mean()
  1821. tm.assert_series_equal(result, expected)