Skip to content

Commit 883b5f5

Browse files
vetorize, check_stacklevel
1 parent f5b6d14 commit 883b5f5

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

pandas/core/arrays/period.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -824,9 +824,10 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray:
824824

825825
is_start = how == "S"
826826
if is_start:
827-
new_data = np.asarray(
828-
[(NaT if period is NaT else period.start_time) for period in new_parr]
827+
start_time = np.vectorize(
828+
lambda period: (NaT if period is NaT else period.start_time)
829829
)
830+
new_data = start_time(new_parr)
830831
else:
831832
new_data = libperiod.periodarr_to_dt64arr(new_parr.asi8, base)
832833

pandas/tests/resample/test_period_index.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ def test_annual_upsample_cases(
150150
if period == "B"
151151
else "Resampling with a PeriodIndex is deprecated"
152152
)
153-
with tm.assert_produces_warning(FutureWarning, match=msg):
153+
with tm.assert_produces_warning(
154+
FutureWarning, match=msg, check_stacklevel=False
155+
):
154156
result = getattr(ts.resample(period, convention=conv), meth)()
155157
expected = result.to_timestamp(period, how=conv)
156158
expected = expected.asfreq(offset, meth).to_period()
@@ -193,7 +195,9 @@ def test_basic_upsample(self, freq, simple_period_range_series):
193195
result = ts.resample("Y-DEC").mean()
194196

195197
msg = "The 'convention' keyword in Series.resample is deprecated"
196-
with tm.assert_produces_warning(FutureWarning, match=msg):
198+
with tm.assert_produces_warning(
199+
FutureWarning, match=msg, check_stacklevel=False
200+
):
197201
resampled = result.resample(freq, convention="end").ffill()
198202
expected = result.to_timestamp(freq, how="end")
199203
expected = expected.asfreq(freq, "ffill").to_period(freq)
@@ -204,7 +208,9 @@ def test_upsample_with_limit(self):
204208
ts = Series(np.random.default_rng(2).standard_normal(len(rng)), rng)
205209

206210
msg = "The 'convention' keyword in Series.resample is deprecated"
207-
with tm.assert_produces_warning(FutureWarning, match=msg):
211+
with tm.assert_produces_warning(
212+
FutureWarning, match=msg, check_stacklevel=False
213+
):
208214
result = ts.resample("M", convention="end").ffill(limit=2)
209215
expected = ts.asfreq("M").reindex(result.index, method="ffill", limit=2)
210216
tm.assert_series_equal(result, expected)
@@ -249,7 +255,9 @@ def test_quarterly_upsample(
249255
if period == "B"
250256
else "Resampling with a PeriodIndex is deprecated"
251257
)
252-
with tm.assert_produces_warning(FutureWarning, match=msg):
258+
with tm.assert_produces_warning(
259+
FutureWarning, match=msg, check_stacklevel=False
260+
):
253261
result = ts.resample(period, convention=convention).ffill()
254262
expected = result.to_timestamp(period, how=convention)
255263
expected = expected.asfreq(offset, "ffill").to_period()
@@ -265,7 +273,9 @@ def test_monthly_upsample(self, target, convention, simple_period_range_series):
265273
if target == "D"
266274
else r"PeriodDtype\[B\] is deprecated"
267275
)
268-
with tm.assert_produces_warning(FutureWarning, match=msg):
276+
with tm.assert_produces_warning(
277+
FutureWarning, match=msg, check_stacklevel=False
278+
):
269279
result = ts.resample(target, convention=convention).ffill()
270280
expected = result.to_timestamp(target, how=convention)
271281
expected = expected.asfreq(target, "ffill").to_period()
@@ -346,7 +356,7 @@ def test_with_local_timezone(self, tz):
346356
series = Series(1, index=index)
347357
series = series.tz_convert(local_timezone)
348358
msg = "Converting to PeriodArray/Index representation will drop timezone"
349-
with tm.assert_produces_warning(UserWarning, match=msg):
359+
with tm.assert_produces_warning(UserWarning, match=msg, check_stacklevel=False):
350360
result = series.resample("D").mean().to_period()
351361

