Skip to content

Commit 0cd5c5c

Browse files
priya-gitTestjorisvandenbossche
authored andcommitted
Docstring changes to pandas.Series.dt.to_pydatetime (#20198)
1 parent 20acbd1 commit 0cd5c5c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

pandas/core/indexes/accessors.py

+43
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,49 @@ class DatetimeProperties(Properties):
126126
"""
127127

128128
def to_pydatetime(self):
129+
"""
130+
Return the data as an array of native Python datetime objects
131+
132+
Timezone information is retained if present.
133+
134+
.. warning::
135+
136+
Python's datetime uses microsecond resolution, which is lower than
137+
pandas (nanosecond). The values are truncated.
138+
139+
Returns
140+
-------
141+
numpy.ndarray
142+
object dtype array containing native Python datetime objects.
143+
144+
See Also
145+
--------
146+
datetime.datetime : Standard library value for a datetime.
147+
148+
Examples
149+
--------
150+
>>> s = pd.Series(pd.date_range('20180310', periods=2))
151+
>>> s
152+
0 2018-03-10
153+
1 2018-03-11
154+
dtype: datetime64[ns]
155+
156+
>>> s.dt.to_pydatetime()
157+
array([datetime.datetime(2018, 3, 10, 0, 0),
158+
datetime.datetime(2018, 3, 11, 0, 0)], dtype=object)
159+
160+
pandas' nanosecond precision is truncated to microseconds.
161+
162+
>>> s = pd.Series(pd.date_range('20180310', periods=2, freq='ns'))
163+
>>> s
164+
0 2018-03-10 00:00:00.000000000
165+
1 2018-03-10 00:00:00.000000001
166+
dtype: datetime64[ns]
167+
168+
>>> s.dt.to_pydatetime()
169+
array([datetime.datetime(2018, 3, 10, 0, 0),
170+
datetime.datetime(2018, 3, 10, 0, 0)], dtype=object)
171+
"""
129172
return self._get_values().to_pydatetime()
130173

131174
@property

0 commit comments

Comments
 (0)