def get_label_features_of_date(label_field): # 源数据 data = label_field.copy() data['Coupon_id'] = data['Coupon_id'].astype(int) # 将Coupon_id列中float类型的元素转换为int类型 data['Date_received'] = data['Date_received'].astype(int) # 将Date_received列中float类型的元素转换为int类型 # 返回的特征数据集 features = data.copy() features['Week_received'] = features['Date_received'].apply(lambda x: x.isoweekday()) # 星期几,星期一为1,星期天为7 features['is_weekend_reveived'] = features['Week_received'].apply(lambda x: 1 if x in [6, 7] else 0) # 判断领券日是否为休息日 features['Month_received'] = features['Date_received'].apply(lambda x: x.month) # 月份 features = pd.concat([features, pd.get_dummies(features['Week_received'], prefix='Week_received')], axis=1) # one-hot离散星期几 features.reset_index(drop=True, inplace=True) # 重置index # 返回 return features出现报错 AttributeError: 'int' object has no attribute 'isoweekday' 如何解决
时间: 2024-03-22 19:37:16 浏览: 59
这个报错是因为代码中的变量 x 的类型是 int,而 int 类型没有 isoweekday() 方法。通常出现这种错误是因为数据类型转换出现问题。建议检查数据集中 Date_received 列中是否存在 float 类型的元素,如果有需要将其转换为 int 类型。你可以使用 .astype(int) 方法将该列中的所有元素转换为 int 类型,示例代码如下:
```
data['Date_received'] = data['Date_received'].astype(int)
```
这样就可以解决该报错了。
阅读全文