合并列,pickup_date和pickup_time = +不支持的操作数类型:“ DatetimeIndex”和“ str”

时间:2019-12-29 19:51:47

标签: python pandas datetime

  1. 将两列,即pickup_date和pickup_time组合起来
  2. pickup_date = 2015-02-10
  3. pickup_24 = 08:46:15
  4. code = df [pickup_start] = pd.to_datetime(df ['pickup_date2'] +''+ df ['pickup_24'])
  5. 错误= +不支持的操作数类型:“ DatetimeIndex”和“ str”
  6. 帮助!

1 个答案:

答案 0 :(得分:0)

更新:

import pandas as pd

pickup_date2 = '2015-02-10' 
pickup_24 = '08:46:15'

在下面,我将您的时间和日期放在熊猫里。

df = pd.DataFrame({"pickup_date2":pickup_date2,"pickup_24":pickup_24}, index=[0])

添加这些列以创建一个新列,

df['date_time']=df['pickup_date2']+':'+df['pickup_24']

然后我们可以将此列转换为pandas datetime,

df['date_time'] = pd.to_datetime(df['date_time'],format='%Y-%m-%d:%H:%M:%S')

上面我指定了格式。

旧代码:

没有数据或代码,很难弄清楚您要做什么。

但是,您应该将这些列转换为日期时间。

df[pickup_date2] = pd.to_datetime(df['pickup_date2'])
df[pickup_24] = pd.to_datetime(df['pickup_24'])

然后

df[pickup_start] = df[pickup_date2] + df[pickup_24]

如果大熊猫可以轻松地检测出每列的格式,那么它应该可以工作。如果没有,请阅读to_datetimeformatting