I try to extract hour from the timestamp : I have a dataframe called 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
This is my code :
from datetime import datetime,timedelta
mytime = datetime.strptime(df_no_missing["TIMESTAMP"],"%d/%m/%Y %H:%M")
print (mytime.day)
print (mytime.hour)
I get this error :
in () 1 df_no_missing.info() 2 from datetime import datetime,timedelta ----> 3 mytime = datetime.strptime(df_no_missing["TIMESTAMP"],"%d/%m/%Y %H:%M") 4 print (mytime.day) 5 print (mytime.hour)
TypeError: strptime() argument 1 must be str, not Series
df_no_missing["TIMESTAMP"]returns the whole column, you can't use it in this context. What do you expect the value ofmytimeto be? - DeepSpacedtypeisobjectsuggesting it's string, you haven't displayed what the current format is but can you trydf_no_missing["TIMESTAMP"] = pd.to_datetime(df_no_missing["TIMESTAMP"])to convert todatetimeafter which you can dodf_no_missing["TIMESTAMP"].dt.dayanddf_no_missing["TIMESTAMP"].dt.hour- EdChum