Trying to create a numpy array and failing - python

So I am trying to run a code that was regularly working, but now I get an error
The first lines of my code are:
import numpy as np
array = np.array([0,1,2])
Then, I get the following error:
'list' object is not callable

This code is working fine in Jupyter notebook. You might want to restart your session and try again. An other alternative would be to use a different name to assign the array object instead of 'array' itself.

Related

Google-Colaboratory throwing "TypeError: 'tensorflow.python.framework.ops.EagerTensor' object is not callable" in code without errors

for some reason Google colab keeps on throwing errors everytime in code that has no errors,
an example is
import tensorflow as tf
print(tf.__version__)
scalar = tf.constant(7)
scalar
It throws an error for this which is TypeError: 'tensorflow.python.framework.ops.EagerTensor' object is not callable
I tried re-running it several times but that did not fix it, despite the code being correct. When I restarted and re-ran the code it worked but the code i had in my last line which was
matrix1 = tf.constant([[10, 7],
[7, 10]])
matrix1
does not work despite having the code
matrix = tf.constant([[10, 7],
[7, 10]])
matrix
which did work several lines above from this one.
I tried again to restart it and re-run the code but it still did not work.
I tried to update my google chrome because but my google chrome is at the latest version.
I made a new notebook and tried the code and it works! but I need to use the notebook I have because I have already written a lot of code there.
I also tried to update my operating system and now it is up to date. macOS Monterey 12.4 but it still did not fix anything.
The problem also occurs when i try to change a numpy array to a tensor
import numpy as np
numpy_A = np.arange(1, 25, dtype = np.int32) #create a Numpy arrary between 1 and 25
A = tf.constant(numpy_A, shape = (2, 3, 4))
and it is always TypeError: 'tensorflow.python.framework.ops.EagerTensor' object is not callable
that keeps getting thrown as the error.
and the code above is correct, I am following an online tutorial, and it worked and I kept checking my code over and over again but it is the same exact thing that was written. I am using the most up to date version of tensorflow as well which is 2.8.2
I have looked up various ways to fix this but there is nothing I have found.
I am using colab.research.google.com for this code. And I have to keep using colab for the online course, in order to take notes, and run several things individually and follow along with the instructor.

how to set window=6400 in plt.specgram command?

I would like to make spectrogram in python but I am not familiar with commands. I know how it works in MATLAB, but it responds differently in python.
In matlab, my command is like,
[~,f,t,z1] = spectrogram(x,window,overlap,nfft,fs)
where,
overlap=5120, window=6400, nfft=6400, fs=100.
I would like to input same data in pyhton, but it shows error when I type:
import matplotlib.pyplot as plt
plt.specgram(x,fs=100,NFFT=6400,noverlap=5120,window=6400)
it shows: TypeError: 'int' object is not callable
Also, how to obtain f,t,Z1?
Any help is greatly appreciated.

TypeError: 'tuple' object is not callable (for print statement)

import numpy as np
a=np.load('ampli/array.npy')
print(a)
Im trying to print an npy array like this from colab. I've used this exact syntax to print other arrays and it has worked but this isnt working suddenly. I tried opening a new notebook and uploaded the npy file there and it printed it out without any issue. How can I resolve this issue?
The error suggests that it is your print that is not callable.
Where the line print(a) fails, replace by print("1") and make sure it fails also.
Then find where the print built-in method is being assigned to some tuple.

Facing "'numpy.ndarray' object has no attribute 'median'" error while using median

I was going through a cheat sheet of NumPy and found the median() function. As I was trying it, an error was thrown. Here, you can find the cheat sheet. The code is:
import numpy as np
check = np.array([1, 2, 3])
check.median()
The error message is:
'numpy.ndarray' object has no attribute 'median'
In the official NumPy doc, I found that I should use median() as np.median(check). So, I was wondering if check.median() is an obsolete method or I was doing anything in a wrong way?
The NumPy version I was using was 1.19.5. Then I upgraded it to the latest version (1.21.2). Still, the same error persists.

DataFrame.set_index returns 'str' object is not callable

I'm not looking for a solution here as I found a workaround; mostly I'd just like to understand why my original approach didn't work given that the work around did.
I have a dataframe of 2803 rows with the default numeric key. I want to replace that with the values in column 0, namely TKR.
So I use f.set_index('TKR') and get
f.set_index('TKR')
Traceback (most recent call last):
File "<ipython-input-4-39232ca70c3d>", line 1, in <module>
f.set_index('TKR')
TypeError: 'str' object is not callable
So I think maybe there's some noise in my TKR column and rather than scrolling through 2803 rows I try f.head().set_index('TKR')
When that works I try f.head(100).set_index('TKR') which also works. I continue with parameters of 500, 1000, and 1500 all of which work. So do 2800, 2801, 2802 and 2803. Finally I settle on
f.head(len(f)).set_index('TKR')
which works and will handle a different size dataframe next month. I would just like to understand why this works and the original, simpler, and (I thought) by the book method doesn't.
I'm using Python 3.6 (64 bit) and Pandas 0.18.1 on a Windows 10 machine
You might have accidentally assigned the pd.DataFrame.set_index() to a value.
example of this mistake: f.set_index = 'intended_col_name'
As a result for the rest of your code .set_index was changed into a str, which is not callable, resulting in this error.
Try restarting your notebook, remove the wrong code and replace it with f.set_index('TKR')
I know it's been a long while, but I think some people may need the answer in the future.
What you do with f.set_index('TKR') is totally right as long as 'TKR' is a column of DataFrame f.
That is to say, this is a bug you are not supposed to have. It is always because that you redefine some build-in function methods or functions of python in your former steps(Possibly 'set_index'). So, the way to fix is to review your code to find out which part is wrong.
If you are using Jupiter notebook, restart it and run this block only can fix this problem.
I believe I have a solution for you.
I ran into the same problem and I was constructing my dataframes from a dictionary, like this:
df_beta = df['Beta']
df_returns = df['Returns']
then, trying to do df_beta.set_index(Date) would fail. My workaround was
df_beta = df['Beta'].copy()
df_returns = df['Returns'].copy()
So apparently, if you build your dataframes as a "view" of another existing dataframe, you can't set index and it will raise 'Series not callable' error. If instead you create an explicit new object copying the original dataframes, then you can call reset_index, which is what you kind of do when you compute the head.
Hope this helps, 2 years later :)
I have the same problem here.
import tushare as ts
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
ts.set_token('*************************************')
tspro = ts.pro_api()
gjyx = tspro.daily(ts_code='000516.SZ', start_date='20190101')
# this doesn't work
# out:'str' object is not callable
gjyx = gjyx.set_index('trade_date')
# this works
gjyx = gjyx.head(len(gjyx)).set_index('trade_date')
jupyter notebook 6.1.6, python 3.9.1, miniconda3, win10
But when I upload this ipynb to ubuntu on AWS, it works.
I once had this same issue.
This simple line of code keep throwing TypeError: 'series' object is not callable error again and again.
df = df.set_index('Date')
I had to shutdown my kernel and restart the jupyter notebook to fix it.

Categories

Resources