Skip to content

DOC: Clarify existence Series.dt.to_timestamp #59671

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

Open
1 task done
sfc-gh-joshi opened this issue Aug 30, 2024 · 5 comments
Open
1 task done

DOC: Clarify existence Series.dt.to_timestamp #59671

sfc-gh-joshi opened this issue Aug 30, 2024 · 5 comments
Labels
Accessors accessor registration mechanism (not .str, .dt, .cat) Datetime Datetime data dtype Docs

Comments

@sfc-gh-joshi
Copy link

Pandas version checks

  • I have checked that the issue still exists on the latest versions of the docs on main here

Location of the documentation

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.to_timestamp.html (does not exist)

Documentation problem

Copied from a Modin issue: modin-project/modin#7232 (comment)

Series.dt.to_timestamp() is a valid method for PeriodProperties, but not for DatetimeProperties, TimedeltaProperties. For example, following is valid:

>>> seconds_series = pd.Series(pd.period_range(start="2000-01-01 00:00:00", end="2000-01-01 00:00:03", freq="s"))
>>> seconds_series
0    2000-01-01 00:00:00
1    2000-01-01 00:00:01
2    2000-01-01 00:00:02
3    2000-01-01 00:00:03
dtype: period[s]
>>> seconds_series.dt.to_timestamp()
0   2000-01-01 00:00:00
1   2000-01-01 00:00:01
2   2000-01-01 00:00:02
3   2000-01-01 00:00:03
dtype: datetime64[ns]

However, it is invalid for timedelta:

>>> seconds_series = pd.Series(pd.timedelta_range(start="1 second", periods=3, freq="s"))
>>> seconds_series.dt.to_timestamp()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'TimedeltaProperties' object has no attribute 'to_timestamp'
>>> seconds_series
0   0 days 00:00:01
1   0 days 00:00:02
2   0 days 00:00:03
dtype: timedelta64[ns]

Suggested fix for documentation

Clarify whether to_timestamp should exist for all Series.dt accessors, and whether it should be implemented for classes other than PeriodProperties.

@sfc-gh-joshi sfc-gh-joshi added Docs Needs Triage Issue that has not been reviewed by a pandas team member labels Aug 30, 2024
@rhshadrach rhshadrach added Datetime Datetime data dtype Needs Discussion Requires discussion from core team before further action Accessors accessor registration mechanism (not .str, .dt, .cat) and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Sep 4, 2024
@saldanhad
Copy link
Contributor

saldanhad commented Sep 21, 2024

Hi @sfc-gh-joshi, you can use to_timestamp() method with dt accessors however, the pre-requisite is to have a specific period index, which means dates have to be specific eg: 2000-01-01 00:00:01 and not relative like 0 days, 1 days etc as can be seen in your example which is why you get the error.

If however you needed them in datetime format you could add a base_date as reference as shown below which could then convert it without the need to use to_timestamp().

seconds_series = pd.Series(pd.timedelta_range(start="1 second", periods=3, freq="s"))
base_date = pd.Timestamp('2023-01-01')
result = seconds_series + base_date
result 

2023-01-01 00:00:01
2023-01-01 00:00:02
2023-01-01 00:00:03

dtype: datetime64[ns]

@saldanhad
Copy link
Contributor

saldanhad commented Sep 21, 2024

Hi @sfc-gh-joshi, you can use to_timestamp() method with dt accessors however, the pre-requisite is to have a specific period index, which means dates have to be specific eg: 2000-01-01 00:00:01 and not relative like 0 days, 1 days etc as can be seen in your example which is why you get the error, hope this helps.

Hi @rhshadrach , Congrats on the recent release of pandas 2.2.3, with respect to this issue I believe adding the above, along with a brief explanation on how using different freq values affect the output could help here.
Lmkyt, and I would be happy to release a PR to address this if needed?

@rhshadrach
Copy link
Member

I think Series.dt.to_timestamp should be added to the API docs. But I think it makes sense to not implement to_timestamp for datetime and timedelta. Labeling this as a docs issue, @saldanhad - a PR is welcome!

@rhshadrach rhshadrach removed the Needs Discussion Requires discussion from core team before further action label Sep 21, 2024
@rhshadrach
Copy link
Member

@saldanhad - just take, no slash.

@saldanhad
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Accessors accessor registration mechanism (not .str, .dt, .cat) Datetime Datetime data dtype Docs
Projects
None yet
Development

No branches or pull requests

3 participants