要提取时间序列的特征,可以使用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)
通过以上方法,可以方便地从时间序列中提取各种特征进行分析和建模。