【问题标题】:datetime OSError: [Errno 22] Invalid argument日期时间 OSError:[Errno 22] 无效参数
【发布时间】:2018-12-18 09:59:44
【问题描述】:
import pandas as pd
import datetime

def open_csv(path):
    try:
        df = pd.read_csv(path)
        return df
    except FileNotFoundError:
        print("ERR: FileNotFoundError", path)


data = open_csv("historical/BNBBTC")
for d in data["Open_time"]:
    print(d)
    print(type(d))
    print(datetime.datetime.fromtimestamp(d).strftime('%Y-%m-%d %H:%M:%S'))

错误: 1514764800000 Traceback (most recent call last): <class 'int'> File "D:/bot/add_data.py", line 16, in <module> print(datetime.datetime.fromtimestamp(d).strftime('%Y-%m-%d %H:%M:%S')) OSError: [Errno 22] Invalid argument

我不明白是什么问题?如果 b = int ("1514764800000") 那么一切正常!

【问题讨论】:

  • 你不需要额外的代码,只需将read_csv修改为df = pd.read_csv(path, parse_dates='Open_time')

标签: python pandas datetime eoserror


【解决方案1】:

有点晚了,但你的问题是时间戳以毫秒为单位,像这样转换它:

        milliSeconds = timestamp % 1000
        timestamp = timestamp / 1000
        converted = datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S') + f".{milliSeconds}"

【讨论】:

    【解决方案2】:

    import time 并将datetime.datetime.fromtimestamp(d).strftime('%Y-%m-%d %H:%M:%S') 替换为time.strftime('%Y-%m-%d %H:%M:%S')

    【讨论】:

      【解决方案3】:
      def epoch_to_date(epoch_time):
          time_stamp = epoch_time / 1000
          date_time = datetime.datetime.fromtimestamp(time_stamp).strftime('%Y-%m-%d %H:%M:%S')
          print(date_time)
          return date_time
      
      
      epoch_time = epoch_to_date(epoch_time)
      

      【讨论】:

        猜你喜欢
        • 2021-10-13
        • 2021-11-01
        • 2019-04-27
        • 1970-01-01
        • 2020-01-26
        • 2018-10-05
        • 1970-01-01
        • 1970-01-01
        • 2017-04-10
        相关资源
        最近更新 更多