Skip to content

DOC Fix EX01 issues in docstrings #51440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions ci/code_checks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Series.size \
pandas.Series.T \
pandas.Series.hasnans \
pandas.Series.to_timestamp \
pandas.Series.to_list \
pandas.Series.__iter__ \
pandas.Series.keys \
Expand Down Expand Up @@ -218,7 +217,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.Period.year \
pandas.Period.asfreq \
pandas.Period.now \
pandas.Period.to_timestamp \
pandas.arrays.PeriodArray \
pandas.Interval.closed \
pandas.Interval.left \
Expand Down Expand Up @@ -562,7 +560,6 @@ if [[ -z "$CHECK" || "$CHECK" == "docstrings" ]]; then
pandas.DataFrame.swapaxes \
pandas.DataFrame.first_valid_index \
pandas.DataFrame.last_valid_index \
pandas.DataFrame.to_timestamp \
pandas.DataFrame.attrs \
pandas.DataFrame.plot \
pandas.DataFrame.sparse.density \
Expand Down
7 changes: 7 additions & 0 deletions pandas/_libs/tslibs/period.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,13 @@ cdef class _Period(PeriodMixin):
Returns
-------
Timestamp

Examples
--------
>>> period = pd.Period('2023-1-1', freq='D')
>>> timestamp = period.to_timestamp()
>>> timestamp
Timestamp('2023-01-01 00:00:00')
"""
how = validate_end_alias(how)

Expand Down
31 changes: 31 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -11013,6 +11013,37 @@ def to_timestamp(
-------
DataFrame
The DataFrame has a DatetimeIndex.

Examples
--------
>>> idx = pd.PeriodIndex(['2023', '2024'], freq='Y')
>>> d = {'col1': [1, 2], 'col2': [3, 4]}
>>> df1 = pd.DataFrame(data=d, index=idx)
>>> df1
col1 col2
2023 1 3
2024 2 4

The resulting timestamps will be at the beginning of the year in this case

>>> df1 = df1.to_timestamp()
>>> df1
col1 col2
2023-01-01 1 3
2024-01-01 2 4
>>> 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

>>> df2 = pd.DataFrame(data=d, index=idx)
>>> df2 = df2.to_timestamp(freq='M')
>>> df2
col1 col2
2023-01-31 1 3
2024-01-31 2 4
>>> df2.index
DatetimeIndex(['2023-01-31', '2024-01-31'], dtype='datetime64[ns]', freq=None)
"""
new_obj = self.copy(deep=copy)

Expand Down
29 changes: 29 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5710,6 +5710,35 @@ def to_timestamp(
Returns
-------
Series with DatetimeIndex

Examples
--------
>>> idx = pd.PeriodIndex(['2023', '2024', '2025'], freq='Y')
>>> s1 = pd.Series([1, 2, 3], index=idx)
>>> s1
2023 1
2024 2
2025 3
Freq: A-DEC, dtype: int64

The resulting frequency of the Timestamps is `YearBegin`

>>> s1 = s1.to_timestamp()
>>> s1
2023-01-01 1
2024-01-01 2
2025-01-01 3
Freq: AS-JAN, dtype: int64

Using `freq` which is the offset that the Timestamps will have

>>> s2 = pd.Series([1, 2, 3], index=idx)
>>> s2 = s2.to_timestamp(freq='M')
>>> s2
2023-01-31 1
2024-01-31 2
2025-01-31 3
Freq: A-JAN, dtype: int64
"""
if not isinstance(self.index, PeriodIndex):
raise TypeError(f"unsupported Type {type(self.index).__name__}")
Expand Down