英文:
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
df = pd.DataFrame({'Alert_Time': ['2020-12-22 00:00:00']})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论