Can't append NaN to python list - python-3.x

The error is
Traceback (most recent call last):
File "1.py", line 28, in <module>
buy.append(np.nan)
AttributeError: 'numpy.ndarray' object has no attribute 'nan'
Here the code its python 3
import fxcmpy
import socketio
from pylab import plt
import numpy as np
from finta import TA
TOKEN='xxxx'
con = fxcmpy.fxcmpy(access_token=TOKEN, log_level='error', server='real', log_file='log.txt')
#print(con.get_instruments())
data = con.get_candles('US30', period='D1', number=250)
con.close()
df1=data[['askopen','askhigh', 'asklow', 'askclose']]
plt.style.use('seaborn')
np=df1.to_numpy()
df2=df1.rename(columns={'askopen':'open','askhigh':'high','asklow':'low','askclose':'close'})
dfhma=TA.HMA(df2,14)
pr1=dfhma.shift(1)
pr2=dfhma.shift(2)
buy=[]
sell=[]
i=0
flag=''
for item in dfhma:
if item > pr1[i] and item > pr2[i] and flag!=1:
flag=1
buy.append(item)
else:
buy.append(np.nan)
if item < pr1[i] and item < pr2[i] and flag!=0:
flag=0
sell.append(item)
else:
sell.append(np.nan)
i=i+1
print(buy)
print('buy len='+str(len(buy)))
mk=[]
for item in dfhma:
print(item)
plt.plot(dfhma)
plt.scatter(dfhma.index,buy,marker='^',color='g')
plt.scatter(dfhma.index,sell,marker='v',color='r')
plt.show()
Search Google/Stackoverflow nothing was found and change nan to NaN,NAN still got the same error guessing its newbie error Help !! Just try to add NaN to the list as a buy/sell signal and it doesn't work what could be wrong here ?

In line 14 np=df1.to_numpy() you reassigned variable np from a package to a numpy array. So when you called np.nan it was searching nan from the numpy ndarray instance, not the package.
Change the variable to any other name and it will work fine.

Related

Struggling to displaying the right (formatted) value for a matplotlib labels

