如何在Jupyter中合并日期时间列

时间:2019-01-02 17:12:01

标签: python pandas

如何将年,月和日合并为一个年月日 如何将小时和分钟合并为小时:分钟

我输入代码

import pandas as pd
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from datetime import datetime

df1 = pd.read_csv('D:\charlotte_rg_2003-2014.csv')
df1.head(3)

    Year    Month   Day Hour    Minute  R15
0   2003    1   1   0   0   0.0
1   2003    1   1   0   15  0.0
2   2003    1   1   0   30  0.0

df1['Year-Month-Day'] = 
pd.to_datetime(df1.Year+'/'+df1.Month+'/'+df1.Day)
df1['Hour:Minute'] = pd.to_datetime(df1.Hour+':'+df1.Minute,format='%H:%M').dt.strftime('%H:%M')
df1[['Year-Month-Day','Hour:Minute','R15']]

怎么了?现在我得到了错误:

~\AppData\Local\Continuum\anaconda3\lib\site-packages\pandas\core\ops.py in na_op(x, y)
   1008         try:
-> 1009             result = expressions.evaluate(op, str_rep, x, y, **eval_kwargs)
   1010         except TypeError:
 TypeError: ufunc 'add' did not contain a loop with signature matching types dtype('<U21') dtype('<U21') dtype('<U21')

非常感谢!

0 个答案:

没有答案