【问题标题】:How to get from array with Timestamps - years, months, days in python?如何从带有时间戳的数组中获取-python中的年、月、日?
【发布时间】:2023-03-14 11:52:02
【问题描述】:

如何从带有时间戳的数组中获取 - 年、月、日? 我有索引为时间戳的 DataFrame,我试试这个

for i in data_frame.index:
    print(datetime.fromtimestamp(i).isoformat())

但是我收到了这个错误:

print(datetime.fromtimestamp(i).isoformat()) ===>
===> TypeError: an integer is required (got type Timestamp)

【问题讨论】:

    标签: python datetime time timestamp


    【解决方案1】:

    首先使用df['date'] = pd.to_datetime(df['timestap']) 转换为正确的格式 然后为年、月和日创建新列

    df['year'] = df['date'].dt.year
    df['month'] = df['date'].dt.month
    df['day'] = df['date'].dt.day
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-17
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      • 2011-02-23
      • 2016-05-20
      • 1970-01-01
      • 2020-05-20
      相关资源
      最近更新 更多