将时间戳分为两个单独的列作为python中的日期和时间

时间:2019-07-12 20:04:17

标签: python

代码:

import requests
import pandas as pd
import arrow
import datetime
from datetime import date
import pandas as pd

# import pandas_datareader as datareader

import matplotlib.pyplot as plt

from mpl_finance import candlestick_ohlc

def get_quote_data(symbol='GOOGL', data_range='1d', data_interval='60m'):
    res = requests.get('https://query1.finance.yahoo.com/v8/finance/chart/{symbol}?range={data_range}&interval={data_interval}'.format(**locals()))
    data = res.json()
    body = data['chart']['result'][0]    
    dt = datetime.datetime
    dt = pd.Series(map(lambda x: arrow.get(x).to('US/Central').datetime.replace(tzinfo=None), body['timestamp']), name='Datetime')
    df = pd.DataFrame(body['indicators']['quote'][0], index=dt)
    dg = pd.DataFrame(body['timestamp'])   
    df = df.loc[:, ('open', 'high', 'low', 'close', 'volume')]
    df.dropna(inplace=True)     #removing NaN rows
    df.columns = ['OPEN', 'HIGH','LOW','CLOSE','VOLUME']    #Renaming columns in pandas

    return df

data = get_quote_data('BKNG', '30d', '60m')
#data1 = get_quote_data('BKNG', '1d', '60m')
print(data)

#print(data1)

输出:

---------------------------------------
                           OPEN         HIGH          LOW        CLOSE  \
Datetime                                                                  
2019-05-31 08:30:00  1653.254517  1658.060059  1640.535156  1656.219971   
2019-05-31 09:30:00  1656.989990  1662.140015  1650.270020  1660.010010   
2019-05-31 10:30:00  1660.000000  1660.790039  1651.060059  1655.670044   
2019-05-31 11:30:00  1654.709961  1658.079956  1650.050049  1657.474976   
2019-05-31 12:30:00  1658.890015  1660.175049  1655.750000  1656.390015   
2019-05-31 13:30:00  1657.060059  1658.599976  1653.589966  1654.650024   
2019-05-31 14:30:00  1654.530029  1658.140015  1653.550049  1655.000000   
2019-06-03 08:30:00  1658.500000  1675.944946  1658.349976  1671.275024   
2019-06-03 09:30:00  1671.355103  1692.209961  1671.119995  1672.280029   
2019-06-03 10:30:00  1672.064941  1672.229980  1652.170044  1653.640015   
2019-06-03 11:30:00  1654.339966  1660.430054  1652.058350  1658.910034   
2019-06-03 12:30:00  1659.510010  1664.099976  1653.974976  1655.989990   
2019-06-03 13:30:00  1655.540039  1657.930054  1649.300049  1650.000000   
2019-06-03 14:30:00  1650.000000  1654.020020  1648.377930  1650.699951   
2019-06-04 08:30:00  1675.989990  1722.469971  1674.390015  1720.469971   
2019-06-04 09:30:00  1720.469971  1729.280029  1715.050049  1727.359985   
2019-06-04 10:30:00  1727.364990  1730.989990  1722.060059  1727.930054  

我想将第一列“时间戳”分为“日期”和“时间”两个单独的列。请帮忙。

1 个答案:

答案 0 :(得分:0)

df['datetime'] = pd.to_datetime(df['datetime'])
df['date'] = df['timestamp'].date
df['time'] = df['timestamp'].time

希望这会有所帮助!

有关更多信息,请查看pandas to_datetime