Home > Blockchain >  Transform Unix time stamp to a readable date time format
Transform Unix time stamp to a readable date time format

Time:04-21

Im new to python and I would like to make my time column a readable date time format. My end goal is too pull yearly data for a crypto and only using Daily closes.

Kinda having trouble defining my Time column properly to transform it.

import pandas as pd

import requests
import json
import datetime
url = 'https://api.kucoin.com'


kline = requests.get(url   '/api/v1/market/candles?                type=1min&symbol=BTC-USDT&startAt=1566703297&endAt=1566789757')
kline = kline.json()
kline = pd.DataFrame(kline['data'])
kline = kline.rename({0:"Time",1:"Open",
            2:"Close",3:"High",4:"Low",5:"Amount",6:"Volume"},   axis='columns')

kline.set_index('Time', inplace=True)
kline.head()

CodePudding user response:

You can use pd.to_datetime with unit argument

kline = kline.rename({0:"Time",1:"Open",
                      2:"Close",3:"High",
                      4:"Low",5:"Amount",6:"Volume"}, axis='columns')

kline['Time'] = pd.to_datetime(kline['Time'], unit='s')
# ^^^  Addition here

kline.set_index('Time', inplace=True)
  • Related