pythonpandasdatetimeccxt

convert UNIX timestamp to US/Central using tz_convert


I'm trying to convert my UNIX timestamp to the US/Central timezone timestamp, but i keep getting the UTC output. I don't know what i'm doing wrong in the code.

import ccxt
import pandas as pd
from dateutil import tz

binance = ccxt.binance({
    'enableRateLimit': True,
    'apiKey': 'xxxxxxxxxxxxxxxxxxx',
    'secret': 'xxxxxxxxxxxxx'
    })

symbol = 'ETHUSDT'
timeframe = '15m'
limit = 500

bars = binance.fetch_ohlcv (symbol, timeframe = timeframe, limit = limit)
df = pd.DataFrame(bars, columns = ['timestamp','open','high','low', 'close', 'volume'])
df['timestamp'] = pd.to_datetime(df['timestamp'], unit = 'ms').dt.tz_localize(tz='US/Central')
df['timestamp'] = pd.to_datetime(df['timestamp'], unit = 'ms').dt.tz_convert(tz='US/Central')
print(df)

                    timestamp     open     high      low    close      volume
0   2022-11-21 12:15:00-06:00  1120.63  1122.74  1118.26  1119.31   3278.5060
1   2022-11-21 12:30:00-06:00  1119.30  1127.48  1115.10  1125.31  11065.4442
2   2022-11-21 12:45:00-06:00  1125.32  1128.36  1123.92  1127.30   5447.6054
3   2022-11-21 13:00:00-06:00  1127.30  1136.75  1125.67  1133.81  15977.1500
4   2022-11-21 13:15:00-06:00  1133.82  1146.99  1132.77  1139.39  21009.7356
..                        ...      ...      ...      ...      ...         ...
495 2022-11-26 16:00:00-06:00  1210.90  1212.87  1208.77  1212.54   3822.1327
496 2022-11-26 16:15:00-06:00  1212.55  1213.92  1212.09  1213.63   2414.2695
497 2022-11-26 16:30:00-06:00  1213.62  1213.63  1211.05  1212.89   2461.4644
498 2022-11-26 16:45:00-06:00  1212.89  1212.94  1209.00  1209.76   2544.8965
499 2022-11-26 17:00:00-06:00  1209.75  1210.00  1207.74  1209.77   1638.1446

Solution

  • I think you want.

    df["timestamp"] = (
        pd.to_datetime(df["timestamp"], unit="ms")
        .dt.tz_localize("UTC")
        .dt.tz_convert("US/Central")
        .dt.tz_localize(None)
    )