在0s 的数据框中填充所有日期时间字符串的最佳方法是什么,例如,1/2/2016 8:42
变为01/02/2016 08:42
?
字符串通常采用d/m/yyyy h:mm
格式,但在某些情况下可能是“MAX”或“MIN”。
我需要填充零,以便对时间戳进行排序是正确的。
尽管将字符串转换为datetime
对象是理想的,但由于代码的其他部分需要将其作为字符串,因此无法完成。
我目前的实施使用strptime
和strftime
:
bad = '1/2/2016 8:42'
good = '01/02/2016 08:42'
df = pandas.DataFrame({'Timestamp':[bad,good], 'Foo':[42,117]})
print(df)
try:
# Convert timestamp to have leading zeros to allow for correct sorting, so e.g. 8:42 converts to 08:42
df['Timestamp'] = df['Timestamp'].apply(lambda x: datetime.strptime(x, "%d/%m/%Y %H:%M").strftime("%d/%m/%Y %H:%M"))
except ValueError: # Timestamp is MAX/MIN
pass
print(df)
哪个输出:
Other Timestamp
0 27 1/2/2016 8:42
1 59 01/02/2016 08:42
Other Timestamp
0 27 01/02/2016 08:42
1 59 01/02/2016 08:42
答案 0 :(得分:0)
修改强>
使用Pandas系列,此代码有效:
from datetime import datetime
import pandas as pd
bad = '1/2/2016 8:42'
good = '01/02/2016 08:42'
data = {}
data['Timestamp'] = bad
data = pd.Series(data)
print(data)
try:
# Convert timestamp to have leading zeros to allow for correct sorting
# so e.g. 8:42 converts to 08:42
data['Timestamp'] = datetime.strptime(data['Timestamp'], '%d/%m/%Y %H:%M').strftime('%d/%m/%Y %H:%M')
except ValueError:
# Timestamp is MAX/MIN
pass
print(data)
assert good == data['Timestamp']
这将打印:
Timestamp 1/2/2016 8:42
dtype: object
Timestamp 01/02/2016 08:42
dtype: object
你可以摆脱apply
功能,你将以AttributeError: 'str' object has no attribute 'apply'
结束:
try:
data['Timestamp'] = datetime.strptime(data['Timestamp'], '%d/%m/%Y %H:%M').strftime('%d/%m/%Y %H:%M')
except ValueError:
pass