I have the following:
> date1
Timestamp('2014-01-23 00:00:00', tz=None)
> date2
datetime.date(2014, 3, 26)
and I read on this answer that I could use pandas.to_datetime() to convert from Timestamps to datetime objects, but it doesn’t seem to work:
> pd.to_datetime(date1)
Timestamp('2014-01-23 00:00:00', tz=None)
Why? How can I convert between these two formats?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use the to_pydatetime method to be more explicit:
In [11]: ts = pd.Timestamp('2014-01-23 00:00:00', tz=None)
In [12]: ts.to_pydatetime()
Out[12]: datetime.datetime(2014, 1, 23, 0, 0)
It’s also available on a DatetimeIndex:
In [13]: rng = pd.date_range('1/10/2011', periods=3, freq='D')
In [14]: rng.to_pydatetime()
Out[14]:
array([datetime.datetime(2011, 1, 10, 0, 0),
datetime.datetime(2011, 1, 11, 0, 0),
datetime.datetime(2011, 1, 12, 0, 0)], dtype=object)
Method 2
Pandas Timestamp to datetime.datetime:
pd.Timestamp('2014-01-23 00:00:00', tz=None).to_pydatetime()
datetime.datetime to Timestamp
pd.Timestamp(datetime(2014, 1, 23))
Method 3
>>> pd.Timestamp('2014-01-23 00:00:00', tz=None).to_datetime()
datetime.datetime(2014, 1, 23, 0, 0)
>>> pd.Timestamp(datetime.date(2014, 3, 26))
Timestamp('2014-03-26 00:00:00')
Method 4
To answer the question of going from an existing python datetime to a pandas Timestamp do the following:
import time, calendar, pandas as pd
from datetime import datetime
def to_posix_ts(d: datetime, utc:bool=True) -> float:
tt=d.timetuple()
return (calendar.timegm(tt) if utc else time.mktime(tt)) + round(d.microsecond/1000000, 0)
def pd_timestamp_from_datetime(d: datetime) -> pd.Timestamp:
return pd.to_datetime(to_posix_ts(d), unit='s')
dt = pd_timestamp_from_datetime(datetime.now())
print('({}) {}'.format(type(dt), dt))
Output:
(<class 'pandas._libs.tslibs.timestamps.Timestamp'>) 2020-09-05 23:38:55
I was hoping for a more elegant way to do this but the to_posix_ts is already in my standard tool chain so I’m moving on.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0