Skip to content

Commit 371a8f2

Browse files
authored
BUG: PeriodArray.to_timestamp when non-contiguous (#44935)
1 parent 63e7ef1 commit 371a8f2

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ Period
769769
- Bug in adding a :class:`Period` object to a ``np.timedelta64`` object incorrectly raising ``TypeError`` (:issue:`44182`)
770770
- Bug in :meth:`PeriodIndex.to_timestamp` when the index has ``freq="B"`` inferring ``freq="D"`` for its result instead of ``freq="B"`` (:issue:`44105`)
771771
- Bug in :class:`Period` constructor incorrectly allowing ``np.timedelta64("NaT")`` (:issue:`44507`)
772+
- Bug in :meth:`PeriodIndex.to_timestamp` giving incorrect values for indexes with non-contiguous data (:issue:`44100`)
772773
-
773774

774775
Plotting

pandas/_libs/tslibs/period.pyx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int freq1, int freq2, bint end):
10881088
"""
10891089
cdef:
10901090
Py_ssize_t n = len(arr)
1091+
Py_ssize_t increment = arr.strides[0] // 8
10911092
ndarray[int64_t] result = np.empty(n, dtype=np.int64)
10921093

10931094
_period_asfreq(
@@ -1097,6 +1098,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int freq1, int freq2, bint end):
10971098
freq1,
10981099
freq2,
10991100
end,
1101+
increment,
11001102
)
11011103
return result
11021104

@@ -1110,6 +1112,7 @@ cdef void _period_asfreq(
11101112
int freq1,
11111113
int freq2,
11121114
bint end,
1115+
Py_ssize_t increment=1,
11131116
):
11141117
"""See period_asfreq.__doc__"""
11151118
cdef:
@@ -1127,7 +1130,7 @@ cdef void _period_asfreq(
11271130
get_asfreq_info(freq1, freq2, end, &af_info)
11281131

11291132
for i in range(length):
1130-
val = ordinals[i]
1133+
val = ordinals[i * increment]
11311134
if val != NPY_NAT:
11321135
val = func(val, &af_info)
11331136
out[i] = val

pandas/tests/indexes/period/methods/test_to_timestamp.py

+30
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,36 @@
1616

1717

1818
class TestToTimestamp:
19+
def test_to_timestamp_non_contiguous(self):
20+
# GH#44100
21+
dti = date_range("2021-10-18", periods=9, freq="B")
22+
pi = dti.to_period()
23+
24+
result = pi[::2].to_timestamp()
25+
expected = dti[::2]
26+
tm.assert_index_equal(result, expected)
27+
28+
result = pi._data[::2].to_timestamp()
29+
expected = dti._data[::2]
30+
# TODO: can we get the freq to round-trip?
31+
tm.assert_datetime_array_equal(result, expected, check_freq=False)
32+
33+
result = pi[::-1].to_timestamp()
34+
expected = dti[::-1]
35+
tm.assert_index_equal(result, expected)
36+
37+
result = pi._data[::-1].to_timestamp()
38+
expected = dti._data[::-1]
39+
tm.assert_datetime_array_equal(result, expected, check_freq=False)
40+
41+
result = pi[::2][::-1].to_timestamp()
42+
expected = dti[::2][::-1]
43+
tm.assert_index_equal(result, expected)
44+
45+
result = pi._data[::2][::-1].to_timestamp()
46+
expected = dti._data[::2][::-1]
47+
tm.assert_datetime_array_equal(result, expected, check_freq=False)
48+
1949
def test_to_timestamp_freq(self):
2050
idx = period_range("2017", periods=12, freq="A-DEC")
2151
result = idx.to_timestamp()

0 commit comments

Comments
 (0)