从Excel中拖动date和时间到pandas,并将其结合到时间戳

最终编辑(希望):哦,我的上帝,你解决了它! 升级到pandas0.15.2后,这个解决scheme似乎工作:

trades['OEDatum'] = (trades[['OEDatum', 'OEUhrzeit']].apply (lambda x: dt.datetime.combine (x['OEDatum'].date(), x['OEUhrzeit']), axis=1)) 

非常感谢@EDChum和@joris


我试图通过read_excel将一些Excelsheet中的数据放入Pandas数据框中:

  Asset OEDatum OEUhrzeit ODatum OUhrzeit L/S Entrykurs \ Trade 1 EURUSD 2014-06-12 12:00:00 2014-06-12 12:23:09 L 1.2456 2 USDJPY 2014-11-11 10:15:35 2014-11-11 10:34:50 S 126.6300 3 EURJPY 2014-12-23 13:15:24 2014-12-23 13:25:45 L 114.4600 4 GBPJPY 2014-12-23 14:27:36 2014-12-23 14:35:56 S 156.6000 

我感兴趣的值有以下数据types:

 OEDatum datetime64[ns] OEUhrzeit object ODatum datetime64[ns] OUhrzeit object 

正如你所看到的,Pandas把date作为datetime64值和时间作为对象。

现在我需要将“OEDatum”与“OEUhrzeit”和“ODatum”与“OUhrzeit”结合到时间戳中。 这些时间戳稍后应该用于search大的tickdata文件。

但对我来说,把date和时间结合起来是不可能的。

在其他许多尝试中,我想将时间数据更改为string,然后使用“to_datetime”:

 trades.OEUhrzeit.apply(str) pd.to_datetime(trades.OEUhrzeit, utc=False, format='%H%M%S') 

但是接下来是这样的:

  Traceback (most recent call last): File "F:\Python Projekte\Test und Funktionsenwicklung\src\Tupel_und_ATR_Updater.py", line 251, in <module> trades_ohne_tupel() File "F:\Python Projekte\Test und Funktionsenwicklung\src\Tupel_und_ATR_Updater.py", line 173, in trades_ohne_tupel **pd.to_datetime(trades.OEUhrzeit, utc=False, format='%H%M%S') File "C:\Python34\lib\site-packages\pandas\tseries\tools.py", line 320, in to_datetime values = _convert_listlike(arg.values, False, format)** File "C:\Python34\lib\site-packages\pandas\tseries\tools.py", line 313, in _convert_listlike raise e File "C:\Python34\lib\site-packages\pandas\tseries\tools.py", line 287, in _convert_listlike arg, format, coerce=coerce File "tslib.pyx", line 1579, in pandas.tslib.array_strptime (pandas\tslib.c:25541) ValueError: time data datetime.time(12, 0) does not match format '%H%M%S' 

所以我希望有人能为我解决这个问题。 Thx提前。

编辑:@EDChum你是对的我用pandas0.14.1,numpy 1.8.2和Python 3.4.2 – 认为这意味着我必须更新我的pandas…….

一种方法是将时间string转换为date时间,但只取时间部分,然后调用apply并调用datetime.combine为两列生成date时间:

 In [61]: df['OEtime'] = pd.to_datetime(df['OEUhrzeit']).dt.time df['OEDatum'] = df[['OEDatum','OEtime']].apply(lambda x: dt.datetime.combine(x['OEDatum'].date(),x['OEtime']), axis=1) In [62]: df['OUhrtime'] = pd.to_datetime(df['OUhrzeit']).dt.time df['ODatum'] = df[['ODatum','OUhrtime']].apply(lambda x: dt.datetime.combine(x['ODatum'].date(),x['OUhrtime']), axis=1) df Out[62]: Trade Asset OEDatum OEUhrzeit ODatum OUhrzeit \ 0 1 EURUSD 2014-06-12 12:00:00 12:00:00 2014-06-12 12:23:09 12:23:09 1 2 USDJPY 2014-11-11 10:15:35 10:15:35 2014-11-11 10:34:50 10:34:50 2 3 EURJPY 2014-12-23 13:15:24 13:15:24 2014-12-23 13:25:45 13:25:45 3 4 GBPJPY 2014-12-23 14:27:36 14:27:36 2014-12-23 14:35:56 14:35:56 L/S Entrykurs OEtime OUhrtime 0 L 1.2456 12:00:00 12:23:09 1 S 126.6300 10:15:35 10:34:50 2 L 114.4600 13:15:24 13:25:45 3 S 156.6000 14:27:36 14:35:56 

编辑

它看起来像你的时间栏已经是一个datetime.time对象,所以只需要下面的工作:

 df['OEDatum'] = df[['OEDatum','OEUhrzeit']].apply(lambda x: dt.datetime.combine(x['OEDatum'].date(),x['OEUhrzeit']), axis=1) df['ODatum'] = df[['ODatum','OUhrzeit']].apply(lambda x: dt.datetime.combine(x['ODatum'].date(),x['OUhrzeit']), axis=1)