python中的datatime.dt.day一系列

1998-10-19这种日期是datatime标准形式
如果数据为1998/10/19或其他可以下面语句变为标准

train_df['label'] = (train_df['repay_date'] - train_df['auditing_date']).dt.days

2.下面这种类型的语句可以对操作后的datatime直接进行取数
pandas.series.dt.days

df['purchase_date'] = pd.to_datetime(df['purchase_date'])
    df['year'] = df['purchase_date'].dt.year
    df['weekofyear'] = df['purchase_date'].dt.weekofyear
    df['month'] = df['purchase_date'].dt.month
    df['dayofweek'] = df['purchase_date'].dt.dayofweek
    df['weekend'] = (df.purchase_date.dt.weekday >=5).astype(int)
    df['hour'] = df['purchase_date'].dt.hour

比如x=‘1998-10-19’
x.dt.days=19
x.dt.year=1998
依次类推

你可能感兴趣的:(python)