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?
Timestamp is the pandas equivalent of python's Datetime and is interchangeable with it in most cases. It's the type used for the entries that make up a DatetimeIndex, and other timeseries oriented data structures in pandas.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With