Pandas: Convert Timestamp to datetime.date
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)
As of pandas 0.20.3, use .to_pydatetime()
to convert any pandas.DateTimeIndex
instances to Python datetime.datetime
.
Assume time column is in timestamp integer msec format
1 day = 86400000 ms
Here you go:
day_divider = 86400000df['time'] = df['time'].values.astype(dtype='datetime64[ms]') # for msec formatdf['time'] = (df['time']/day_divider).values.astype(dtype='datetime64[D]') # for day format