分组后无法将对象转换为日期

时间:2019-02-02 01:53:19

标签: python-3.x pandas datetime

几天前,我在处理其他数据集时成功进行了转换。但是,我无法对当前数据集应用相同的技术。该集合如下所示:

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 个答案:

答案 0 :(得分:2)

您可以只使用.rename()重命名列

生成一些数据(与OP相同的格式)

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.7pandas (0.23.4)完成的。