Skip to content

Commit 66f7453

Browse files
committed
add doc strings for dt.to_timestamp
1 parent eb5b998 commit 66f7453

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

pandas/core/arrays/datetimes.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def _scalar_type(self) -> type[Timestamp]:
261261
)
262262
_datetimelike_methods: list[str] = [
263263
"to_period",
264+
"to_timestamp",
264265
"tz_localize",
265266
"tz_convert",
266267
"normalize",
@@ -1271,6 +1272,51 @@ def to_period(self, freq=None) -> PeriodArray:
12711272
freq = res
12721273
return PeriodArray._from_datetime64(self._ndarray, freq, tz=self.tz)
12731274

1275+
def to_timestamp(self):
1276+
"""
1277+
Convert ``Series`` of ``Periods`` to ``DatetimeIndex/Array``.
1278+
1279+
This method converts a Series of Periods to a ``DatetimeIndex/Array``.
1280+
The ``freq`` and ``how`` parameters cannot be used when calling
1281+
``to_timestamp`` the ``.dt`` accessor.
1282+
1283+
Returns
1284+
-------
1285+
DatetimeArray/Index
1286+
A `DatetimeArray` or `DatetimeIndex` object representing the period
1287+
converted to a timestamp.
1288+
1289+
See Also
1290+
--------
1291+
PeriodIndex.day : The days of the period.
1292+
PeriodIndex.from_fields : Construct a PeriodIndex from fields
1293+
(year, month, day, etc.).
1294+
PeriodIndex.from_ordinals : Construct a PeriodIndex from ordinals.
1295+
PeriodIndex.hour : The hour of the period.
1296+
PeriodIndex.minute : The minute of the period.
1297+
PeriodIndex.month : The month as January=1, December=12.
1298+
PeriodIndex.second : The second of the period.
1299+
PeriodIndex.year : The year of the period.
1300+
1301+
Examples
1302+
--------
1303+
>>> s = pd.Series(pd.period_range("2023-01", periods=3, freq="M"))
1304+
>>> s
1305+
0 2023-01
1306+
1 2023-02
1307+
2 2023-03
1308+
dtype: period[M]
1309+
>>> s.dt.to_timestamp()
1310+
0 2023-01-01
1311+
1 2023-02-01
1312+
2 2023-03-01
1313+
dtype: datetime64[ns]
1314+
"""
1315+
# Call the to_timestamp function in PeriodArray() class, ignoring freq and how
1316+
from pandas.core.arrays import PeriodArray
1317+
1318+
return PeriodArray(self._data).to_timestamp()
1319+
12741320
# -----------------------------------------------------------------
12751321
# Properties - Vectorized Timestamp Properties/Methods
12761322

0 commit comments

Comments
 (0)