Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting between datetime and Pandas Timestamp objects

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?

like image 701
Amelio Vazquez-Reina Avatar asked Apr 02 '14 23:04

Amelio Vazquez-Reina


People also ask

Is timestamp the same as datetime pandas?

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.


1 Answers

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) 
like image 55
Andy Hayden Avatar answered Sep 28 '22 02:09

Andy Hayden