TypeError:strptime()参数1必须是str,而不是Period

时间:2019-12-03 21:08:35

标签: python pandas

我有这个数据框。

import pandas as pd
from datetime import datetime
df = pd.DataFrame({'id': [11,22,33,44,55], 
                   'name': ['A','B','C','D','E'], 
                   'timestamp': [1407617838,965150022,1158531592,1500701864,965149631]})
df
   id name timestamp
0  11    A      2014
1  22    B      2000
2  33    C      2006
3  44    D      2017
4  55    E      2000
df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
df['timestamp'] = df['timestamp'].dt.to_period('Y')
y1 = df['timestamp'].iloc[0]
y2 = df['timestamp'].iloc[1]
d1 = datetime.strptime(y1, "%Y")
d2 = datetime.strptime(y2, "%Y")
diff = abs((d2 - d1).days)
print(diff)

我已将时间戳转换为实际日期和获取的年份。 我想在时间戳的前两行之间取两个。 例如(绝对(2014-2000)= 4)

1 个答案:

答案 0 :(得分:1)

如果您通过dt acessor of timeseries来获取年份,则会得到整数(而不是“ Period”对象):

df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
df['timestamp'] = df['timestamp'].dt.year
y1 = df['timestamp'].iloc[0]
y2 = df['timestamp'].iloc[1]
# d1 = datetime.strptime(y1, "%Y") <- No need to recast to datetime!
# d2 = datetime.strptime(y2, "%Y")
diff = abs((y2 - y1))
print(diff)
>>> 14

如您所见,当您尝试将年份重铸为datetime对象时,我评论了这两行。有这个原因吗?根据您的问题,我认为您想要年数的差异。如果您希望获得时间戳之间的确切天数,则应该这样做:(无需投射和重铸):

df['timestamp'] = pd.to_datetime(df['timestamp'], unit='s')
y1 = df['timestamp'].iloc[0]
y2 = df['timestamp'].iloc[1]
diff = abs((y2 - y1).days)
print(diff)
>>> 5122