简体   繁体   English

datetime OSError: [Errno 22] 参数无效

[英]datetime OSError: [Errno 22] Invalid argument

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'))

error: 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错误: 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

I can not understand what is the problem?我不明白这是什么问题? If b = int ("1514764800000") then everything works!如果 b = int ("1514764800000") 那么一切正常!

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'))

error: 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错误: 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

I can not understand what is the problem?我不明白是什么问题? If b = int ("1514764800000") then everything works!如果 b = int ("1514764800000") 那么一切正常!

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'))

error: 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错误: 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

I can not understand what is the problem?我不明白是什么问题? If b = int ("1514764800000") then everything works!如果 b = int ("1514764800000") 那么一切正常!

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)


epoch_time = epoch_to_date(epoch_time)

I got same error OSError: [Errno 22] Invalid argument while converting timestamp to date format but my use case is different.我得到了同样的错误OSError: [Errno 22] Invalid argument while conversion timestamp to date format 但我的用例不同。

This is because converted date is out of range and online python editor show the correct error: ValueError: year 54552 is out of range .这是因为转换日期超出范围,在线 python 编辑器显示正确错误: ValueError: year 54552 is out of range

By converting timestamp to string and removing last three digits work for me.通过将时间戳转换为字符串并删除最后三位数字对我有用。

data = 1659347766123 
data = int(str(data)[:-3])
print(data)
timeStamp = datetime.fromtimestamp(data)

print(timeStamp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM