大佬教程收集整理的这篇文章主要介绍了将 Datetime 列和 Int 列合并为小时替换 00 - ValueError:组装映射至少需要 [年、月、日],大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含两列的数据框:
datetiR_74_11845@e
列 %Y-%m-%d
int
列它们代表一天和一个小时。
我想合并,因为我想给 prophet
所以我需要转换成这种格式:
YYYY-MM-DD HH:MM:SS
我试过这种方式:
example={"date":["2018-04-18","2018-04-18","2018-04-18"],"alert_h":[4,17,23]}
df=pd.DataFrame(examplE)
df['date']=pd.to_datetiR_74_11845@e(df['date']) #I am converTing str to datetiR_74_11845@e just for creaTing the example,I have already in the original df a datetiR_74_11845@e format
df.assign(date_h=pd.to_datetiR_74_11845@e(df[['date','alert_h']],format='%Y-%m-%d %h'))
我收到以下错误消息。
ValueError: to assemble mapPings requires at least that [year,month,day] be specifIEd: [day,year] is missing
我阅读了以下问题:
如果这是重复的,请随时关闭此问题我将研究任何其他链接,因为我无法找到解决我的问题的特定问题。
第一种方法:
newdf=df['date'].str.split('-',expand=TruE).assign(hour=df['alert_h'])
newdf=newdf.astype(str)
newdf.columns=['year','month','day','hour']
df['date_h']=pd.to_datetiR_74_11845@e(newdf,format='%Y-%m-%d %h:%m:%s')
第二种方法:
example={"date":["2018-04-18","2018-04-18","2018-04-18"],"alert_h":[4,17,23]}
df=pd.DataFrame(examplE)
date_h=df['date']+' '+df['alert_h'].astype(str)+':00:00'
df['date']=pd.to_datetiR_74_11845@e(df['date'])
df['date_h']=pd.to_datetiR_74_11845@e(date_h)
编辑(第三种方法):
@mrFuppes
的方法(效率更高):
df['date_h']=pd.to_datetiR_74_11845@e(df['date']) + pd.to_timedelta(df['alert_h'],unit='h')
以上是大佬教程为你收集整理的将 Datetime 列和 Int 列合并为小时替换 00 - ValueError:组装映射至少需要 [年、月、日]全部内容,希望文章能够帮你解决将 Datetime 列和 Int 列合并为小时替换 00 - ValueError:组装映射至少需要 [年、月、日]所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。