代码之家  ›  专栏  ›  技术社区  ›  AK88

无法将对象转换为groupby之后的日期

  •  1
  • AK88  · 技术社区  · 6 年前

    几天前,我在使用不同的数据集时成功地进行了转换。但是,我不能将相同的技术应用于我当前的数据集。该集看起来如下所示:

    totalHist.columns.values[[0, 1]] = ['Datez', 'Volumez']
    totalHist.head()
    
    Datez   Volumez
    0   2016-09-19  6.300000e+07
    1   2016-09-20  3.382694e+07
    2   2016-09-26  4.000000e+05
    3   2016-09-27  4.900000e+09
    4   2016-09-28  5.324995e+08
    
    
    totalHist.dtypes
    
    Datez       object
    Volumez    float64
    dtype: object
    

    这是用来做的把戏:

    totalHist['Datez'] = pd.to_datetime(totalHist['Datez'], format='%d-%m-%Y')
    totalHist.dtypes
    

    现在这给了我:

    KeyError: 'Datez'
    During handling of the above exception, another exception occurred:
    

    totalHist = df.groupby('Date', as_index = False).agg({"Trading_Value": "sum"})
    totalHist.head()
    
    totalHist.columns.values[[0, 1]] = ['Datez', 'Volumez']
    totalHist.head()
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   edesz    6 年前

    你可以用 .rename() 重命名列的步骤

    d = ['1/1/2018','1/2/2018','1/3/2018',
         '1/3/2018','1/4/2018','1/2/2018','1/1/2018','1/5/2018']
    df = pd.DataFrame(d, columns=['Date'])
    df['Trading_Value'] = [1000,1005,1001,1001,1002,1009,1010,1002]
    print(df)
    
           Date  Trading_Value
    0  1/1/2018           1000
    1  1/2/2018           1005
    2  1/3/2018           1001
    3  1/3/2018           1001
    4  1/4/2018           1002
    5  1/2/2018           1009
    6  1/1/2018           1010
    7  1/5/2018           1002
    
    

    分组

    totalHist = df.groupby('Date', as_index = False).agg({"Trading_Value": "sum"})
    print(totalHist.head())
    
           Date  Trading_Value
    0  1/1/2018           2010
    1  1/2/2018           2014
    2  1/3/2018           2002
    3  1/4/2018           1002
    4  1/5/2018           1002
    

    totalHist.rename(columns={'Date':'Datez','totalHist':'Volumez'}, inplace=True)
    print(totalHist)
    
          Datez  Trading_Value
    0  1/1/2018           2010
    1  1/2/2018           2014
    2  1/3/2018           2002
    3  1/4/2018           1002
    4  1/5/2018           1002
    

    最后,转换为 datetime

    totalHist['Datez'] = pd.to_datetime(totalHist['Datez'])
    print(totalHist.dtypes)
    
    Datez            datetime64[ns]
    Trading_Value             int64
    dtype: object
    

    这件事是用 python --version = 3.6.7 pandas (0.23.4) .