84

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)
kilojoules
  • 8,814
  • 17
  • 70
  • 133
  • 3
    See also the related question for *datetime*: http://stackoverflow.com/questions/13703720/converting-between-datetime-timestamp-and-datetime64 – Andy Hayden Dec 20 '15 at 23:09

5 Answers5

101

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)
Andy Hayden
  • 328,850
  • 93
  • 598
  • 514
  • 2
    For an entire column or series, just use this in conjunction with an apply method and lambda. For example, if t is a series of timestamps: t.apply(lambda x: x.date()) – Den Thap Dec 02 '19 at 19:41
  • 3
    It is worth to mention that the time part is _lost_ and only date part is kept. For those who need to keep time, use `.to_pydatetime()` as mentioned by Xavier Ho. – Mincong Huang Feb 23 '20 at 08:27
  • There is a corresponding `.time()` method that drops the date and returns just the `datetime.time` component – BallpointBen Mar 01 '21 at 16:29
27

As of pandas 0.20.3, use .to_pydatetime() to convert any pandas.DateTimeIndex instances to Python datetime.datetime.

Xavier Ho
  • 15,527
  • 9
  • 47
  • 52
  • 6
    Worth noting that for large DatetimeIndexs this can be slow / lot of memory. This is because a DatetimeIndex is basically just a light wrapper around an array of int64s, whilst an array of python datetimes is an array of fully-fledged python objects/not compactly laid out. – Andy Hayden Mar 18 '18 at 19:45
6

You can convert a datetime.date object into a pandas Timestamp like this:

#!/usr/bin/env python3
# coding: utf-8

import pandas as pd
import datetime

# create a datetime data object
d_time = datetime.date(2010, 11, 12)

# create a pandas Timestamp object
t_stamp = pd.to_datetime('2010/11/12')

# cast `datetime_timestamp` as Timestamp object and compare
d_time2t_stamp = pd.to_datetime(d_time)

# print to double check
print(d_time)
print(t_stamp)
print(d_time2t_stamp)

# since the conversion succeds this prints `True`
print(d_time2t_stamp == t_stamp)
albert
  • 7,127
  • 8
  • 42
  • 75
5

Assume time column is in timestamp integer msec format

1 day = 86400000 ms

Here you go:

day_divider = 86400000

df['time'] = df['time'].values.astype(dtype='datetime64[ms]') # for msec format

df['time'] = (df['time']/day_divider).values.astype(dtype='datetime64[D]') # for day format
Alperen Sözer
  • 175
  • 2
  • 6
2

So, got this from an IBM coursera tutorial.

data['date'] = data['TimeStamp'].apply(lambda d: datetime.date.fromtimestamp(d))
Ali karimi
  • 63
  • 5