Guide:
https://theoehrly.github.io/Fast-F1/examples_gallery/plot_qualifying_results.html#sphx-glr-examples-gallery-plot-qualifying-results-py
I am having trouble displaying the correct value or formatted form as a matplotlib label.
Issue: Bar Graph labels displaying as an unwanted, or badly formatted values.
(Is this TimeDelta[ns] in an integer under scientific notation? The dtype is timedelta64[ns])
Expected Values: The amount of time each driver is from the leader (s.ms) (HAM=0.038). Note: order is the same
print(times)
Code:
#!/usr/bin/python3-64
#required packages
#pip3 install fastf1
#pip3 install pandas
#pip3 install matplotlib
#pip3 install numpy
import matplotlib.pyplot as plt
import matplotlib.patches as pat
import fastf1 as ff1
import fastf1.plotting as ff1p
ff1p.setup_mpl(mpl_timedelta_support=True, color_scheme=None, misc_mpl_mods=False)
from fastf1.core import Laps
import pandas as pd
import numpy as np
from timple.timedelta import strftimedelta as td
import os
l=str.lower
def data_cache():
cache='/ff1_temp' #temp cache
while(True):
warn=input(l(f'!WARNING! A data cache will be made at {cache}\n'
f'Formula 1 Race Data will be downloaded to {cache}\n'
f'Would you like to continue? [y/n]\n'))
if(warn=='n'):
print('Quitting!\n')
exit(0)
elif(warn=='y'):
print(f'cache location: {cache}\n')
if not os.path.exists(cache): # os.path.exists(cache)
os.mkdir(cache) # os.mkdir(cache)
ff1.Cache.enable_cache(cache) # Fast F1 Cache API
break
else:
print('Plese Enter [y/n]\n')
continue
def data_load():
data=ff1.get_session(2021,'Netherlands','Q') #Y,L,S = Year, Location, Session
data.load(laps=True,telemetry=False,weather=False,messages=False)
return(data)
def data_graph():
data=data_load()
drivers=pd.unique(data.laps['DriverNumber'])
fll=list()
for row in drivers: #get fastest laps for session from each driver
fld=data.laps.pick_driver(row).pick_fastest()
fll.append(fld)
fl=Laps(fll).sort_values(by='LapTime').reset_index(drop=True)
flf=fl.pick_fastest()
fl['LapTimeDelta']=fl['LapTime']-flf['LapTime'] #determine the TimeDelta from leader
tc=list()
for index, lap in fl.iterlaps(): #team colours
color=ff1p.team_color(lap['Team'])
tc.append(color)
return(fl,tc,flf)
def data_plot():
fl,tc,flf=data_graph()
fig,ax=plt.subplots()
times=fl['LapTimeDelta']
fli=fl.index
# y x
bars=ax.barh(fli,times, color=tc,edgecolor='grey')
print(times) #expected values
ax.set_yticks(fl.index)
ax.set_yticklabels(fl['Driver'])
ax.set_xlabel('Time Difference (ms)')
#should be x axis?
ax.bar_label(bars) #(times)
ax.invert_yaxis()
lt=td(flf['LapTime'], '%m:%s.%ms')
plt.suptitle(f'2021 Dutch GP Qualifying\n'
f"Fastest at {lt} ({flf['Driver']})")
plt.show()
if(__name__=="__main__"):
data_cache()
data_plot()
exit(0)
results of print(bars)
results of print(type(times)) and print(type(bars))
What has been Attempted:
def data_plot():
ax.bar_label(times)
Traceback (most recent call last):
File "\python\datacollection\fp1.ff1.graph.py", line 144, in <module>
data_plot()
File "\python\datacollection\fp1.ff1.graph.py", line 132, in data_plot
ax.bar_label(times)
File "\Python\Python310\lib\site-packages\matplotlib\axes\_axes.py", line 2609, in bar_label
bars = container.patches
File "\Python\Python310\lib\site-packages\pandas\core\generic.py", line 5575, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Lap' object has no attribute 'patches'
---
def data_plot_label(fli,times):
for i in range(len(fli)):
plt.text(i,times[i],times[i],ha='center',bbox=dict(alpha=0.8))
def data_plot():
data_plot_label(fli,times)
Close:
I'm still pretty green with this stuff,
Am I going about this correctly?
What are my options regarding labelling and matplotlib?
How do I set the correct formatted value for this label?
I find the graph is harder to understand without the actual values on it. It has less depth.
Relevant Docs:
https://theoehrly.github.io/Fast-F1/
https://pandas.pydata.org/docs/reference/index.html
https://matplotlib.org/stable/api/index
I overlooked something in the docs. I was not specifying the label only the container.
Reference:
https://matplotlib.org/stable/api/_as_gen/matplotlib.axes.Axes.bar_label.html#matplotlib.axes.Axes.bar_label
Axes.bar_label(container, labels=None, *, fmt='%g', label_type='edge', padding=0, **kwargs)
Solution:
prfx='0 days 00:00:0'
sufx='000'
remov=''
def data_plot():
#removes leading and tailingzeros
times=times.astype(str).str.replace(prfx,remov).str.replace(sufx,remov)
#before: 0 days 00:00:0x.xxx000
#after: x.xxx
#over looked label, label_type=position-on-bar
ax.bar_label(bars, times, label_type='edge')
Just a little more formatting and it should look great!

AttributeError: 'DataFrame' object has no attribute 'NET_NAME'

