Skip to content

Commit a855c28

Browse files
committed
Clarify the documentation for DataFrame.to_timestamp and DataFrame.to_timestamp
1 parent aa134bb commit a855c28

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

pandas/core/frame.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13151,9 +13151,11 @@ def to_timestamp(
1315113151
copy: bool | lib.NoDefault = lib.no_default,
1315213152
) -> DataFrame:
1315313153
"""
13154-
Cast PeriodIndex to DatetimeIndex of timestamps, at *beginning* of period.
13154+
Cast PeriodIndex to DatetimeIndex of timestamps.
1315513155
13156-
This can be changed to the *end* of the period, by specifying `how="e"`.
13156+
Will cast at *beginning* of period if `freq` which is the offset frequency
13157+
is None. This can be changed to the *end* of the period, by specifying
13158+
`how="e"`. Will cast to the *end* of the period when specifying `freq`.
1315713159
1315813160
Parameters
1315913161
----------
@@ -13211,15 +13213,29 @@ def to_timestamp(
1321113213
>>> df1.index
1321213214
DatetimeIndex(['2023-01-01', '2024-01-01'], dtype='datetime64[ns]', freq=None)
1321313215
13214-
Using `freq` which is the offset that the Timestamps will have
13216+
By specifying `how="e"` we can change the resulting timestamps to be
13217+
at the end of the year
1321513218
1321613219
>>> df2 = pd.DataFrame(data=d, index=idx)
13217-
>>> df2 = df2.to_timestamp(freq="M")
13218-
>>> df2
13220+
>>> df2 = df2.to_timestamp(how="e")
13221+
col1 col2
13222+
2023-12-31 23:59:59.999999999 1 3
13223+
2024-12-31 23:59:59.999999999 2 4
13224+
>>> df2.index
13225+
DatetimeIndex(['2023-12-31 23:59:59.999999999',
13226+
'2024-12-31 23:59:59.999999999'],
13227+
dtype='datetime64[ns]',
13228+
freq=None)
13229+
13230+
Using `freq` which is the offset that the Timestamps will have
13231+
13232+
>>> df3 = pd.DataFrame(data=d, index=idx)
13233+
>>> df3 = df3.to_timestamp(freq="M")
13234+
>>> df3
1321913235
col1 col2
1322013236
2023-01-31 1 3
1322113237
2024-01-31 2 4
13222-
>>> df2.index
13238+
>>> df3.index
1322313239
DatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)
1322413240
"""
1322513241
self._check_copy_deprecation(copy)

pandas/core/series.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5568,9 +5568,11 @@ def to_timestamp(
55685568
copy: bool | lib.NoDefault = lib.no_default,
55695569
) -> Series:
55705570
"""
5571-
Cast to DatetimeIndex of Timestamps, at *beginning* of period.
5571+
Cast PeriodIndex to DatetimeIndex of timestamps.
55725572
5573-
This can be changed to the *end* of the period, by specifying `how="e"`.
5573+
Will cast at *beginning* of period if `freq` which is the offset frequency
5574+
is None. This can be changed to the *end* of the period, by specifying
5575+
`how="e"`. Will cast to the *end* of the period when specifying `freq`.
55745576
55755577
Parameters
55765578
----------
@@ -5625,11 +5627,22 @@ def to_timestamp(
56255627
2025-01-01 3
56265628
Freq: YS-JAN, dtype: int64
56275629
5628-
Using `freq` which is the offset that the Timestamps will have
5630+
By specifying `how="e"` we can change the resulting timestamps to be
5631+
at the end of the year
56295632
56305633
>>> s2 = pd.Series([1, 2, 3], index=idx)
5631-
>>> s2 = s2.to_timestamp(freq="M")
5634+
>>> s2 = s2.to_timestamp(how="e")
56325635
>>> s2
5636+
2023-12-31 23:59:59.999999999 1
5637+
2024-12-31 23:59:59.999999999 2
5638+
2025-12-31 23:59:59.999999999 3
5639+
dtype: int64
5640+
5641+
Using `freq` which is the offset that the Timestamps will have
5642+
5643+
>>> s3 = pd.Series([1, 2, 3], index=idx)
5644+
>>> s3 = s3.to_timestamp(freq="M")
5645+
>>> s3
56335646
2023-01-31 1
56345647
2024-01-31 2
56355648
2025-01-31 3

0 commit comments

Comments
 (0)