From d67b21dac38fed3a824b9f1d31df584a7d45a071 Mon Sep 17 00:00:00 2001 From: Brock Date: Thu, 16 Dec 2021 16:00:01 -0800 Subject: [PATCH 1/2] BUG: PeriodArray.to_timestamp when non-contiguous --- pandas/_libs/tslibs/period.pyx | 5 +++- .../period/methods/test_to_timestamp.py | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/pandas/_libs/tslibs/period.pyx b/pandas/_libs/tslibs/period.pyx index 67696f9740ea1..1df1c9a947e8d 100644 --- a/pandas/_libs/tslibs/period.pyx +++ b/pandas/_libs/tslibs/period.pyx @@ -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( @@ -1097,6 +1098,7 @@ def period_asfreq_arr(ndarray[int64_t] arr, int freq1, int freq2, bint end): freq1, freq2, end, + increment, ) return result @@ -1110,6 +1112,7 @@ cdef void _period_asfreq( int freq1, int freq2, bint end, + Py_ssize_t increment=1, ): """See period_asfreq.__doc__""" cdef: @@ -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 diff --git a/pandas/tests/indexes/period/methods/test_to_timestamp.py b/pandas/tests/indexes/period/methods/test_to_timestamp.py index c2328872aee1b..164ed3ec43996 100644 --- a/pandas/tests/indexes/period/methods/test_to_timestamp.py +++ b/pandas/tests/indexes/period/methods/test_to_timestamp.py @@ -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() From 1b3df8ec4353fc2fa8e830fd6d486009ea98a6b2 Mon Sep 17 00:00:00 2001 From: Brock Date: Fri, 17 Dec 2021 11:53:21 -0800 Subject: [PATCH 2/2] whatsnew --- doc/source/whatsnew/v1.4.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index caf3a4281561f..044361de218df 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -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