ValueError: invalid literal for int() with base 10: '2020-12-22 00:00:00' date time to time stamp conversion

huangapple go评论61阅读模式
英文:

ValueError: invalid literal for int() with base 10: '2020-12-22 00:00:00' date time to time stamp conversion

问题

在进行日期和数值数据的聚类时,执行k均值聚类时,日期数据未适应模型,显示以下错误:

k_means = KMeans(n_clusters=2)
k_means.fit(df)

错误信息:

ValueError  Traceback (most recent call last)
    
    <ipython-input-43-caf6280a5928> in <cell line: 2>()
      1 k_means = KMeans(n_clusters=2)
   --> 2 k_means.fit(df)
    
    -> 2070 return np.asarray(self._values, dtype=dtype)
    
      
    
    ValueError: could not convert string to float: '2020-12-22 00:00:00'

因此,我使用将日期转换为时间戳的方式,以便将其适应模型,但在从日期转换为时间戳时出现以下错误:

df["stamp"] = df["Alert_Time"].values.astype(np.int64) // 10 ** 9

错误信息:

ValueError      Traceback (most recent call last)
       
    
    <ipython-input-50-8c3cf615eeda> in <cell line: 1>()
    -> 1 df["stamp"]=df["Alert_Time"].values.astype(np.int64) // 10 ** 9
    
    ValueError: invalid literal for int() with base 10: '2020-12-22'
英文:

I am doing clustering on date and value data. while performing k-means clustering date
data not not fitted in model showing this error

k_means = KMeans(n_clusters=2)
k_means.fit(df) 

Error:

ValueError  Traceback (most recent call last)

    <ipython-input-43-caf6280a5928> in <cell line: 2>()
      1 k_means = KMeans(n_clusters=2)
   --> 2 k_means.fit(df)

    -> 2070 return np.asarray(self._values, dtype=dtype)

  

    ValueError: could not convert string to float: '2020-12-22 00:00:00'

    so i use to convert date into timestamp so that fitted into model 
    but while conversion into timestamp from date showing following error  


        df["stamp"] = df["Alert_Time"].values.astype(np.int64) // 10 ** 9


    ValueError      Traceback (most recent call last)
   

    <ipython-input-50-8c3cf615eeda> in <cell line: 1>()
    -> 1 df["stamp"]=df["Alert_Time"].values.astype(np.int64) // 10 ** 9

    ValueError: invalid literal for int() with base 10: '2020-12-22'

答案1

得分: 0

你可以使用 pd.to_datetime

df['stamp'] = pd.to_datetime(df['Alert_Time']).sub(pd.Timestamp(0)).dt.total_seconds()
print(df)

# 输出
            Alert_Time         stamp
0  2020-12-22 00:00:00  1.608595e+09

最小可重现示例

df = pd.DataFrame({'Alert_Time': ['2020-12-22 00:00:00']})
英文:

You can use pd.to_datetime:

df['stamp'] = pd.to_datetime(df['Alert_Time']).sub(pd.Timestamp(0)).dt.total_seconds()
print(df)

# Output
            Alert_Time         stamp
0  2020-12-22 00:00:00  1.608595e+09

Minimal Reproducible Example:

df = pd.DataFrame({'Alert_Time': ['2020-12-22 00:00:00']})

huangapple
  • 本文由 发表于 2023年5月26日 16:34:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76339063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定