简体   繁体   English

由于额外的键,to_datetime装配错误

[英]to_datetime assemblage error due to extra keys

My pandas version is 0.23.4. 我的熊猫版本是0.23.4。

I tried to run this code: 我试着运行这段代码:

df['date_time'] = pd.to_datetime(df[['year','month','day','hour_scheduled_departure','minute_scheduled_departure']])  

and the following error appeared: 并出现以下错误:

extra keys have been passed to the datetime assemblage: [hour_scheduled_departure, minute_scheduled_departure] 额外的密钥已传递给日期时间组合:[hour_scheduled_departure,minute_scheduled_departure]

Any ideas of how to get the job done by pd.to_datetime? 有关如何通过pd.to_datetime完成工作的任何想法?

@anky_91 @ anky_91

In this image an extract of first 10 rows is presented. 在该图像中,呈现了前10行的提取。 First column [int32]: year; 第一栏[int32]:年; Second column[int32]: month; 第二栏[int32]:月; Third column[int32]: day; 第三栏[int32]:日; Fourth column[object]: hour; 第四栏[对象]:小时; Fifth column[object]: minute. 第五栏[对象]:分钟。 The length of objects is 2. 物体的长度是2。

Another solution: 另一种方案:

>>pd.concat([df.A,pd.to_datetime(pd.Series(df[df.columns[1:]].fillna('').values.tolist(),name='Date').map(lambda x: '0'.join(map(str,x))))],axis=1)

    A   Date
0   a   2002-07-01 05:07:00
1   b   2002-08-03 03:08:00
2   c   2002-09-05 06:09:00
3   d   2002-04-07 09:04:00
4   e   2002-02-01 02:02:00
5   f   2002-03-05 04:03:00

For the example you have added as image (i have skipped the last 3 columns due to save time) 对于您添加为图像的示例 (由于节省时间,我已跳过最后3列)

>>df.month=df.month.map("{:02}".format)
>>df.day = df.day.map("{:02}".format)
>>pd.concat([df.A,pd.to_datetime(pd.Series(df[df.columns[1:]].fillna('').values.tolist(),name='Date').map(lambda x: ''.join(map(str,x))))],axis=1)

    A   Date
0   a   2015-01-01 00:05:00
1   b   2015-01-01 00:01:00
2   c   2015-01-01 00:02:00
3   d   2015-01-01 00:02:00
4   e   2015-01-01 00:25:00
5   f   2015-01-01 00:25:00

You can use rename to columns, so possible use pandas.to_datetime with columns year, month, day, hour, minute : 您可以使用rename为列,因此可以将pandas.to_datetime与列year, month, day, hour, minute

df = pd.DataFrame({
        'A':list('abcdef'),
         'year':[2002,2002,2002,2002,2002,2002],
         'month':[7,8,9,4,2,3],
         'day':[1,3,5,7,1,5],
         'hour_scheduled_departure':[5,3,6,9,2,4],
         'minute_scheduled_departure':[7,8,9,4,2,3]
})

print (df)
   A  year  month  day  hour_scheduled_departure  minute_scheduled_departure
0  a  2002      7    1                         5                           7
1  b  2002      8    3                         3                           8
2  c  2002      9    5                         6                           9
3  d  2002      4    7                         9                           4
4  e  2002      2    1                         2                           2
5  f  2002      3    5                         4                           3

cols = ['year','month','day','hour_scheduled_departure','minute_scheduled_departure']
d = {'hour_scheduled_departure':'hour','minute_scheduled_departure':'minute'}
df['date_time'] = pd.to_datetime(df[cols].rename(columns=d)) 
#if necessary remove columns
df = df.drop(cols, axis=1) 
print (df)
   A           date_time
0  a 2002-07-01 05:07:00
1  b 2002-08-03 03:08:00
2  c 2002-09-05 06:09:00
3  d 2002-04-07 09:04:00
4  e 2002-02-01 02:02:00
5  f 2002-03-05 04:03:00

Detail : 细节

print (df[cols].rename(columns=d))
   year  month  day  hour  minute
0  2002      7    1     5       7
1  2002      8    3     3       8
2  2002      9    5     6       9
3  2002      4    7     9       4
4  2002      2    1     2       2
5  2002      3    5     4       3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM