From 6480b225f0e78609fec7bee149d1744550399a14 Mon Sep 17 00:00:00 2001 From: Priyanka Ojha Date: Sat, 10 Mar 2018 16:16:47 +0100 Subject: [PATCH 1/9] PO: made docstring changes to pandas.Series.dt.to_pydatetime --- pandas/core/indexes/accessors.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index c5b300848876e..3cac2c7816cee 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -126,6 +126,31 @@ class DatetimeProperties(Properties): """ def to_pydatetime(self): + """ + Return DatetimeIndex as object ndarray of datetime.datetime objects. + + This function converts the Pandas Series DatetimeIndex to + a ndarray object. These two types are different and hence + 2 differnt functions. + + Returns + ------- + datetimes : ndarray + + See Also + -------- + pd.DatetimeIndex.to_pydatetime : Convert a Timestamp object to a native Python datetime object. + pd.date.dt.values.to_datetime : For an Index containing strings or datetime.datetime objects, attempt + conversion to DatetimeIndex. + + Examples + -------- + >>> df = pd.DataFrame({'date': [pd.to_datetime('2018-03-10'), pd.to_datetime('2017-02-01')]}) + >>> type(df) + + >>> type(df.date.dt.to_pydatetime()) + + """ return self._get_values().to_pydatetime() @property From e4dbd78f9c4644bab28ff7da3b7b491be909085e Mon Sep 17 00:00:00 2001 From: Priyanka Ojha Date: Sat, 10 Mar 2018 18:03:00 +0100 Subject: [PATCH 2/9] Docstring changes to pandas.Series.dt.to_pydatetime, fixed PEP8 errors --- pandas/core/indexes/accessors.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 3cac2c7816cee..f4b4043ae2e83 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -139,9 +139,10 @@ def to_pydatetime(self): See Also -------- - pd.DatetimeIndex.to_pydatetime : Convert a Timestamp object to a native Python datetime object. - pd.date.dt.values.to_datetime : For an Index containing strings or datetime.datetime objects, attempt - conversion to DatetimeIndex. + pd.DatetimeIndex.to_pydatetime : Convert a Timestamp object to a native + Python datetime object. + pd.date.dt.values.to_datetime : For an Index containing strings or + datetime.datetime objects, attempt conversion to DatetimeIndex. Examples -------- From c83eff0a2478b286de1a529944eb273f6b1fd4ab Mon Sep 17 00:00:00 2001 From: Priyanka Ojha Date: Sat, 10 Mar 2018 21:13:44 +0100 Subject: [PATCH 3/9] DOCSTRING : pandas.Series.dt.to_pydatetime : Incorporated Reviewers Feedback --- pandas/core/indexes/accessors.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index f4b4043ae2e83..212b6b315efbd 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -127,29 +127,23 @@ class DatetimeProperties(Properties): def to_pydatetime(self): """ - Return DatetimeIndex as object ndarray of datetime.datetime objects. - - This function converts the Pandas Series DatetimeIndex to - a ndarray object. These two types are different and hence - 2 differnt functions. + Return DatetimeIndex as an object ndarray. Returns ------- - datetimes : ndarray + ndarray of object dtype See Also -------- - pd.DatetimeIndex.to_pydatetime : Convert a Timestamp object to a native + pandas.DatetimeIndex.to_pydatetime : Convert a Timestamp object to a native Python datetime object. - pd.date.dt.values.to_datetime : For an Index containing strings or - datetime.datetime objects, attempt conversion to DatetimeIndex. Examples -------- - >>> df = pd.DataFrame({'date': [pd.to_datetime('2018-03-10'), pd.to_datetime('2017-02-01')]}) + >>> df = pd.Series(pd.date_range('20180310', periods=2)) >>> type(df) - - >>> type(df.date.dt.to_pydatetime()) + + >>> type(df.dt.to_pydatetime()) """ return self._get_values().to_pydatetime() From 4f73176c0ed0e95556a539cf763bb213f528b1c9 Mon Sep 17 00:00:00 2001 From: Priyanka Ojha Date: Sun, 11 Mar 2018 00:26:07 +0100 Subject: [PATCH 4/9] DOCSTRING : pandas.Series.dt.to_pydatetime : Incorporated Reviewers comments --- pandas/core/indexes/accessors.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 212b6b315efbd..667d3eeef4ae9 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -127,11 +127,11 @@ class DatetimeProperties(Properties): def to_pydatetime(self): """ - Return DatetimeIndex as an object ndarray. + Return Series as a native Python datetime objects. Returns ------- - ndarray of object dtype + Series of object dtype See Also -------- @@ -140,11 +140,14 @@ def to_pydatetime(self): Examples -------- - >>> df = pd.Series(pd.date_range('20180310', periods=2)) - >>> type(df) - - >>> type(df.dt.to_pydatetime()) - + >>> s = pd.Series(pd.date_range('20180310', periods=2)) + >>> s.head() + 0 2018-03-10 + 1 2018-03-11 + dtype: datetime64[ns] + >>> s.dt.to_pydatetime() + array([datetime.datetime(2018, 3, 10, 0, 0), + datetime.datetime(2018, 3, 11, 0, 0)], dtype=object) """ return self._get_values().to_pydatetime() From 35ce553061adb2ca6c7f29549f8ba3682cbe4eb2 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sun, 11 Mar 2018 10:20:15 -0500 Subject: [PATCH 5/9] Updated return time, added DTI example --- pandas/core/indexes/accessors.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 667d3eeef4ae9..1dab503898568 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -127,27 +127,41 @@ class DatetimeProperties(Properties): def to_pydatetime(self): """ - Return Series as a native Python datetime objects. + Return an ndarray of native Python datetime objects. + + Timezone information is retained if present. Returns ------- - Series of object dtype - - See Also - -------- - pandas.DatetimeIndex.to_pydatetime : Convert a Timestamp object to a native - Python datetime object. + numpy.ndarray + object dtype array containing native Python datetime objects. Examples -------- + This method is available on both Series with datetime values, under the + ``.dt`` accessor, and directly on DatetimeIndex. + + **Series** + >>> s = pd.Series(pd.date_range('20180310', periods=2)) >>> s.head() 0 2018-03-10 1 2018-03-11 dtype: datetime64[ns] + >>> s.dt.to_pydatetime() array([datetime.datetime(2018, 3, 10, 0, 0), - datetime.datetime(2018, 3, 11, 0, 0)], dtype=object) + datetime.datetime(2018, 3, 11, 0, 0)], dtype=object) + + **DatetimeIndex** + >>> idx = pd.date_range("2018-03-10", periods=2) + >>> idx # doctest: +NORMALIZE_WHITESPACE + DatetimeIndex(['2018-03-10', '2018-03-11'], + dtype='datetime64[ns]', freq='D') + + >>> idx.to_pydatetime() + array([datetime.datetime(2018, 3, 10, 0, 0), + datetime.datetime(2018, 3, 11, 0, 0)], dtype=object) """ return self._get_values().to_pydatetime() From 0279ae8752f91c32a0fb4b179c5f3cf5db111cea Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 12 Mar 2018 11:24:38 -0500 Subject: [PATCH 6/9] Removed DTI references --- pandas/core/indexes/accessors.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 1dab503898568..cdaa25b868f87 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -138,11 +138,6 @@ def to_pydatetime(self): Examples -------- - This method is available on both Series with datetime values, under the - ``.dt`` accessor, and directly on DatetimeIndex. - - **Series** - >>> s = pd.Series(pd.date_range('20180310', periods=2)) >>> s.head() 0 2018-03-10 @@ -150,16 +145,6 @@ def to_pydatetime(self): dtype: datetime64[ns] >>> s.dt.to_pydatetime() - array([datetime.datetime(2018, 3, 10, 0, 0), - datetime.datetime(2018, 3, 11, 0, 0)], dtype=object) - - **DatetimeIndex** - >>> idx = pd.date_range("2018-03-10", periods=2) - >>> idx # doctest: +NORMALIZE_WHITESPACE - DatetimeIndex(['2018-03-10', '2018-03-11'], - dtype='datetime64[ns]', freq='D') - - >>> idx.to_pydatetime() array([datetime.datetime(2018, 3, 10, 0, 0), datetime.datetime(2018, 3, 11, 0, 0)], dtype=object) """ From d8fe91de8a71f3115f13dea91e8830ea8a67905d Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Mon, 12 Mar 2018 13:12:10 -0500 Subject: [PATCH 7/9] Added warning --- pandas/core/indexes/accessors.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index cdaa25b868f87..d28a6dede7028 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -131,11 +131,20 @@ def to_pydatetime(self): Timezone information is retained if present. + .. warning:: + + Python's datetimes use microsecond resolution, which is lower than + pandas' (nanosecond). The values are truncated. + Returns ------- numpy.ndarray object dtype array containing native Python datetime objects. + See Also + -------- + datetime.datetime : Standard library value for a datetime. + Examples -------- >>> s = pd.Series(pd.date_range('20180310', periods=2)) @@ -147,6 +156,17 @@ def to_pydatetime(self): >>> s.dt.to_pydatetime() array([datetime.datetime(2018, 3, 10, 0, 0), datetime.datetime(2018, 3, 11, 0, 0)], dtype=object) + + pandas' nanosecond precision is truncated to microseconds. + + >>> idx = pd.date_range('2017', periods=2, freq='ns') + >>> idx + DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 00:00:00.000000001'], + dtype='datetime64[ns]', freq='N') + + >>> idx.to_pydatetime() + array([datetime.datetime(2017, 1, 1, 0, 0), + datetime.datetime(2017, 1, 1, 0, 0)], dtype=object) """ return self._get_values().to_pydatetime() From 34b4e99cb29d494010df65537c1ba7bb5959bb9d Mon Sep 17 00:00:00 2001 From: Priyanka Ojha Date: Tue, 13 Mar 2018 03:20:30 +0100 Subject: [PATCH 8/9] Docstring_series_dt_to_pydatetime : Added example of Series for nanosecond scenario --- pandas/core/indexes/accessors.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index d28a6dede7028..651ee727d6273 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -133,8 +133,8 @@ def to_pydatetime(self): .. warning:: - Python's datetimes use microsecond resolution, which is lower than - pandas' (nanosecond). The values are truncated. + Python's datetime uses microsecond resolution, which is lower than + pandas (nanosecond). The values are truncated. Returns ------- @@ -148,7 +148,7 @@ def to_pydatetime(self): Examples -------- >>> s = pd.Series(pd.date_range('20180310', periods=2)) - >>> s.head() + >>> s 0 2018-03-10 1 2018-03-11 dtype: datetime64[ns] @@ -159,14 +159,15 @@ def to_pydatetime(self): pandas' nanosecond precision is truncated to microseconds. - >>> idx = pd.date_range('2017', periods=2, freq='ns') - >>> idx - DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 00:00:00.000000001'], - dtype='datetime64[ns]', freq='N') + >>> s = pd.Series(pd.date_range('20180310', periods=2, freq='ns')) + >>> s + 0 2018-03-10 00:00:00.000000000 + 1 2018-03-10 00:00:00.000000001 + dtype: datetime64[ns] - >>> idx.to_pydatetime() - array([datetime.datetime(2017, 1, 1, 0, 0), - datetime.datetime(2017, 1, 1, 0, 0)], dtype=object) + >>> s.dt.to_pydatetime() + array([datetime.datetime(2018, 3, 10, 0, 0), + datetime.datetime(2018, 3, 10, 0, 0)], dtype=object) """ return self._get_values().to_pydatetime() From 932feff6ea76c250702588c92b7feb53a6d587a6 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 13 Mar 2018 11:52:10 +0100 Subject: [PATCH 9/9] Update accessors.py --- pandas/core/indexes/accessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 651ee727d6273..c27754d57d82b 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -127,7 +127,7 @@ class DatetimeProperties(Properties): def to_pydatetime(self): """ - Return an ndarray of native Python datetime objects. + Return the data as an array of native Python datetime objects Timezone information is retained if present.