简体   繁体   中英

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

I can not understand what is the problem? If b = int ("1514764800000") then everything works!

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

I can not understand what is the problem? If b = int ("1514764800000") then everything works!

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

I can not understand what is the problem? If b = int ("1514764800000") then everything works!

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.

This is because converted date is out of range and online python editor show the correct error: 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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