从日期python提取年/月到新列

时间:2018-08-20 08:05:49

标签: python pandas datetime dataframe

我有一个对象类型为日期的列

> df['created_at_first']

那就是结果

created_at_first
2018-07-01 02:08:06
2018-06-05 01:39:30
2018-05-16 21:18:48

我想为年,月,日,小时创建新列。 所以我得到这样的东西:

year  month  day  hour 
2018   07    01   02
2018   06    05   01
2018   05    16   21

我该如何管理?

4 个答案:

答案 0 :(得分:5)

也许:

df['created_at_first'] = pd.to_datetime(df['created_at_first'])
df['year'] = df['created_at_first'].dt.year
df['month'] = df['created_at_first'].dt.month
df['day'] = df['created_at_first'].dt.day
df['hour'] = df['created_at_first'].dt.hour

答案 1 :(得分:2)

一种灵活的方法是将operator.attrgetterpd.concat一起使用。通过这种方法,您可以指定任意属性列表,然后通过pd.Series.dt访问器提取这些属性。

fields = ['year', 'month', 'day', 'hour']

res = pd.concat(attrgetter(*fields)(df['dates'].dt), axis=1, keys=fields)

print(res)

   year  month  day  hour
0  2018      7    1     2
1  2018      6    5     1
2  2018      5   16    21

设置

import pandas as pd
from operator import attrgetter

df = pd.DataFrame({'dates': ['2018-07-01 02:08:06',
                             '2018-06-05 01:39:30',
                             '2018-05-16 21:18:48']})

df['dates'] = pd.to_datetime(df['dates'])

答案 2 :(得分:1)

DatetimeIndex将有助于获得所需的结果

created_at_first=["2018-07-01 02:08:06","2018-06-05 01:39:30","2018-05-16 21:18:48"]
import pandas as pd 
df=pd.DataFrame({'ColumnName':created_at_first})
df['year'] = pd.DatetimeIndex(df['ColumnName']).year
df['month'] = pd.DatetimeIndex(df['ColumnName']).month
df['day'] = pd.DatetimeIndex(df['ColumnName']).day
df['hour'] = pd.DatetimeIndex(df['ColumnName']).hour

官方文件:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DatetimeIndex.html

输出:

            columnName  year  month  day  hour
0  2018-07-01 02:08:06  2018      7    1     2
1  2018-06-05 01:39:30  2018      6    5     1
2  2018-05-16 21:18:48  2018      5   16    21

答案 3 :(得分:1)

您可以尝试使用strftime,然后按照'-'函数中给出的方法strftime('%Y-%m-%d-%H')进行拆分。代码:

created_at_first=["2018-07-01 02:08:06","2018-06-05 01:39:30","2018-05-16 21:18:48"]
df=pd.DataFrame({'ColumnName':created_at_first})
df['ColumnName']= pd.to_datetime(df['ColumnName'])

df2 = pd.DataFrame(df.ColumnName.dt.strftime('%Y-%m-%d-%H').str.split('-').tolist(),
                   columns=['Year','Month','Day','Hour'],dtype=int)
df2
    Year Month Day Hour
0   2018    07  01   02
1   2018    06  05   01
2   2018    05  16   21

如果要在单个数据框中使用所有列,请在pd.concat()axis=1中使用pd.concat((df,df2),axis=1) ColumnName Year Month Day Hour 0 2018-07-01 02:08:06 2018 07 01 02 1 2018-06-05 01:39:30 2018 06 05 01 2 2018-05-16 21:18:48 2018 05 16 21

public class ValidateActionParametersAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext context)
    {
        var descriptor = context.ActionDescriptor as ControllerActionDescriptor;

        if (descriptor != null)
        {
            var parameters = descriptor.MethodInfo.GetParameters();

            foreach (var parameter in parameters)
            {
                var argument = context.ActionArguments[parameter.Name];

                EvaluateValidationAttributes(parameter, argument, context.ModelState);
            }
        }

        base.OnActionExecuting(context);
    }

    private void EvaluateValidationAttributes(ParameterInfo parameter, object argument, ModelStateDictionary modelState)
    {
        var validationAttributes = parameter.CustomAttributes;

        foreach (var attributeData in validationAttributes)
        {
            var attributeInstance = CustomAttributeExtensions.GetCustomAttribute(parameter, attributeData.AttributeType);

            var validationAttribute = attributeInstance as ValidationAttribute;

            if (validationAttribute != null)
            {
                var isValid = validationAttribute.IsValid(argument);
                if (!isValid)
                {
                    modelState.AddModelError(parameter.Name, validationAttribute.FormatErrorMessage(parameter.Name));
                }
            }
        }
    }
}