I am using this function to pull data from the Cryptocompare website into a pandas dataframe:
def daily_price_historical(symbol, comparison_symbol='USD', limit=1, aggregate=1, exchange='', allData='true'):
url = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit={}&aggregate={}&allData={}'\
.format(symbol.upper(), comparison_symbol.upper(), limit, aggregate, allData)
if exchange:
url += '&e={}'.format(exchange)
page = requests.get(url)
data = page.json()['Data']
df = pd.DataFrame(data)
df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]
df.set_index('timestamp', inplace=True)
df['symbol'] = symbol
df['1dret'] = 100* df['close'].pct_change()
return df
This works fine for most symbols I pass in, but when I loop over a longer list of symbols I get the error: AttributeError: 'DataFrame' object has no attribute 'time'
I assume this is due to the API returning an error for certain symbols, e.g.:
https://min-api.cryptocompare.com/data/histoday?fsym=FAKE&tsym=USD
returns "Response":"Error" with no further data
I'm afraid I'm not very experienced with url requests/APIs. Is there code I can add to the function to skip the symbols that are causing the issue?
Thanks for your help!
Additional information:
Code used to loop over coins (which is a list of 130 symbols):
price_columns = ['close', 'high', 'low', 'open', 'time',
'volumefrom','volumeto', 'symbol', '1dret']
top_coin_prices = pd.DataFrame(columns=price_columns)
for coin in coins:
output = daily_price_historical(coin)
top_coin_prices = top_coin_prices.append(output)
Full Traceback:
AttributeError Traceback (most recent call last)
<ipython-input-277-126f5d1686b2> in <module>()
8 # populate df with data for all coins
9 for coin in coins:
---> 10 output = daily_price_historical(coin)
11 top_coin_prices = top_coin_prices.append(output)
12
<ipython-input-111-65b3fa76b4ab> in daily_price_historical(symbol, comparison_symbol, limit, aggregate, exchange, allData)
7 data = page.json()['Data']
8 df = pd.DataFrame(data)
----> 9 df['timestamp'] = [datetime.datetime.fromtimestamp(d) for d in df.time]
10 df.set_index('timestamp', inplace=True)
11 df['symbol'] = symbol
/anaconda/lib/python3.6/site-packages/pandas/core/generic.py in __getattr__(self, name)
2968 if name in self._info_axis:
2969 return self[name]
-> 2970 return object.__getattribute__(self, name)
2971
2972 def __setattr__(self, name, value):
AttributeError: 'DataFrame' object has no attribute 'time'
Related
def get_IB_historical_data(self, ibcontract, tickerid, durationStr, barSizeSetting):
historic_data_queue = finishableQueue(self.init_historicprices(tickerid))
self.reqHistoricalData(
tickerid, # tickerId,
ibcontract, # contract,
datetime.datetime.today().strftime("%Y%m%d %H:%M:%S %Z"), # endDateTime,
durationStr, # durationStr,
barSizeSetting, # barSizeSetting,
"TRADES", # whatToShow,
1, # useRTH,
1, # formatDate
False, # KeepUpToDate <<==== added for api 9.73.2
[] ## chartoptions not used
)
MAX_WAIT_SECONDS = 10
historic_data = historic_data_queue.get(timeout = MAX_WAIT_SECONDS)
if historic_data_queue.timed_out():
print("historic_data_queue.timed_out")
self.cancelHistoricalData(tickerid)
df = pd.DataFrame(historic_data)
df.columns = ['Datetime', 'Open', 'High', 'Low', 'Close', 'Volume']
return df
if name == 'main':
app = App_Class('127.0.0.1',7497, 11)
time.sleep(1234)
ibcontract = IBcontract()
ibcontract.secType = 'FUT'
ibcontract.lastTradeDateOrContractMonth = '20221129'
ibcontract.symbol = 'HSI'
ibcontract.exchange = 'HKFE'
resolved_ibcontract = app.resolve_ib_contract(ibcontract)
print(resolved_ibcontract)
df = app.get_IB_historical_data(resolved_ibcontract, 10, durationStr='30 D', barSizeSetting='1 D')
print(df)
I am new python learner. I dont know why i cant print the dataframe, I have subscribed the data.
My first run to run the program: 576501930,HSI,FUT,20221129,0.0,,50,HKFE,,HKD,HSIX2,HSI,False,,combo:
ERROR 10 320 Error reading request. String index out of range: 0
historic_data_queue.timed_out
ERROR -1 504 Not connected
ValueError: Length mismatch: Expected axis has 0 elements, new values have 6 elements
My second time to run the program and when i rerun, they said" _queue.Empty"
Anyone know why and how to fix it, thanks.
'''Add'''
ibcontract.includeExpired = True
'''under contract details'''
'''change from'''
datetime.datetime.today().strftime("%Y%m%d %H:%M:%S %Z"), # endDateTime,
'''to'''
datetime.datetime.today().strftime("%Y%m%d-%H:%M:%S"), # endDateTime,
'''in def get_IB_historical_data'''
I am trying to get EIA data using its API, however I encountered json errors when it's calling the series. I recalled it was working fine about 6 months ago, not sure if it is something changed in EIA's API. Could anyone shed some light how to fix this?
Here's the code:
import pandas as pd
import eia
def retrieve_data():
# Create EIA API using your specific API key
api_key = "YOUR_API_KEY"
api = eia.API(api_key)
# Retrieve Data By Series ID
series_ID='STEO.PASC_OECD_T3.M'
series_search = api.data_by_series(series=series_ID)
df = pd.DataFrame(series_search)
df.index.names = ['Date']
df.columns=[ "Price"]
df.index = df.index.str.replace('^([\d]{4})\s([\d]{2})([\d]
{2})\s[\d] {2}', r'\1-\2-\3',regex=True)
df.index = pd.to_datetime(df.index)
return df
data = retrieve_data()
print(data)
and the error message is as the following:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
/tmp/ipykernel_942387/4124051913.py in <module>
17 return df
18
---> 19 data = retrieve_data()
20 print(data)
21 #data.to_csv('OK_WTI_Spot_Price_FOB.csv',index=True)
/tmp/ipykernel_942387/4124051913.py in retrieve_data()
9 # Retrieve Data By Series ID
10 series_ID='STEO.PASC_OECD_T3.M'
---> 11 series_search = api.data_by_series(series=series_ID)
12 df = pd.DataFrame(series_search)
13 df.index.names = ['Date']
~/miniconda3/lib/python3.7/site-packages/eia/api.py in data_by_series(self, series)
422 else:
423 lst_dates = [x[0][0:4] + " " + x[0][4:] + " " + x[0][6:8]
--> 424 for x in search.json()['series'][0]['data']]
425 lst_values = [x[1] for x in
426 search.json()['series'][0]['data']]
KeyError: 'series'
Below is my sample dataframe, I would like to use networkdays to calculate the working day in columns in my df, but it got the issue as below, can someone help assist on this?
#import lib
import pandas as pd
from workdays import workday, networkdays
#sample dict to create df
dict1 = {
'startdate' : ['2022-01-17','2022-02-28'],
'enddate' : ['2022-01-17','2022-03-15']
}
#convert to datetime format
df['startdate'] = df['startdate'].astype('datetime64')
df['enddate'] = df['enddate'].astype('datetime64')
#create new column count and apply function
df['count']=df.apply(networkdays(df['startdate'],df['enddate']))
#getting error :
AttributeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_20080/333906513.py in <module>
----> 1 df['count']=df.apply(networkdays(df['startdate'],df['enddate']))
C:\ProgramData\Anaconda3\lib\site-packages\workdays.py in networkdays(start_date, end_date, holidays)
10
11 def networkdays(start_date, end_date, holidays=[]):
---> 12 delta_days = (end_date - start_date).days + 1
13 full_weeks, extra_days = divmod(delta_days, 7)
14 # num_workdays = how many days/week you work * total # of weeks
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __getattr__(self, name)
5485 ):
5486 return self[name]
-> 5487 return object.__getattribute__(self, name)
5488
5489 def __setattr__(self, name: str, value) -> None:
AttributeError: 'Series' object has no attribute 'days'
df['startdate'] is a series object, it contains more than one piece of data.
>> df['enddate'] # <class 'pandas.core.series.Series'>
0 2022-01-17
1 2022-03-15
You should pass a lambda to the df.apply function, if you have to apply a function to all the rows in the DataFrame. The parameter of the lambda is the row of data.
df = pd.DataFrame(dict1)
df['startdate'] = df['startdate'].astype('datetime64')
df['enddate'] = df['enddate'].astype('datetime64')
df['count']=df.apply(lambda x: networkdays(x['startdate'], x['enddate']), axis=1)
print(df)
# startdate enddate count
# 0 2022-01-17 2022-01-17 1
# 1 2022-02-28 2022-03-15 12
I get the following error:
NameError Traceback (most recent call last)
in
81 writer.save()
82
---> 83 write_excel(res)
84
NameError: name 'res' is not defined
Could you please help? :)
def read_merge(file):
"""" read files and merge the data """
username = pd.read_csv('file.csv', delimiter = ';')
merged_output = pd.read_excel('file-1.xlsx')
merged_output = pd.merge(merged_output, username, how='left', left_on=['h'], right_on=['h2'])
merged_output = merged_output.drop(columns=['h2'])
return(merged_output)
def remove_string(merged_output):
"""" remove rows containing strings from list_string """
list_string = ["obj-1", "obj-2"]
res = merged_output[~merged_output['Rule'].isin(list_string)]
return res
def add_agg_columns(res):
""""aggregate columns"""
res = res.groupby('Entity_Name').agg({'Rule': ', '.join,
'Name':'first',
'Area': 'first',
'Summary': 'first'}).reset_index()
def write_excel(res):
""""Write Excel document"""
writer = pd.ExcelWriter(r"C:\PATH\output.xlsx", engine = 'xlsxwriter')
workbook=writer.book
worksheet=workbook.add_worksheet('SHEET')
writer.sheets['SHEET'] = worksheet
res.to_excel(writer, sheet_name='SHEET', startcol = 0, startrow = 0)
writer.save()
write_excel(res)
The last line of code write_excel(res) references variable res which is not defined in your code.
The error message NameError: name 'res' is not defined should give you some clue at to what is going on
i m trying to make a trade bot that when macdh turns positive from negative i wanna get a buy signal. i get macdh values but when i type if parameter i get typeerror.
my error type is
if df['macdh'].iloc[i]>0 and df['macdh'].iloc[-2]<0:
TypeError: 'NoneType' object is not subscriptable
import requests
import json
from stockstats import StockDataFrame as Sdf
class TradingModel:
def __init__(self, symbol):
self.symbol = symbol
self.df = self.getData()
def getData(self):
# define URL
base = 'https://api.binance.com'
endpoint = '/api/v3/klines'
params = '?&symbol='+self.symbol+'&interval=4h'
url = base + endpoint + params
data = requests.get(url)
dictionary = json.loads(data.text)
# put in dataframe and clean-up
df = pd.DataFrame.from_dict(dictionary)
df = df.drop(range(6, 12), axis=1)
col_names = ['time', 'open', 'high', 'low', 'close', 'volume']
df.columns = col_names
for col in col_names:
df[col]=df[col].astype(float)
stock = Sdf.retype(df)
df['macdh']=stock['macdh']
df['macds']=stock['macds']
df['macd']=stock['macd']
print(df)
def strategy(self):
df = self.df
buy_signals=[]
for i in range(1,len(df['close'])):
if df['macdh'][i]>0 and df['macdh'].iloc[-2]<0:
buy_signals.append(df['time'][i],df['low'][i])
def Main():
symbol = "BTCUSDT"
model = TradingModel(symbol)
model.strategy()
if __name__ == '__main__':
Main() ``
when i added return df its done
df['macdh']=stock['macdh']
df['macds']=stock['macds']
df['macd']=stock['macd']
return df