Issue with Pandas and circular reference in import - python

I am trying to use pandas for the first time and I have copied a very simple program
import pandas as pd
series1 = pd.Series([1,2,3,4])
print(series1)
The issue I am having is that when I try and run the program I am getting the following error
Traceback (most recent call last):
File "C:\Users\faintr\AppData\Local\Programs\Python\Python38-32\pandas.py", line 1, in <module>
import pandas as pd
File "C:\Users\faintr\AppData\Local\Programs\Python\Python38-32\pandas.py", line 2, in <module>
series1 = pd.Series([1,2,3,4])
AttributeError: partially initialized module 'pandas' has no attribute 'Series' (most likely due to a circular import)
Pandas version(1.14.0) is installed.
I have looked into circular imports and whilst I think I understand the concept if this is the cause I do not know how to fix it. I have tried downgrading pandas to an earlier version with no look
Can anyone help please

Related

Matplotlib can't find documented function set_cmap

I have the following code:
import matplotlib.pyplot as plt
plt.cm.set_cmap("Blues")
This gives me an error:
Traceback (most recent call last):
File ".\lorenz_explorer.py", line 12, in <module>
plt.cm.set_cmap("Blues")
AttributeError: module 'matplotlib.cm' has no attribute 'set_cmap'
My matplotlib version is 3.3.1, and the function certainly exists in the documentation for 3.3.1: Link
Then am I doing something wrong or is this a bug? Do I need to import matplotlib.cm separately or something along those lines?
As the documentation link you provide shows, the name of the function is matplotlib.pyplot.set_cmap, not matplotlib.pyplot.cm.set_cmap. So you can call it with plt.set_cmap("Blues").
In other words, the function is not part of the cm library, which is somewhat counter-intuitive.

When i use numpy it says - unused import statement .How to fix it?

I have installed numpy but when I import it, it doesn't work.
from numpy import *
arr=array([1,2,3,4])
print(arr)
Result:
C:\Users\YUVRAJ\PycharmProjects\mycode2\venv\Scripts\python.exe C:/Users/YUVRAJ/PycharmProjects/mycode2/numpy.py
Traceback (most recent call last):
File "C:/Users/YUVRAJ/PycharmProjects/mycode2/numpy.py", line 1, in <module>
from numpy import *
File "C:\Users\YUVRAJ\PycharmProjects\mycode2\numpy.py", line 2, in <module>
x=array([1,2,3,4])
NameError: name 'array' is not defined
Process finished with exit code 1
The problem is you named your script as numpy.py, which is a conflict with the module numpy that you need to use. Just rename your script to something else and will be fine.
Instead of using from numpy import *
Try using this:
import numpy
from numpy import array
And then add your code:
arr=array([1,2,3,4])
print(arr)
EDIT: Even though this is the accepted answer, this may not work under all circumstances. If this doesn't work, see adrtam's answer.

'function' object has no attribute 'plot'

I was following this tutorial https://www.kaggle.com/residentmario/univariate-plotting-with-pandas
and trying to do the exercise mentioned with the pokemon database but whenever I try to implement the code below I get the error mentioned below and don't understand what to do. I am using matplotlib.use('agg') because I was getting an error related to Tkinter. I am using pycharm, python 3.6 and I am on ubuntu 18.04
Here is my code:
import pandas as pd
import matplotlib
matplotlib.use('agg')
from matplotlib.pyplot import plot
df=pd.read_csv("/home/mv/PycharmProjects/visualization/pokemon.csv")
df['type1'].value_counts.plot(kind='bar')
error
Traceback (most recent call last):
File "/home/mv/PycharmProjects/visualization/univariate plotting.py",
line 9, in <module>
df['type1'].value_counts.plot(kind='bar')
AttributeError: 'function' object has no attribute 'plot'
The error states that df['type1'].value_counts is a function.
To plot the result of the function change:
df['type1'].value_counts.plot(kind='bar')
into
df['type1'].value_counts().plot(kind='bar')

Resolving "AttributeError: module 'dask.bag' has no attribute 'from_filenames'"

I am following a simple tutorial to load Reddit comment data from pushshift.io into a dask bag. I am getting the strange error: "Resolving "AttributeError: module 'dask.bag' has no attribute 'from_filenames'", despite the fact that this is standard procedure as described here: http://dask.pydata.org/en/doc-test-build/bag.html
import dask
import dask.bag as db
data = db.from_filenames("reddit_1_28_2018.txt", chunkbytes=100000).map(json.loads)
AttributeError Traceback (most recent call last)
<ipython-input-17-bcbd31affbfb> in <module>()
2 import dask.bag as db
3
----> 4 data = db.from_filenames("reddit_1_28_2018.txt", chunkbytes=100000).map(json.loads)
AttributeError: module 'dask.bag' has no attribute 'from_filenames'
I suspect that the resource you were looking at was very old. I recommend reading the Dask documentation for up-to-date information.
I suspect that you're looking for db.read_text

AttributeError: 'module' object (scipy) has no attribute *** Why does this error occur?

In scipy, the error occurs quite often.
>>> import scipy
>>> scipy.integrate.trapz(gyroSeries, timeSeries)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'integrate'
>>>
I figure out how to solve this problem by doing the following:
>>>
>>> import scipy.integrate
>>> scipy.integrate.trapz(gyroSeries, timeSeries)
>>> 1.2
My question:
Why does the error occur?
Why would that fix the error?
Most possibly because scipy is a library (package) that contains modules and to import a specific module from the scipy library, you need to specify it and import the module itself. As it's a separate module (sub-package), once you import it, it's attributes are available to you by using the regular scipy.module.attribute
In order to fix the error, add the following line at the top of your script
from scipy import integrate
Just simply use
import scipy.constants as spc
and then
C = spc.c #speed of light m/s
pi = spc.pi

Categories