Python Pandas将日期和小时合并为一列

时间:2016-04-19 23:29:32

标签: python datetime pandas

如何将var vid = document.getElementById('myvid'), ticks = 50, // number of frames during fast-forward frms = 10, // number of milleseconds between frames in fast-forward/rewind endtime = 24.0; // time to fast-forward/remind to (in seconds) // fast-forward/rewind video to end time var tdelta = (endtime - vid.currentTime)/ticks; for ( var i = 0; i < ticks; ++i ) { (function(j){ setTimeout(function() { vid.currentTime = vid.currentTime + tdelta * j; }, j * frms); })(i); } (datetime64 [ns])和Day(int64)合并到一个日期时间列中?

Hour

2 个答案:

答案 0 :(得分:6)

df = pd.DataFrame({
    'Day': np.array(['2010-04-24', '2012-08-20', '2016-03-06', '2016-01-02', '2010-12-21'], dtype=np.datetime64), 
    'Hour': np.array([17, 10, 9, 10, 4], dtype=np.int64)})

>>> pd.to_datetime(df.Day) + pd.to_timedelta(df.Hour, unit='h')
0   2010-04-24 17:00:00
1   2012-08-20 10:00:00
2   2016-03-06 09:00:00
3   2016-01-02 10:00:00
4   2010-12-21 04:00:00
dtype: datetime64[ns]

答案 1 :(得分:1)

使用此列表推导创建一个使用dt.timedelta添加一小时到一天的列:

data = {'Day':pd.to_datetime(['2010-04-24','2012-08-20','2016-03-06','2016-01-02','2010-12-21']),'Hour':[17,10,9,10,4]}
df = pd.DataFrame(data)
df['Datetime'] = [df.loc[x,'Day'] + dt.timedelta(hours = int(df.loc[x,'Hour'])) for x in list(df.index)]

这假设Day列的小时/分钟/秒都是00:00:00

返回

Day         Hour    Datetime
2010-04-24  17      2010-04-24 17:00:00
2012-08-20  10      2012-08-20 10:00:00
2016-03-06  9       2016-03-06 09:00:00
2016-01-02  10      2016-01-02 10:00:00
2010-12-21  4       2010-12-21 04:00:00