[英]TypeError: strptime() argument 1 must be str, not Period
i have this data frame.我有这个数据框。
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)
i have converted the timestamp into real dates and fetched years.我已将时间戳转换为实际日期并获取年份。 i want two take difference between first two rows of timestamp.
我想要两个时间戳的前两行之间的差异。 For example (abs (2014-2000) = 4)
例如 (abs (2014-2000) = 4)
If you take the year through the dt
acessor of timeseries , you get integers (instead of "Period" objects):如果您通过timeseries的
dt
访问器获取年份,则会得到整数(而不是“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
As you see, I commented the two lines were you were trying to recast the years into datetime
objects.如您所见,我评论了这两行是您试图将年份重新转换为
datetime
对象。 Was there a reason for this?这有什么原因吗? From your question, I assumed you wanted the difference in number of years.
根据您的问题,我假设您想要年数的差异。 If you wanted the exact number of days between the timestamps then this should do: (no need to cast and recast):
如果您想要时间戳之间的确切天数,则应该这样做:(无需转换和重新转换):
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.