Python Pandas to_datetime 删除下划线
Python Pandas to_datetime removes underscore
在我的申请中时间是这样存储的:
05-04-2017_12:28:26.758369
所以我写道:
df[timestamp_lbl] = pd.to_datetime(df[timestamp_lbl],
format="%d-%m-%Y_%H:%M:%S.%f")
当我打印 df[timestamp_lbl] 时,我得到:
05-04-2017 12:28:26.758369
现在缺少下划线。我究竟做错了什么?谢谢!
注意:我需要该列具有 datetime64 而不是字符串。
format
参数仅告诉 to_datetime
在解析日期字符串时期望使用哪种语法。但是一旦它被转换为 Pandas Datetime 对象,它的打印表示总是相同的。如果你需要在某个时候打印出它的值,你可以使用 .strftime()
方法而不改变它的 dtype
:
ts = "05-04-2017_12:28:26.758369"
tsobj = pd.to_datetime(ts,format="%d-%m-%Y_%H:%M:%S.%f")
print(tsobj.strftime("%d-%m-%Y_%H:%M:%S.%f"))
'05-04-2017_12:28:26.758369'
在我的申请中时间是这样存储的:
05-04-2017_12:28:26.758369
所以我写道:
df[timestamp_lbl] = pd.to_datetime(df[timestamp_lbl],
format="%d-%m-%Y_%H:%M:%S.%f")
当我打印 df[timestamp_lbl] 时,我得到:
05-04-2017 12:28:26.758369
现在缺少下划线。我究竟做错了什么?谢谢!
注意:我需要该列具有 datetime64 而不是字符串。
format
参数仅告诉 to_datetime
在解析日期字符串时期望使用哪种语法。但是一旦它被转换为 Pandas Datetime 对象,它的打印表示总是相同的。如果你需要在某个时候打印出它的值,你可以使用 .strftime()
方法而不改变它的 dtype
:
ts = "05-04-2017_12:28:26.758369"
tsobj = pd.to_datetime(ts,format="%d-%m-%Y_%H:%M:%S.%f")
print(tsobj.strftime("%d-%m-%Y_%H:%M:%S.%f"))
'05-04-2017_12:28:26.758369'