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!

Upvotes: 0

Views: 5446

Answers (3)

grey
grey

Reputation: 357

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)

Upvotes: 1

Leo Ma
Leo Ma

Reputation: 1151

kind of late, but the problem in your case is that timestamp is in milliSeconds, convert it like this:

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

Upvotes: 1

kn_pro
kn_pro

Reputation: 67

import time and replace datetime.datetime.fromtimestamp(d).strftime('%Y-%m-%d %H:%M:%S') with time.strftime('%Y-%m-%d %H:%M:%S')

Upvotes: 0

Related Questions