【问题标题】:Pulling date and time from Excel to Pandas and combining it to a timestamp将日期和时间从 Excel 提取到 Pandas 并将其组合到时间戳
【发布时间】:2015-08-26 02:47:28
【问题描述】:

最终编辑(希望如此): 天哪,你解决了! 升级到 Pandas 0.15.2 后,此解决方案似乎有效:

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

我感兴趣的值,有以下数据类型:

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

如您所见,Pandas 将日期提取为 datetime64 值,而时间是一个对象。

现在我需要将 'OEDatum' 与 'OEUhrzeit' 以及 'ODatum' 与 'OUhrzeit' 结合到时间戳中。这些时间戳稍后将用于搜索大型 tickdata 文件。

但我根本不可能将日期与时间结合起来......

在许多其他尝试中,我想将时间数据更改为字符串,然后使用“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'

所以我希望有人可以向我展示这个问题的解决方案。 提前谢谢。

编辑:@EDChum 你说得对,我使用 pandas 0.14.1、numpy 1.8.2 和 Python 3.4.2 - 认为这意味着我必须更新我的熊猫.......

【问题讨论】:

  • 我认为你不需要to_datetime 位,你可以试试df['OEDatum'] = df[['OEDatum','OEUhrzeit']].apply(lambda x: dt.datetime.combine(x['OEDatum'].date(),x['OEUhrzeit']), axis=1)
  • 是的,这就是解决方案,请参阅我的编辑。再次感谢!

标签: python excel datetime pandas


【解决方案1】:

一种方法是将时间字符串转换为日期时间,但只取时间部分,然后调用applydatetime.combine 为两列生成日期时间:

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)

【讨论】:

  • 不幸的是,这不起作用。我收到以下消息:'AttributeError: 'Series' object has no attribute 'dt''
  • 请在您的问题中编辑任何新信息,谢谢。同时发布你的 pandas、python、numpy 版本。
  • 听起来你运行的是早于 0.15.0 的 pandas 版本,你能确认一下
  • @EdChum 不需要第一行(OEUhrzeit 的转换)。时间值已经是 datetime.time 对象,所以 datetime.combine 已经可以工作了。 @nic 这意味着如果您无法升级,它也可以与 0.14 一起使用
  • 是的,这确实不清楚,但是您可以在问题的ValueError消息中看到它
猜你喜欢
  • 2017-02-01
  • 2020-06-27
  • 2021-12-03
  • 1970-01-01
  • 2015-01-14
  • 2011-05-06
  • 1970-01-01
  • 2018-07-09
相关资源
最近更新 更多