TypeError: cannot astype a datetimelike from [datetime64[ns, UTC]] to [int32]

TypeError: cannot astype a datetimelike from [datetime64[ns, UTC]] to [int32]

我正在尝试 运行 此代码并收到此错误。m 数据如下所示

TypeError: 无法将 datetimelike 从 [datetime64[ns, UTC]] 键入到 [int32]

    import numpy as np
    import pandas as pd
    import seaborn as sns
    from sklearn.ensemble import IsolationForest
    data = pd.read_csv("data.csv", parse_dates=['timestamp'])
    data['timestamp'] = data['timestamp'].astype('int')
    model=IsolationForest(n_estimators=50, max_samples='auto', contamination=float(0.1),max_features=1.0)
    model.fit(data[['timestamp','a','b','c','d','e','f','g',    'h','i','j','k']])
    data['scores']=model.decision_function(data[['timestamp','a','b','c','d','e','f','g',   'h','i','j','k']])
    data['anomaly']=model.predict(data[['timestamp','a','b','c','d','e','f','g',    'h','i','j','k']])

table 为空

试试这个:

data['timestamp'] = data['timestamp'].astype('datetime64[s]').astype('int')

它先转换为秒,然后再转换为整数。