From e7ff4ca1da8333ae933f001a8fc81a09b74cea1d Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Mon, 12 Feb 2024 22:05:13 +0100 Subject: [PATCH 1/3] add an example to PeriodIndex.to_timestamp --- pandas/core/arrays/period.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index ab79622ddd8be..655bc3bc60924 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -636,6 +636,12 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray: >>> idx.to_timestamp() DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'], dtype='datetime64[ns]', freq='MS') + + We can not infer the frequency if len(index) < 3: + + >>> idx = pd.PeriodIndex(["2023-01", "2023-02"], freq="M") + >>> idx.to_timestamp() + DatetimeIndex(['2023-01-01', '2023-02-01'], dtype='datetime64[ns]', freq=None) """ from pandas.core.arrays import DatetimeArray From a2dc46a2c5fac07f09b3b113fc643633371fa396 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Wed, 14 Feb 2024 20:41:47 +0100 Subject: [PATCH 2/3] correct to_timestamp docs, add an example --- pandas/core/arrays/period.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index 655bc3bc60924..e5b3434f11bc2 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -637,11 +637,19 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray: DatetimeIndex(['2023-01-01', '2023-02-01', '2023-03-01'], dtype='datetime64[ns]', freq='MS') - We can not infer the frequency if len(index) < 3: + The frequency will not be inferred if the index contains less than + three elements and values of index are not strictly monotonic: >>> idx = pd.PeriodIndex(["2023-01", "2023-02"], freq="M") >>> idx.to_timestamp() DatetimeIndex(['2023-01-01', '2023-02-01'], dtype='datetime64[ns]', freq=None) + + >>> idx = pd.PeriodIndex( + ... ["2023-01", "2023-02", "2023-02", "2023-03"], freq="2M" + ... ) + >>> idx.to_timestamp() + DatetimeIndex(['2023-01-01', '2023-02-01', '2023-02-01', '2023-03-01'], + dtype='datetime64[ns]', freq=None) """ from pandas.core.arrays import DatetimeArray From 760d5552985bcac517a5edbb1e4f8ba6414770a3 Mon Sep 17 00:00:00 2001 From: Natalia Mokeeva Date: Thu, 15 Feb 2024 12:23:22 +0100 Subject: [PATCH 3/3] correct the wording in to_timestamp docstring --- pandas/core/arrays/period.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/arrays/period.py b/pandas/core/arrays/period.py index e5b3434f11bc2..d3760abb8eaf3 100644 --- a/pandas/core/arrays/period.py +++ b/pandas/core/arrays/period.py @@ -638,7 +638,7 @@ def to_timestamp(self, freq=None, how: str = "start") -> DatetimeArray: dtype='datetime64[ns]', freq='MS') The frequency will not be inferred if the index contains less than - three elements and values of index are not strictly monotonic: + three elements, or if the values of index are not strictly monotonic: >>> idx = pd.PeriodIndex(["2023-01", "2023-02"], freq="M") >>> idx.to_timestamp()