[英]converting float number into datetime format
I have a file that contains DateTime in float format我有一个包含浮点格式的 DateTime 的文件
example 14052020175648.000000
I want to convert this into 14-05-2020
and leave the timestamp value.示例14052020175648.000000
我想将其转换为14-05-2020
并保留时间戳值。
input ==> 14052020175648.000000
输入 ==> 14052020175648.000000
expected output ==> 14-05-2020
预计 output ==> 14-05-2020
Use pd.to_datetime
:使用pd.to_datetime
:
df = pd.DataFrame({'Timestamp': ['14052020175648.000000']})
df['Date'] = pd.to_datetime(df['Timestamp'].astype(str).str[:8], format='%d%m%Y')
print(df)
# Output:
Timestamp Date
0 14052020175648.000000 2020-05-14
I used astype(str)
in case where Timestamp
is a float number and not a string, so it's not mandatory if your column already contains strings.在Timestamp
是浮点数而不是字符串的情况下,我使用astype(str)
,因此如果您的列已经包含字符串,则它不是强制性的。
This can solve your problem这可以解决你的问题
from datetime import datetime
string = "14052020175648.000000"
yourDate = datetime.strptime(string[:8], '%d%m%Y').strftime("%d-%m-%Y")
print(yourDate)
Output: Output:
14-05-2020
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.