python构造特征之处理日期

pandas对日期有专门的api,提供了丰富的处理

由于我想增加dataframe里属性,想从日期入手

https://pandas.pydata.org/pandas-docs/stable/reference/series.html

 代码:

df['dayofmonth'] = df['日期'].dt.day.astype(int)
df['dayofyear'] = df['日期'].dt.dayofyear.astype(int)
df['dayofweek'] = df['日期'].dt.dayofweek.astype(int)
df['month'] = df['日期'].dt.month.astype(int)
df['year'] = df['日期'].dt.year.astype(int)
df['weekofyear'] = df['日期'].dt.weekofyear.astype(int)
df['is_month_start'] = (df['日期'].dt.is_month_start).astype(int)
df['is_month_end'] = (df['日期'].dt.is_month_end).astype(int)
df['quarter'] = (df['日期'].dt.quarter).astype(int)
df['is_year_start'] = (df['日期'].dt.is_year_start).astype(int)
df['is_year_end'] = (df['日期'].dt.is_year_end).astype(int)
df['is_quarter_start'] = (df['日期'].dt.is_quarter_start).astype(int)
df['is_quarter_end'] = (df['日期'].dt.is_quarter_end).astype(int)


    这样我们就可以增加多个属性,这样我们可以从单纯的日期上面获取更加丰富的属性,上面那个链接上有全部的属性,我粘一下:

Series.dt.dateReturns numpy array of python datetime.date objects (namely, the date part of Timestamps without timezone information).
Series.dt.timeReturns numpy array of datetime.time.
Series.dt.timetzReturns numpy array of datetime.time also containing timezone information.
Series.dt.yearThe year of the datetime.
Series.dt.monthThe month as January=1, December=12.
Series.dt.dayThe days of the datetime.
Series.dt.hourThe hours of the datetime.
Series.dt.minuteThe minutes of the datetime.
Series.dt.secondThe seconds of the datetime.
Series.dt.microsecondThe microseconds of the datetime.
Series.dt.nanosecondThe nanoseconds of the datetime.
Series.dt.weekThe week ordinal of the year.
Series.dt.weekofyearThe week ordinal of the year.
Series.dt.dayofweekThe day of the week with Monday=0, Sunday=6.
Series.dt.weekdayThe day of the week with Monday=0, Sunday=6.
Series.dt.dayofyearThe ordinal day of the year.
Series.dt.quarterThe quarter of the date.
Series.dt.is_month_startIndicates whether the date is the first day of the month.
Series.dt.is_month_endIndicates whether the date is the last day of the month.
Series.dt.is_quarter_startIndicator for whether the date is the first day of a quarter.
Series.dt.is_quarter_endIndicator for whether the date is the last day of a quarter.
Series.dt.is_year_startIndicate whether the date is the first day of a year.
Series.dt.is_year_endIndicate whether the date is the last day of the year.
Series.dt.is_leap_yearBoolean indicator if the date belongs to a leap year.
Series.dt.daysinmonthThe number of days in the month.
Series.dt.days_in_monthThe number of days in the month.
Series.dt.tzReturn timezone, if any.
Series.dt.freq
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值