Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Convert Timestamp to datetime.date

I have a pandas column of Timestamp data

In [27]: train["Original_Quote_Date"][6]  Out[27]: Timestamp('2013-12-25 00:00:00') 

How can check equivalence of these objects to datetime.date objects of the type

datetime.date(2013, 12, 25) 
like image 756
kilojoules Avatar asked Dec 20 '15 22:12

kilojoules


People also ask

How do I change time stamp to date in pandas?

To convert a Timestamp object to a native Python datetime object, use the timestamp. to_pydatetime() method.

How do I change timestamp to datetime?

Timestamp to DateTime object You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How do I convert a timestamp to a date in Python?

Import the “datetime” file to start timestamp conversion into a date. Create an object and initialize the value of the timestamp. Use the ” fromtimestamp ()” method to place either data or object. Print the date after conversion of the timestamp.

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

Use the .date method:

In [11]: t = pd.Timestamp('2013-12-25 00:00:00')  In [12]: t.date() Out[12]: datetime.date(2013, 12, 25)  In [13]: t.date() == datetime.date(2013, 12, 25) Out[13]: True 

To compare against a DatetimeIndex (i.e. an array of Timestamps), you'll want to do it the other way around:

In [21]: pd.Timestamp(datetime.date(2013, 12, 25)) Out[21]: Timestamp('2013-12-25 00:00:00')  In [22]: ts = pd.DatetimeIndex([t])  In [23]: ts == pd.Timestamp(datetime.date(2013, 12, 25)) Out[23]: array([ True], dtype=bool) 
like image 60
Andy Hayden Avatar answered Oct 02 '22 16:10

Andy Hayden