Pandas

Pandas中怎么提取时间序列的特征

小亿
87
2024-05-11 18:16:53
栏目: 编程语言

要提取时间序列的特征,可以使用Pandas中的dt属性。以下是一些常用的时间序列特征提取方法:

  1. 提取年、月、日、小时、分钟、秒等时间单位:
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
  1. 提取星期几:
df['weekday'] = df['timestamp'].dt.dayofweek
  1. 提取季节:
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)
  1. 提取是否是工作日:
df['is_weekday'] = df['weekday'].apply(lambda x: 1 if x < 5 else 0)

通过以上方法,可以方便地从时间序列中提取各种特征进行分析和建模。

0
看了该问题的人还看了