From fb88d911817a0624da1bacf6af8c3a67370fbb72 Mon Sep 17 00:00:00 2001 From: Denniskamau Date: Sat, 10 Mar 2018 16:37:12 +0300 Subject: [PATCH 1/7] Improvement to pandas.DatetimeIndex.period --- pandas/core/indexes/datetimes.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index e5e9bba269fd4..e20e26e7e2cf7 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1057,7 +1057,30 @@ def to_pydatetime(self): def to_period(self, freq=None): """ - Cast to PeriodIndex at a particular frequency + Cast to PeriodIndex at a particular frequency. + + Converts timestamps index to period. + + Parameters + ---------- + freq : string or pandas offset object,optional + One of pandas date offset string or corresponding objects. + + Returns + ------- + period: period[D] + + Examples + -------- + >>> df = pd.DataFrame({"y": [1,2,3]}, + ... index=pd.to_datetime(["2000-03-31 00:00:00", + ... "2000-05-31 00:00:00", + ... "2000-08-31 00:00:00"])) + >>> df.index = df.index.to_period("M") + + See also + -------- + Index: base pandas index type """ from pandas.core.indexes.period import PeriodIndex From 3ed30ec26acd68baf9e1833577ccb6bca2615039 Mon Sep 17 00:00:00 2001 From: Denniskamau Date: Sat, 10 Mar 2018 16:52:54 +0300 Subject: [PATCH 2/7] Improvement to pandas.DatetimeIndex.period --- pandas/core/indexes/datetimes.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index e20e26e7e2cf7..8cf804ec3e141 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1059,11 +1059,11 @@ def to_period(self, freq=None): """ Cast to PeriodIndex at a particular frequency. - Converts timestamps index to period. + Converts DatetimeIndex to periodIndex. Parameters ---------- - freq : string or pandas offset object,optional + freq : string or pandas offset object, optional One of pandas date offset string or corresponding objects. Returns From c64779e04dd2295c596226746b2b165525b098c3 Mon Sep 17 00:00:00 2001 From: Denniskamau Date: Sat, 10 Mar 2018 20:05:56 +0300 Subject: [PATCH 3/7] Improvement to pandas.DatetimeIndex.period --- pandas/core/indexes/datetimes.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 8cf804ec3e141..010a5bef4bedb 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1068,19 +1068,21 @@ def to_period(self, freq=None): Returns ------- - period: period[D] + period : DatetimeIndex Examples -------- - >>> df = pd.DataFrame({"y": [1,2,3]}, - ... index=pd.to_datetime(["2000-03-31 00:00:00", - ... "2000-05-31 00:00:00", - ... "2000-08-31 00:00:00"])) - >>> df.index = df.index.to_period("M") + >>> index=pd.to_datetime(["2000-03-31 00:00:00", + ... "2000-08-31 00:00:00"]) + >>> index.to_period("M") + PeriodIndex(['2000-03', '2000-08'], + ... dtype='period[M]', + ... freq='M') See also -------- - Index: base pandas index type + pandas.PeriodIndex: Immutable ndarray holding ordinal values + pandas.DatetimeIndex.to_pydatetime: Return DatetimeIndex as object """ from pandas.core.indexes.period import PeriodIndex From 92c8a4154cb352d95b53a91d89ead2de8ae38819 Mon Sep 17 00:00:00 2001 From: Denniskamau Date: Sat, 10 Mar 2018 20:10:02 +0300 Subject: [PATCH 4/7] Improvement to pandas.DatetimeIndex.period --- pandas/core/indexes/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 010a5bef4bedb..9e05706493788 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1059,7 +1059,7 @@ def to_period(self, freq=None): """ Cast to PeriodIndex at a particular frequency. - Converts DatetimeIndex to periodIndex. + Converts DatetimeIndex to PeriodIndex. Parameters ---------- From 8fc1e9a38ee7f34818b84a6d80eea23d4856921e Mon Sep 17 00:00:00 2001 From: Denniskamau Date: Sat, 10 Mar 2018 20:40:39 +0300 Subject: [PATCH 5/7] Improvement to pandas.DatetimeIndex.period --- pandas/core/indexes/datetimes.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 9e05706493788..60de83ef24bd6 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1072,13 +1072,17 @@ def to_period(self, freq=None): Examples -------- - >>> index=pd.to_datetime(["2000-03-31 00:00:00", - ... "2000-08-31 00:00:00"]) - >>> index.to_period("M") - PeriodIndex(['2000-03', '2000-08'], - ... dtype='period[M]', - ... freq='M') - + >>> df = pd.DataFrame({"y": [1,2,3]}, + ... index=pd.to_datetime(["2000-03-31 00:00:00", + ... "2000-05-31 00:00:00", + ... "2000-08-31 00:00:00"])) + >>> df.index = df.index.to_period("M") + >>> df + y + 2000-03 1 + 2000-05 2 + 2000-08 3 + See also -------- pandas.PeriodIndex: Immutable ndarray holding ordinal values From 1ac618ead124317e2a073e5a00a96cce8cf8384d Mon Sep 17 00:00:00 2001 From: Denniskamau Date: Sun, 11 Mar 2018 18:03:55 +0300 Subject: [PATCH 6/7] correct PEP8 issues --- pandas/core/indexes/datetimes.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 60de83ef24bd6..ec1f834938fcd 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1059,7 +1059,7 @@ def to_period(self, freq=None): """ Cast to PeriodIndex at a particular frequency. - Converts DatetimeIndex to PeriodIndex. + Converts DatetimeIndex to PeriodIndex Parameters ---------- @@ -1068,7 +1068,7 @@ def to_period(self, freq=None): Returns ------- - period : DatetimeIndex + period: DatetineIndex Examples -------- @@ -1078,11 +1078,11 @@ def to_period(self, freq=None): ... "2000-08-31 00:00:00"])) >>> df.index = df.index.to_period("M") >>> df - y - 2000-03 1 - 2000-05 2 - 2000-08 3 - + y + 2000-03 1 + 2000-05 2 + 2000-08 3 + See also -------- pandas.PeriodIndex: Immutable ndarray holding ordinal values @@ -1175,17 +1175,17 @@ def union(self, other): def to_perioddelta(self, freq): """ - Calculates TimedeltaIndex of difference between index - values and index converted to PeriodIndex at specified - freq. Used for vectorized offsets + Calculate TimedeltaIndex of difference between index + values and index converted to periodIndex at specified + freq. Used for vectorized offsets Parameters ---------- - freq : Period frequency + freq: Period frequency Returns ------- - y : TimedeltaIndex + y: TimedeltaIndex """ return to_timedelta(self.asi8 - self.to_period(freq) .to_timestamp().asi8) From fa32bcc8d9cc2bae182c2085a40ea2db4ed3cd2a Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 12 Mar 2018 11:17:14 -0500 Subject: [PATCH 7/7] Fixed Return type. Added reference to offsets. Added Raises. Added inferred example. --- pandas/core/indexes/datetimes.py | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index ec1f834938fcd..8b16391a8e8cd 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -1059,16 +1059,23 @@ def to_period(self, freq=None): """ Cast to PeriodIndex at a particular frequency. - Converts DatetimeIndex to PeriodIndex + Converts DatetimeIndex to PeriodIndex. Parameters ---------- - freq : string or pandas offset object, optional - One of pandas date offset string or corresponding objects. + freq : string or Offset, optional + One of pandas' :ref:`offset strings ` + or an Offset object. Will be inferred by default. Returns ------- - period: DatetineIndex + PeriodIndex + + Raises + ------ + ValueError + When converting a DatetimeIndex with non-regular values, so that a + frequency cannot be inferred. Examples -------- @@ -1076,12 +1083,16 @@ def to_period(self, freq=None): ... index=pd.to_datetime(["2000-03-31 00:00:00", ... "2000-05-31 00:00:00", ... "2000-08-31 00:00:00"])) - >>> df.index = df.index.to_period("M") - >>> df - y - 2000-03 1 - 2000-05 2 - 2000-08 3 + >>> df.index.to_period("M") + PeriodIndex(['2000-03', '2000-05', '2000-08'], + dtype='period[M]', freq='M') + + Infer the daily frequency + + >>> idx = pd.date_range("2017-01-01", periods=2) + >>> idx.to_period() + PeriodIndex(['2017-01-01', '2017-01-02'], + dtype='period[D]', freq='D') See also --------