Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: PeriodArray.to_timestamp when non-contiguous #44935

Merged
merged 3 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@ Period
- Bug in adding a :class:`Period` object to a ``np.timedelta64`` object incorrectly raising ``TypeError`` (:issue:`44182`)
- Bug in :meth:`PeriodIndex.to_timestamp` when the index has ``freq="B"`` inferring ``freq="D"`` for its result instead of ``freq="B"`` (:issue:`44105`)
- Bug in :class:`Period` constructor incorrectly allowing ``np.timedelta64("NaT")`` (:issue:`44507`)
- Bug in :meth:`PeriodIndex.to_timestamp` giving incorrect values for indexes with non-contiguous data (:issue:`44100`)
-

Plotting
Expand Down
5 changes: 4 additions & 1 deletion pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1088,6 +1088,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int freq1, int freq2, bint end):
"""
cdef:
Py_ssize_t n = len(arr)
Py_ssize_t increment = arr.strides[0] // 8
ndarray[int64_t] result = np.empty(n, dtype=np.int64)

_period_asfreq(
Expand All @@ -1097,6 +1098,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int freq1, int freq2, bint end):
freq1,
freq2,
end,
increment,
)
return result

Expand All @@ -1110,6 +1112,7 @@ cdef void _period_asfreq(
int freq1,
int freq2,
bint end,
Py_ssize_t increment=1,
):
"""See period_asfreq.__doc__"""
cdef:
Expand All @@ -1127,7 +1130,7 @@ cdef void _period_asfreq(
get_asfreq_info(freq1, freq2, end, &af_info)

for i in range(length):
val = ordinals[i]
val = ordinals[i * increment]
if val != NPY_NAT:
val = func(val, &af_info)
out[i] = val
Expand Down
30 changes: 30 additions & 0 deletions pandas/tests/indexes/period/methods/test_to_timestamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,36 @@


class TestToTimestamp:
def test_to_timestamp_non_contiguous(self):
# GH#44100
dti = date_range("2021-10-18", periods=9, freq="B")
pi = dti.to_period()

result = pi[::2].to_timestamp()
expected = dti[::2]
tm.assert_index_equal(result, expected)

result = pi._data[::2].to_timestamp()
expected = dti._data[::2]
# TODO: can we get the freq to round-trip?
tm.assert_datetime_array_equal(result, expected, check_freq=False)

result = pi[::-1].to_timestamp()
expected = dti[::-1]
tm.assert_index_equal(result, expected)

result = pi._data[::-1].to_timestamp()
expected = dti._data[::-1]
tm.assert_datetime_array_equal(result, expected, check_freq=False)

result = pi[::2][::-1].to_timestamp()
expected = dti[::2][::-1]
tm.assert_index_equal(result, expected)

result = pi._data[::2][::-1].to_timestamp()
expected = dti._data[::2][::-1]
tm.assert_datetime_array_equal(result, expected, check_freq=False)

def test_to_timestamp_freq(self):
idx = period_range("2017", periods=12, freq="A-DEC")
result = idx.to_timestamp()
Expand Down