我试图从时间戳中提取小时:我有一个名为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)
我得到这个错误:
在()1 df_no_missing.info()2从datetime导入datetime,timedelta---->3 mytime=datetime.strptime(df_no_missing["TIMESTAMP"],"%d/%m/%Y%H:%m")4次打印(mytime.day)5打印(mytime.h)
TypeError:strptime()参数1必须是str,而不是Series
您可能希望在单个条目上使用datetime.strptime
,而不是在数据帧对象本身上?试试类似的东西:
for data in df_no_missing["TIMESTAMP"]:
mytime = datetime.strptime(data,"%d/%m/%Y %H:%M")
print (mytime.day)
print (mytime.hour)