python 3.7
A task. Add a new column in the received date frame based on two conditions:
if the value in the NET_NAME column is equal to one of the list and the value in the ECELL_TYPE column is LTE, then assign the value to the SHARING column from the ENODEB_NAME column.
import csv
import os
import pandas as pd
import datetime
import numpy as np
from time import gmtime, strftime
WCOUNT=strftime("%V", gmtime())
WCOUNT = int(WCOUNT)
WCOUNT_last = int(WCOUNT)-1
os.environ['NLS_LANG'] = 'Russian.AL32UTF8'
cell_file_list=pd.read_excel('cdt_config.xlsx',sheet_name ='cdt_config',index_col='para_name')
filial_name_list=pd.read_excel('FILIAL_NAME.xlsx')
gcell_file_name1=cell_file_list.para_value.loc['ucell_file_name']
ecell_file_name=cell_file_list.para_value.loc['ecell_file_name']
cols_simple=['RECDATE','REGION_PHOENIX_NAME','NET_NAME','CELL_NAME_IN_BSC','ENODEB_NAME','ECELL_TYPE','NRI_ADDRESS', 'NRI_BS_NUMBER','NRI_SITEID','STOPTIME', ]
cols_export=['GSM', 'UMTS', 'LTE', 'TOTAL', 'NWEEK', 'SHARING' ]
ecell_df=df = pd.read_csv(ecell_file_name, sep=",",encoding='cp1251',
dtype={'NRI_SITEID': str})
ecell_df=ecell_df.rename(columns={"RECDATE.DATE": "RECDATE"})
ecell_df=ecell_df.rename(columns={"ECELL_MNEMONIC": "CELL_NAME_IN_BSC"})
#replace ","
ecell_df.STOPTIME=pd.to_numeric(ecell_df.STOPTIME.replace(',', '', regex=True), errors='coerce')/3600
ecell_df=ecell_df[cols_simple]
#pivot ecell table
ecell_sum_df=pd.pivot_table(ecell_df,values='STOPTIME',index=['RECDATE','NRI_SITEID','REGION_PHOENIX_NAME','NET_NAME','ENODEB_NAME','ECELL_TYPE'],aggfunc='sum')
ecell_sum_df=ecell_sum_df.fillna(0)
#create a empty column with the same index as the pivot table.
ecell_export_df= pd.DataFrame(index=ecell_sum_df.index.copy())
ecell_export_df=ecell_export_df.assign(LTE=0)
ecell_export_df.LTE=ecell_sum_df.STOPTIME
ecell_export_df['SHARING'] = 0
ecell_export_df.SHARING.replace(ecell_export_df.NET_NAME in filial_name_list, ENODEB_NAME,inplace=True)
print(ecell_export_df)
#print (ecell_export_df)
del ecell_df
del ecell_sum_df
export_df=pd.concat([ecell_export_df],join='outer',axis=1)
export_df=export_df.fillna(0)
export_df['TOTAL'] = export_df.sum(axis=1)
export_df['NWEEK'] = WCOUNT_last
del ecell_export_df
#################################################
Below is the error message:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/ReportCDT/CDT 4G_power pivot.py", line 43, in <module>
ecell_export_df.SHARING.replace(ecell_sum_df.NET_NAME in filial_name_list, ENODEB_NAME,inplace=True)
File "C:\Users\vavrumyantsev\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\core\generic.py", line 5067, in __getattr__
eturn object.__getattribute__(self, name)
AttributeError: 'DataFrame' object has no attribute 'NET_NAME'
Your traceback contains: DataFrame object has no attribute NET_NAME,
meaning actually that this DataFrame has no column of this name.
This message pertains to ecell_sum_df.NET_NAME (also contained in
the traceback), so let's look how you created this DataFrame (slightly
reformatted for readablity):
ecell_sum_df=pd.pivot_table(ecell_df, values='STOPTIME',\
index=['RECDATE', 'NRI_SITEID', 'REGION_PHOENIX_NAME', 'NET_NAME',
'ENODEB_NAME', 'ECELL_TYPE'], aggfunc='sum')
Note that NET_NAME is a part of the index list, so in the DataFrame
created it is a part of the MultiIndex, not an "ordinary" column.
So Python is right displaying this message.
Maybe you should move this level of the MultiIndex to a "normal" column?

Tensorflow TypeError: 'numpy.ndarray' object is not callable

