【问题标题】:Incorrect Parsing of dates in pandas dataframe熊猫数据框中的日期解析不正确
【发布时间】:2021-03-18 16:44:48
【问题描述】:

您好,我正在尝试将我的数据框日期更改为可用于提取有用信息的格式。 该数据集带有 'week' 特征,其形式为 DD/MM/YY,如下所示:

In [128]: df_train[['week', 'units_sold']]
Out[128]: 
            week  units_sold
0       17/01/11          20
1       17/01/11          28
2       17/01/11          19
3       17/01/11          44
4       17/01/11          52

我已将日期更改如下:

df_train['new_date'] = pd.to_datetime(df_train['week'])

        new_date  units_sold
0     2011-01-17        20.0
1     2011-01-17        28.0
2     2011-01-17        19.0
3     2011-01-17        44.0
4     2011-01-17        52.0

使用我创建的“new_date”功能,我做了以下一些信息提取:

df_train['weekday'] = df_train['new_date'].dt.weekofyear             #week day of the year
df_train['QTR'] = df_train['new_date'].apply(lambda x: x.quarter)    #current quarter of the year
df_train['month'] = df_train['new_date'].apply(lambda x: x.month)    #current month
df_train['year'] = df_train['new_date'].dt.year                      #current year

但是,在查看我的数据时,我遇到了一些错误。例如,我的数据集中的某个日期是 07/02/11,它应该转换为 2 个月。除了我的解析显示月份是 7,我知道这是不正确的:参见条目 3483

Out[127]: 
          week  month
18    17/01/11      1
1173  24/01/11      1
2328  31/01/11      1
3483  07/02/11      7
4638  14/02/11      2

谁能告诉我哪里出错了? 任何帮助表示赞赏!

【问题讨论】:

    标签: python pandas datetime


    【解决方案1】:

    使用dayfirst=True参数:

    df_train['new_date'] = pd.to_datetime(df_train['week'], dayfirst=True)
    

    然后是.dt 访问器以提高性能,因为在应用中是底层循环:

    df_train['weekday'] = df_train['new_date'].dt.weekofyear #week day of the year
    df_train['QTR'] = df_train['new_date'].dt.quarter    #current quarter of the year
    df_train['month'] = df_train['new_date'].dt.month    #current month
    df_train['year'] = df_train['new_date'].dt.year    
    

    【讨论】:

      猜你喜欢
      • 2013-01-08
      • 2022-01-10
      • 2015-07-25
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-29
      • 2019-08-01
      相关资源
      最近更新 更多