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.date | Returns numpy array of python datetime.date objects (namely, the date part of Timestamps without timezone information). |
Series.dt.time | Returns numpy array of datetime.time. |
Series.dt.timetz | Returns numpy array of datetime.time also containing timezone information. |
Series.dt.year | The year of the datetime. |
Series.dt.month | The month as January=1, December=12. |
Series.dt.day | The days of the datetime. |
Series.dt.hour | The hours of the datetime. |
Series.dt.minute | The minutes of the datetime. |
Series.dt.second | The seconds of the datetime. |
Series.dt.microsecond | The microseconds of the datetime. |
Series.dt.nanosecond | The nanoseconds of the datetime. |
Series.dt.week | The week ordinal of the year. |
Series.dt.weekofyear | The week ordinal of the year. |
Series.dt.dayofweek | The day of the week with Monday=0, Sunday=6. |
Series.dt.weekday | The day of the week with Monday=0, Sunday=6. |
Series.dt.dayofyear | The ordinal day of the year. |
Series.dt.quarter | The quarter of the date. |
Series.dt.is_month_start | Indicates whether the date is the first day of the month. |
Series.dt.is_month_end | Indicates whether the date is the last day of the month. |
Series.dt.is_quarter_start | Indicator for whether the date is the first day of a quarter. |
Series.dt.is_quarter_end | Indicator for whether the date is the last day of a quarter. |
Series.dt.is_year_start | Indicate whether the date is the first day of a year. |
Series.dt.is_year_end | Indicate whether the date is the last day of the year. |
Series.dt.is_leap_year | Boolean indicator if the date belongs to a leap year. |
Series.dt.daysinmonth | The number of days in the month. |
Series.dt.days_in_month | The number of days in the month. |
Series.dt.tz | Return timezone, if any. |
Series.dt.freq |