我正在使用Python,我在“2013-06-24 17:04:28”类型的表中将日期存储为字符串,我想做一些事情,例如减去和平均它们。
我不在乎时间。
将字符串转换为适当的格式是什么?我该怎么做?
提前感谢您的帮助。
最大
答案 0 :(得分:4)
pandas拥有自己的Timestamp对象。您可以使用to_datetime
函数将字符串或整个列(系列)转换为时间戳:
pd.to_datetime(df['date_col'])
请参阅"converting to Timestamps" section of the docs。
注意:在DataFrame中阅读时,您可以转换为时间戳,例如使用read_csv
。
适用于您的日期格式:
In [11]: pd.to_datetime('2013-06-24 17:04:28')
Out[11]: Timestamp('2013-06-24 17:04:28', tz=None)
答案 1 :(得分:0)
time.strptime("2013-06-24 17:04:28", "%Y-%m-%d %H:%M:%S")
然后您可以使用python's time module