while trying to predict the model i am getting this numpy.ndarray error .it might be the returning statement of the prepare function. what can be possibly done to get rid of this error .
import cv2
import tensorflow as tf
CATEGORIES = ["Dog", "Cat"]
def prepare(filepath):
IMG_SIZE = 50 # 50 in txt-based
img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
model = tf.keras.models.load_model("64x3-CNN.model")
prediction = model.predict([prepare('dog.jpg')])
print(prediction) # will be a list in a list.
tried to give the full path still the same error persist.
TypeError Traceback (most recent call last)
<ipython-input-45-f9de27e9ff1e> in <module>
15
16 prediction = model.predict([prepare('dog.jpg')])
---> 17 print(prediction) # will be a list in a list.
18 print(CATEGORIES[int(prediction[0][0])])
TypeError: 'numpy.ndarray' object is not callable
Not sure what the rest of your code looks like. But if you use 'print' as a variable in Python 3 you can get this error:
import numpy as np
x = np.zeros((2,2))
print = np.ones((2,2))
print(x)
Output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'numpy.ndarray' object is not callable
This type of errors mostly occurs when trying to print an array instead of simple strings or single variable numbers, so I would recommend you to change:
17 print(prediction) # will be a list in a list.
18 print(CATEGORIES[int(prediction[0][0])])
Then you would get:
17 print(str(prediction)) # will be a list in a list.
18 print(str(CATEGORIES[int(prediction[0][0])]))

python cv2.imread return none on 6th image

I am trying to import and read all images in a folder. However, when I have more than 5 images, cv2.imread returns none for the 6th image. I have tried using different file names, different files, etc, but I can't get it to work.
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tkinter import filedialog
import os
from mpl_toolkits.mplot3d import Axes3D
global scan_dir
scan_dir = filedialog.askdirectory()
print(scan_dir)
x=os.listdir(scan_dir)
img={}
print(x)
for i in range(0,len(x)):
print(i)
img[i] = cv2.imread(x[i], cv2.IMREAD_GRAYSCALE)
indices[i] = np.where(img[i]<100)
I get the following error...(None is the return of print(img[i] on 6th iteration of the loop)
None
Traceback (most recent call last):
File "C:\CodeRepository\US-3D\GettingCloser.py", line 55, in <module>
indices[i] = np.where(img[i]<100)
TypeError: '<' not supported between instances of 'NoneType' and 'int'
I have the same problem if I try this
global scan_dir
scan_dir = filedialog.askdirectory()
print(scan_dir)
x=os.listdir(scan_dir)
img = cv2.imread(x[5], cv2.IMREAD_GRAYSCALE)
It will return that img is None. This is true for anything beyond the 5th image.
Must be something wrong with the file. dicts are an unordered Data structure. Should not give error always on 5th iteration. However, I have made the changes which will not throw the error. But you need to debug that image
for i in range(0,len(x)):
print(i)
img[i] = cv2.imread(x[i], cv2.IMREAD_GRAYSCALE)
if img[i]:
indices[i] = np.where(img[i]<100)

Convert string to float error in pandas machine learning

For my machine learning code, I have some unknown values with '?' in my csv file. So, I am trying to replace them with 'Nan' but it throws some error. The following code is for the replacement of '?' that I have used. Can anyone please solve this?
Thanks in advance !
import numpy
import pandas as pd
import matplotlib as plot
import numpy as np
df = pd.read_csv('cdk.csv')
x=df.iloc[:,0:24].values
y=df.iloc[:,24].values
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values='NaN', strategy='most_frequent', axis =0,copy=False)
imputer = imputer.fit(x[:,0:5])
imputer.fit_transform(x[:,0:5])
imputer = Imputer(missing_values='normal', strategy='mode', axis =0,copy=False)
imputer = imputer.fit(x[:,5:7])
imputer.fit_transform(x[:,5:7])
This is what error it throws,
Traceback (most recent call last):
File "kidney.py", line 10, in <module>
imputer = imputer.fit(x[:,0:5])
File "C:\Users\YAASHI\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\preprocessing\imputation.py", line 155, in fit
force_all_finite=False)
File "C:\Users\YAASHI\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\validation.py", line 433, in check_array
array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: could not convert string to float: '?'
Link for the csv file
If you want to replace all ? strings with NaN, do this:
df.replace('?', np.nan, inplace=True)
Or better yet, load them as NaN as you read the CSV:
df = pd.read_csv('cdk.csv', na_values=['?'])

Resources