要提取時間序列的特征,可以使用Pandas中的dt屬性。以下是一些常用的時間序列特征提取方法:
df['year'] = df['timestamp'].dt.year
df['month'] = df['timestamp'].dt.month
df['day'] = df['timestamp'].dt.day
df['hour'] = df['timestamp'].dt.hour
df['minute'] = df['timestamp'].dt.minute
df['second'] = df['timestamp'].dt.second
df['weekday'] = df['timestamp'].dt.dayofweek
def get_season(month):
if month in [3, 4, 5]:
return 'Spring'
elif month in [6, 7, 8]:
return 'Summer'
elif month in [9, 10, 11]:
return 'Autumn'
else:
return 'Winter'
df['season'] = df['month'].apply(get_season)
df['is_weekday'] = df['weekday'].apply(lambda x: 1 if x < 5 else 0)
通過以上方法,可以方便地從時間序列中提取各種特征進行分析和建模。