352362
# Create the expected series
@@ -438,7 +448,7 @@ def test_weekly_upsample(self, day, target, convention, simple_period_range_seri
438448
if warn is None:
439449
msg = "Resampling with a PeriodIndex is deprecated"
440450
warn = FutureWarning
441-
with tm.assert_produces_warning(warn, match=msg):
451+
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
442452
result = ts.resample(target, convention=convention).ffill()
443453
expected = result.to_timestamp(target, how=convention)
444454
expected = expected.asfreq(target, "ffill").to_period()
@@ -473,7 +483,9 @@ def test_resample_to_quarterly_start_end(self, simple_period_range_series, how):
473483
# conforms, but different month
474484
ts = simple_period_range_series("1990", "1992", freq="Y-JUN")
475485
msg = "The 'convention' keyword in Series.resample is deprecated"
476-
with tm.assert_produces_warning(FutureWarning, match=msg):
486+
with tm.assert_produces_warning(
487+
FutureWarning, match=msg, check_stacklevel=False
488+
):
477489
result = ts.resample("Q-MAR", convention=how).ffill()
478490
expected = ts.asfreq("Q-MAR", how=how)
479491
expected = expected.reindex(result.index, method="ffill")
@@ -523,7 +535,9 @@ def test_upsample_daily_business_daily(self, simple_period_range_series):
523535

524536
ts = simple_period_range_series("1/1/2000", "2/1/2000")
525537
msg = "The 'convention' keyword in Series.resample is deprecated"
526-
with tm.assert_produces_warning(FutureWarning, match=msg):
538+
with tm.assert_produces_warning(
539+
FutureWarning, match=msg, check_stacklevel=False
540+
):
527541
result = ts.resample("h", convention="s").asfreq()
528542
exp_rng = period_range("1/1/2000", "2/1/2000 23:00", freq="h")
529543
expected = ts.asfreq("h", how="s").reindex(exp_rng)
@@ -586,7 +600,7 @@ def test_resample_tz_localized2(self):
586600

587601
# for good measure
588602
msg = "Converting to PeriodArray/Index representation will drop timezone "
589-
with tm.assert_produces_warning(UserWarning, match=msg):
603+
with tm.assert_produces_warning(UserWarning, match=msg, check_stacklevel=False):
590604
result = s.resample("D").mean().to_period()
591605
ex_index = period_range("2001-09-20", periods=1, freq="D")
592606
expected = Series([1.5], index=ex_index)
@@ -885,7 +899,9 @@ def test_resample_with_nat(self, periods, values, freq, expected_values):
885899
)
886900
expected = DataFrame(expected_values, index=expected_index)
887901
msg = "Resampling with a PeriodIndex is deprecated"
888-
with tm.assert_produces_warning(FutureWarning, match=msg):
902+
with tm.assert_produces_warning(
903+
FutureWarning, match=msg, check_stacklevel=False
904+
):
889905
rs = frame.resample(freq)
890906
result = rs.mean()
891907
tm.assert_frame_equal(result, expected)
@@ -924,7 +940,9 @@ def test_resample_with_offset(self, start, end, start_freq, end_freq, offset):
924940
pi = period_range(start, end, freq=start_freq)
925941
ser = Series(np.arange(len(pi)), index=pi)
926942
msg = "Resampling with a PeriodIndex is deprecated"
927-
with tm.assert_produces_warning(FutureWarning, match=msg):
943+
with tm.assert_produces_warning(
944+
FutureWarning, match=msg, check_stacklevel=False
945+
):
928946
rs = ser.resample(end_freq, offset=offset)
929947
result = rs.mean()
930948
result = result.to_timestamp(end_freq)
@@ -937,7 +955,9 @@ def test_resample_with_offset_month(self):
937955
pi = period_range("19910905 12:00", "19910909 1:00", freq="h")
938956
ser = Series(np.arange(len(pi)), index=pi)
939957
msg = "Resampling with a PeriodIndex is deprecated"
940-
with tm.assert_produces_warning(FutureWarning, match=msg):
958+
with tm.assert_produces_warning(
959+
FutureWarning, match=msg, check_stacklevel=False
960+
):
941961
rs = ser.resample("M", offset="3h")
942962
result = rs.mean()
943963
result = result.to_timestamp("M")
@@ -985,7 +1005,9 @@ def test_sum_min_count(self):
9851005
data[3:6] = np.nan
9861006
s = Series(data, index).to_period()
9871007
msg = "Resampling with a PeriodIndex is deprecated"
988-
with tm.assert_produces_warning(FutureWarning, match=msg):
1008+
with tm.assert_produces_warning(
1009+
FutureWarning, match=msg, check_stacklevel=False
1010+
):
9891011
rs = s.resample("Q")
9901012
result = rs.sum(min_count=1)
9911013
expected = Series(
@@ -1073,7 +1095,7 @@ def test_corner_cases_period(simple_period_range_series):
10731095
len0pts = simple_period_range_series("2007-01", "2010-05", freq="M")[:0]
10741096
# it works
10751097
msg = "Resampling with a PeriodIndex is deprecated"
1076-
with tm.assert_produces_warning(FutureWarning, match=msg):
1098+
with tm.assert_produces_warning(FutureWarning, match=msg, check_stacklevel=False):
10771099
result = len0pts.resample("Y-DEC").mean()
10781100
assert len(result) == 0
10791101

0 commit comments

Comments
 (0)