Skip to content

Commit

Permalink
Clarify the documentation for DataFrame.to_timestamp and DataFrame.to…
Browse files Browse the repository at this point in the history
…_timestamp
  • Loading branch information
natmokval committed Aug 8, 2024
1 parent aa134bb commit a855c28
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
28 changes: 22 additions & 6 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -13151,9 +13151,11 @@ def to_timestamp(
copy: bool | lib.NoDefault = lib.no_default,
) -> DataFrame:
"""
Cast PeriodIndex to DatetimeIndex of timestamps, at *beginning* of period.
Cast PeriodIndex to DatetimeIndex of timestamps.
This can be changed to the *end* of the period, by specifying `how="e"`.
Will cast at *beginning* of period if `freq` which is the offset frequency
is None. This can be changed to the *end* of the period, by specifying
`how="e"`. Will cast to the *end* of the period when specifying `freq`.
Parameters
----------
Expand Down Expand Up @@ -13211,15 +13213,29 @@ def to_timestamp(
>>> df1.index
DatetimeIndex(['2023-01-01', '2024-01-01'], dtype='datetime64[ns]', freq=None)
Using `freq` which is the offset that the Timestamps will have
By specifying `how="e"` we can change the resulting timestamps to be
at the end of the year
>>> df2 = pd.DataFrame(data=d, index=idx)
>>> df2 = df2.to_timestamp(freq="M")
>>> df2
>>> df2 = df2.to_timestamp(how="e")
col1 col2
2023-12-31 23:59:59.999999999 1 3
2024-12-31 23:59:59.999999999 2 4
>>> df2.index
DatetimeIndex(['2023-12-31 23:59:59.999999999',
'2024-12-31 23:59:59.999999999'],
dtype='datetime64[ns]',
freq=None)
Using `freq` which is the offset that the Timestamps will have
>>> df3 = pd.DataFrame(data=d, index=idx)
>>> df3 = df3.to_timestamp(freq="M")
>>> df3
col1 col2
2023-01-31 1 3
2024-01-31 2 4
>>> df2.index
>>> df3.index
DatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)
"""
self._check_copy_deprecation(copy)
Expand Down
21 changes: 17 additions & 4 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5568,9 +5568,11 @@ def to_timestamp(
copy: bool | lib.NoDefault = lib.no_default,
) -> Series:
"""
Cast to DatetimeIndex of Timestamps, at *beginning* of period.
Cast PeriodIndex to DatetimeIndex of timestamps.
This can be changed to the *end* of the period, by specifying `how="e"`.
Will cast at *beginning* of period if `freq` which is the offset frequency
is None. This can be changed to the *end* of the period, by specifying
`how="e"`. Will cast to the *end* of the period when specifying `freq`.
Parameters
----------
Expand Down Expand Up @@ -5625,11 +5627,22 @@ def to_timestamp(
2025-01-01 3
Freq: YS-JAN, dtype: int64
Using `freq` which is the offset that the Timestamps will have
By specifying `how="e"` we can change the resulting timestamps to be
at the end of the year
>>> s2 = pd.Series([1, 2, 3], index=idx)
>>> s2 = s2.to_timestamp(freq="M")
>>> s2 = s2.to_timestamp(how="e")
>>> s2
2023-12-31 23:59:59.999999999 1
2024-12-31 23:59:59.999999999 2
2025-12-31 23:59:59.999999999 3
dtype: int64
Using `freq` which is the offset that the Timestamps will have
>>> s3 = pd.Series([1, 2, 3], index=idx)
>>> s3 = s3.to_timestamp(freq="M")
>>> s3
2023-01-31 1
2024-01-31 2
2025-01-31 3
Expand Down

0 comments on commit a855c28

Please sign in to comment.