我正在使用Python和Pandas。
我在下面的代码中读取了亚马逊股票的信息:
import gspread
from oauth2client.service_account import ServiceAccountCredentials
import pprint
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
from scipy.signal import argrelextrema
from pandas_datareader import data as pdr
import fix_yahoo_finance as yf
import pandas as pd
def sample_reading_from_yahoo():
# setting plotting, start / end date and time
style.use('ggplot')
# setting up the date and time
start = dt.datetime(2017, 1, 1)
end = dt.date.today()
# pull back the data
yf.pdr_override()
#data = pdr.get_data_yahoo("AMZN", start, end="2017-09-12")
data = pdr.get_data_yahoo("AMZN", start, end)
data = data.rename(columns={'Open': 'open'})
data = data.rename(columns={'High': 'high'})
data = data.rename(columns={'Low': 'low'})
data = data.rename(columns={'Close': 'close'})
data = data.rename(columns={'Adj Close': 'adjusted'})
data = data.rename(columns={'Volume': 'vol'})
data = data.drop_duplicates(keep=False)
data = data[['open', 'high', 'low', 'close', 'vol']]
return data
# MAIN
data = sample_reading_from_yahoo()
print(data)
输出如下所示:
open high low close vol
Date
2017-01-03 757.919983 758.760010 747.700012 753.669983 3521100
2017-01-04 758.390015 759.679993 754.200012 757.179993 2510500
2017-01-05 761.549988 782.400024 760.260010 780.450012 5830100
2017-01-06 782.359985 799.440002 778.479980 795.989990 5986200
2017-01-09 798.000000 801.770020 791.770020 796.919983 3440100
2017-01-10 796.599976 798.000000 789.539978 795.900024 2558400
每当我按照以下方式更改MAIN区域时,
# MAIN
data = sample_reading_from_yahoo()
print(data)
data.Date = pd.to_datetime(data.Date, format='%d.%m.%Y %H:%M:%S.%f')
data.set_index(data.Date)
我收到错误
Traceback(最近一次调用最后一次):文件 " C:/Users/dgmufasa/PycharmProjects/Opportunities/Opportunities.py" ;, 第96行,in data = sample_reading_from_yahoo()File" C:/Users/dgmufasa/PycharmProjects/Opportunities/Opportunities.py", 第86行,在sample_reading_from_yahoo中 data.Date = pd.to_datetime(data.Date,format ='%d。%m。%Y%H:%M:%S%f')文件 " C:\ WORK \ SOFTWARE \ Python64bitv3.6 \ lib中\站点包\大熊猫\芯\ generic.py&#34 ;, 第3614行, getattr return object。 getattribute (self,name)AttributeError:' DataFrame'对象没有属性'日期'
我该如何解决这个问题?
@CK Chen - 感谢您的回复。
我做了以下更改:
def sample_reading_from_yahoo():
[... snip ...]
#print(data['vol'])
data = data.drop_duplicates(keep=False)
data = data[['open', 'high', 'low', 'close', 'vol']]
return data
# MAIN
data = sample_reading_from_yahoo()
#print(data.index)
data['datetime_index'] = pd.to_datetime(data.index, format='%m-%d-%Y %H:%M:%S')
data.set_index('datetime_index')
print('THIS THE DATA RESULT')
print(data)
得到了结果:
THIS THE DATA RESULT
open high low close vol \
Date
2017-01-03 757.919983 758.760010 747.700012 753.669983 3521100
2017-01-04 758.390015 759.679993 754.200012 757.179993 2510500
2017-01-05 761.549988 782.400024 760.260010 780.450012 5830100
datetime_index
Date
2017-01-03 2017-01-03
2017-01-04 2017-01-04
2017-01-05 2017-01-05
似乎时间格式没有改变。我做错了什么?
TIA
答案 0 :(得分:0)
'日期'是dataframe的index.name。
你可以尝试:
data['datetime_index'] = pd.to_datetime(data.index, format='%Y-%m-%d')
data.set_index('datetime_index')