简体   繁体   English

类型错误:strptime() 参数 1 必须是 str,而不是句点

[英]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):如果您通过timeseriesdt 访问器获取年份,则会得到整数(而不是“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.

相关问题 `TypeError:strptime()参数0必须是str,而不是 <class 'bytes'> ` - `TypeError: strptime() argument 0 must be str, not <class 'bytes'>` 类型错误:strptime() 参数 1 必须是 str,而不是 None - TypeError : strptime() argument 1 must be str, not None TypeError: strptime() 参数 1 必须是 str,而不是 Series - TypeError: strptime() argument 1 must be str, not Series TypeError:strptime()参数1必须是str,而不是list - TypeError: strptime() argument 1 must be str, not list Datetime(TypeError:strptime() 参数 1 必须是 str,而不是 Timestamp) - Datetime (TypeError: strptime() argument 1 must be str, not Timestamp) datetime.strptime:TypeError:strptime()参数1必须是str,而不是Series - datetime.strptime: TypeError: strptime() argument 1 must be str, not Series 将loadtxt列转换为工作日:TypeError:strptime()参数1必须为str,而不是字节 - Converting a loadtxt column to a weekday: TypeError: strptime() argument 1 must be str, not bytes TypeError:strptime()参数1必须是str,而不是datetime.date Python - TypeError: strptime() argument 1 must be str, not datetime.date Python TypeError: strptime() argument 1 must be str, not float 不明白为什么会这样 - TypeError: strptime() argument 1 must be str, not float do not understand why this is happening strptime() 参数 1 必须是 str,而不是 Series - strptime() argument 1 must be str, not Series
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM