strptime()使用python从时间戳中提取日期时出错

时间:2016-08-02 09:05:17

标签: python datetime pandas

我尝试从时间戳中提取小时: 我有一个名为df_no_missing的数据框:

df_no_missing.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 34673 entries, 1 to 43228
Data columns (total 8 columns):
TIMESTAMP        34673 non-null object
P_ACT_KW         34673 non-null float64
PERIODE_TARIF    34673 non-null object
P_SOUSCR         34673 non-null float64
SITE             34673 non-null object
TARIF            34673 non-null object
depassement      34673 non-null float64
date             34673 non-null int64
dtypes: float64(3), int64(1), object(4)
memory usage: 2.4+ MB

这是我的代码:

from datetime import datetime,timedelta
mytime = datetime.strptime(df_no_missing["TIMESTAMP"],"%d/%m/%Y %H:%M")
print (mytime.day)
print (mytime.hour)

我收到此错误:

  

in()         1 df_no_missing.info()         2来自datetime import datetime,timedelta   ----&GT; 3 mytime = datetime.strptime(df_no_missing [&#34; TIMESTAMP&#34;],&#34;%d /%m /%Y%H:%M&#34;)         4打印(mytime.day)         5打印(mytime.hour)

     

TypeError:strptime()参数1必须是str,而不是Series

1 个答案:

答案 0 :(得分:0)

您可能希望在单个条目而不是数据框对象本身上使用datetime.strptime?尝试类似:

for data in df_no_missing["TIMESTAMP"]:
    mytime = datetime.strptime(data,"%d/%m/%Y %H:%M")
    print (mytime.day)
    print (mytime.hour)