【问题标题】:TypeError: strptime() argument 1 must be str, not Period类型错误:strptime() 参数 1 必须是 str,而不是 Period
【发布时间】:2019-12-03 21:08:35
【问题描述】:

我有这个数据框。

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)

我已将时间戳转换为实际日期并获取年份。 我想要两个取前两行时间戳之间的差异。 例如 (abs (2014-2000) = 4)

【问题讨论】:

    标签: python pandas


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-21
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 2018-10-11
      相关资源
      最近更新 更多