在 datetime 和 PandasTimestamp 对象之间进行转换

我有以下条件:

> date1
Timestamp('2014-01-23 00:00:00', tz=None)


> date2
datetime.date(2014, 3, 26)

我在 这个答案上读到,我可以用 pandas.to_datetime()Timestamps转换成 datetime对象,但是它似乎不起作用:

> pd.to_datetime(date1)
Timestamp('2014-01-23 00:00:00', tz=None)

为什么? 我如何在这两种格式之间转换?

173318 次浏览
>>> 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')

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)

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))

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.