I have a problem with my pandas. First I tried easy-install on cd C:\Python27\Scrips...doing. easy_install.exe numpy, and easy_install.exe pandasbut only the numpy worked, the pandas didnt, so I used pip to install. and it worked on both. But going back to my code, when I tested it,
heres my code:
import fileinput
import csv
import numpy
import pandas
#records = pe.iget_records(file_name="test.xlxs")
records = pd.read_csv('test.csv', header=None, nrows=5)
#To Write ni
cho = raw_input("\nStart Forecaster on file?:<1/0>")
if cho == 1:
for record in records:
rem = df.iloc([i], [0])
print(rem)
sold1 = df.iloc([i], [1])
print(sold1)
sold2 = df.iloc([i], [2])
print(sold2)
rem = int(rem)
sold1 = int(sold1)
sold2 = int(sold2)
result = forecast(rem,sold1,sold2)
print(result)
df.set_value([i], [4], result)
pd.to_csv('test.csv')
print "Forecast Complete! Please check the file!"
else:
quit()
But i get this error:
Traceback (most recent call last):
File "tester.py", line 4, in <module>
import pandas
File "C:\Python27\lib\site-packages\pandas\__init__.py", line 25, in <module>
from pandas import hashtable, tslib, lib
File "pandas\src\numpy.pxd", line 157, in init pandas.hashtable (pandas\hashtable.c:40866)
ValueError: numpy.dtype has the wrong size, try recompiling. Expected 52, got 56
You will need to remove all previous installations of pandas and numpy because you installed them successively too many times making the libraries confused. After deleting them, reinstall pandas and numpy by using pip, like so:
pip install numpy
pip install pandas
Related
I am running my ML code and getting this error-
Enter website name=> www.google.com
Traceback (most recent call last):
File "Dphishing.py", line 12, in <module>
p2.category2(website)
File "C:\xampp\htdocs\Detect_Phishing_Website\p2.py", line 8, in category2
page = whois.whois(website)
AttributeError: module 'whois' has no attribute 'whois'
My code is:
# -*- coding: utf-8 -*-
import p1
import p2
import p3
import p4
import pandas as pd
#import numpy as np
website = str(input("Enter website name=> "))
p1.category1(website)
p2.category2(website)
p3.category3(website)
p4.category4(website)
read = pd.read_csv(r'C:\Users\Anushree\Desktop\college\4th year project\Detect_Phishing_Website\phishing5.txt',header = None,sep = ',')
read = read.iloc[:,:-1].values
dataset = pd.read_csv(r'C:\Users\Anushree\Desktop\college\4th year project\Detect_Phishing_Website\Training Dataset1.csv')
X = dataset.iloc[:,:-1].values
y = dataset.iloc[:,-1].values
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 0.2,random_state = 1001)
from sklearn.ensemble import RandomForestRegressor
regressor = RandomForestRegressor(n_estimators = 10,criterion = "mse",random_state = 2)
regressor.fit(X_train,y_train)
y_pred = regressor.predict(X_test)
from sklearn.model_selection import cross_val_score
accuracy = cross_val_score(estimator = regressor,X=X_train,y=y_train,cv = 5)
accuracy.mean()
accuracy.std()
Detect_phishing_website = regressor.predict(read)
if Detect_phishing_website == 1:
print("legitimate website")
elif Detect_phishing_website == 0:
print ('suspicious website')
else:
print('phishing website')
The code of file p2.py is:
import re
import whois
def category2(website):
file_obj = open(r'C:\Users\Anushree\Desktop\college\4th year project\Detect_Phishing_Website\phishing5.txt','a')
#8 Domain Registration Length
page = whois.whois(website)
if type(page.expiration_date) == list:
domain_reg_len = (page.expiration_date[0] - page.creation_date[0]).days
else:
domain_reg_len = (page.expiration_date - page.creation_date).days
#print domain_reg_len
if domain_reg_len <= 365:
file_obj.write('-1,')
else:
file_obj.write('1,')
#9 Using Non-Standard Port
match_port = re.search(':[//]+[a-z]+.[a-z0-9A-Z]+.[a-zA-Z]+:([0-9#]*)',website)
if match_port:
print (match_port.group())
if match_port.group(1) == '#':#represent multiple ports are active on url
file_obj.write('-1,')
else:
file_obj.write('1,')
else:
file_obj.write('1,')
file_obj.close()
I have already tried uninstalling whois and then reinstalling python-whois using the command pip install python-whois. But that hasn't helped with the error.
How can I understand what is going wrong, and how I can correct it?
Reason for your error:
You have not installed the whois command on your system.
Ubuntu: Use sudo apt install whois
Windows: Download and install from here
First uninstall any whois module with pip uninstall whois and pip uninstall python-whois
Solution 1: Use python-whois
Install python-whois with pip install python-whois
Then make sure you already installed the whois command on your machine.
Then your code should work.
Solution 2: Use whois
Install whois command on your machine. If you are on ubuntu sudo apt install whois will do.
Install whois module with pip install whois,
Then use whois.query() instead of whois.whois() in your code.
Source
just need some help troubleshooting an issue that I am having with the playsound module. I installed it via this command:
pip install playsound
I was told that it does not have dependencies at all, but am unable to use it in my code:
import numpy as np
import cv2
import random
from playsound import playsound
playsound('/home/pi/Documents/Rover/Sounds/StillThere.wav')
Ultimately, my goal is just to be able to import a sound into a code, have it play once after a print statement, and stop. As I am sure you can see, I attempted to fix the issue by installing vext, but that just added more issues. Please help me figure out what I am doing wrong, how I can fix it, and if there is another module that I can install that is easier for a n00b and more able to do what I am trying to, I read here that play-sound only plays the sound and does not stop playing the sound:
"https://stackoverflow.com/questions/57158779/stopping-audio-with-playsound-module?answertab=active#tab-top"
I also read that someone else had this issue, but had been using anaconda/miniconda3, not sure what that is but seemed vital to the solution and since I am not using it, thought I would post. I am thinking it has to do with installing playsound/gi while in the opencv working area (workon cv) Please post your answers in an ELI5-type way, so that I may understand. Thank you!
Error:
This is the error I get:
```Traceback (most recent call last):
File "Soundtest.py", line 13, in <module>
playsound('/home/pi/Documents/Rover/Sounds/StillThere.wav')
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/playsound.py", line 91, in _playsoundNix
import gi
File "/home/pi/.virtualenvs/cv/lib/python3.7/site-packages/vext/gatekeeper/__init__.py", line 204, in load_module
raise ImportError("No module named %s" % modulename)
ImportError: No module named gi```
You can use other module (sounddevice and soundfile). You should 'pip install sounddevice' and 'pip install soundfile' in uour cmd. Put your StillThere.wav file in your Python folder. If you want to thank me, just mark this post as answer please.
import sounddevice as sd
import soundfile as sf
data, fs = sf.read('StillThere.wav', dtype='float32')
# time (in seconds) of start an audio file
start = 0
# time (in seconds) of end an audio file
end = 10000
sd.play(data[fs * start : fs * end, :], fs)
status = sd.wait()
You can use Pyaudio module. You should 'pip install pyaudio' and 'pip install wave' in cmd. Also you should put your '.py' file and audio file in the same folder.
import pyaudio
import wave
filename = 'StillThere.wav'
chunk = 1024
wf = wave.open(filename, 'rb')
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(wf.getsampwidth()),
channels = wf.getnchannels(),
rate = wf.getframerate(),
output = True)
data = wf.readframes(chunk)
while data != '':
stream.write(data)
data = wf.readframes(chunk)
stream.close()
p.terminate()
I'm a new learner in data science. I havent find out why I got an attribute error. I used python 3.8.3 in Visual Studio Code. I installed Pandas in terminal (pip install Pandas). I dont know what the problem is. Any help will be appreciated.
import pandas as pd
df=pd.DataFrame()
print(df)
All I did was to create an empty dataframe. And I got that:
Traceback (most recent call last):
File "c:/Users/Fatma Elik/Documents/VS Code/BTK/Pandas_dataframe.py", line 20, in <module>
import pandas as pd
File "C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\__init__.py", line 180, in <module>
import pandas.testing
File "C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\testing.py", line 5, in <module>
from pandas._testing import (
File "C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\_testing.py", line 404, in <module>
RANDS_CHARS = np.array(list(string.ascii_letters + string.digits), dtype=(np.str_, 1))
AttributeError: module 'string' has no attribute 'ascii_letters'
Secondly I tried this instead and I got an attribute error again:
import pandas as pd
s1=pd.Series([3,2,0,1])
s2=pd.Series([0,3,7,2])
data=dict(apples=s1,oranges=s2)
df=pd.DataFrame(data)
print(df)
I did Ctrl+Click on string and I find that I already created a py file before. Because I searched on file search engine Windows 10 before, I couldnt have found it. Another simple mistake again :)
It's a little weird, the "string" module is a part of the standard library. Could you try this code?
from string import ascii_letters
print(ascii_letters)
Check it works or not, if it doesn't work, can you enter into this file:
"C:\Users\Fatma Elik\AppData\Local\Programs\Python\Python38-32\lib\string.py", and can you find:
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_letters = ascii_lowercase + ascii_uppercase
It should be located from line 25 to 27. If you can't find it, you should try to upgrade your python or reinstall it.
I am trying to run an example from ThinkStats2 code. I copied everything from github - the file structure are exactly the same as given on the github.
chap01soln.py
from __future__ import print_function
import numpy as np
import sys
import nsfg
import thinkstats2
def ReadFemResp(dct_file='2002FemResp.dct',
dat_file='2002FemResp.dat.gz',
nrows=None):
"""Reads the NSFG respondent data.
dct_file: string file name
dat_file: string file name
returns: DataFrame
"""
dct = thinkstats2.ReadStataDct(dct_file)
df = dct.ReadFixedWidth(dat_file, compression='gzip', nrows=nrows)
CleanFemResp(df)
return df
def CleanFemResp(df):
"""Recodes variables from the respondent frame.
df: DataFrame
"""
pass
def ValidatePregnum(resp):
"""Validate pregnum in the respondent file.
resp: respondent DataFrame
"""
# read the pregnancy frame
preg = nsfg.ReadFemPreg()
# make the map from caseid to list of pregnancy indices
preg_map = nsfg.MakePregMap(preg)
# iterate through the respondent pregnum series
for index, pregnum in resp.pregnum.iteritems():
caseid = resp.caseid[index]
indices = preg_map[caseid]
# check that pregnum from the respondent file equals
# the number of records in the pregnancy file
if len(indices) != pregnum:
print(caseid, len(indices), pregnum)
return False
return True
def main(script):
"""Tests the functions in this module.
script: string script name
"""
resp = ReadFemResp()
assert(len(resp) == 7643)
assert(resp.pregnum.value_counts()[1] == 1267)
assert(ValidatePregnum(resp))
print('%s: All tests passed.' % script)
if __name__ == '__main__':
main(*sys.argv)
I am getting ImportError like shown below:
Traceback (most recent call last):
File "C:\wamp\www\ThinkStats_py3\chap01soln.py", line 13, in <module>
import nsfg
File "C:\wamp\www\ThinkStats_py3\nsfg.py", line 14, in <module>
import thinkstats2
File "C:\wamp\www\ThinkStats_py3\thinkstats2.py", line 34, in <module>
import thinkplot
File "C:\wamp\www\ThinkStats_py3\thinkplot.py", line 11, in <module>
import matplotlib
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 105, in <module>
import six
ImportError: No module named 'six'
All the files are listed under the src folder. No packages under src. I tried adding packages for nsfg and thinkstats but another error appeared. I tried upgrading python 2.7 to python 3.4. I am still getting the same error. I know I need to install the six package for matplotlib but why am I getting import error on nsfg?
You are getting the import error on nsfg because it internally imports matplotlib (not directly, but it imports thinkstats2 , which imports thinkplot which imports matplotlib , which has a dependency on six module) . And that library is not installed on your computer, so the import fails.
Most probably you do not have six module , you can try installing it using - pip install six .
Or get it from here, unzip it and install it using - python setup.py install
I am using PyCharm (1.5.4) as my python IDE on MacOS 10.6.4. I am tinkering with some code to manipulate stock price data. As part of that I want to import price data from yahoo by using the DataReader function that comes with Pandas 0.6.0. The code is as follow:
http://www.statalgo.com/2011/09/08/pandas-getting-financial-data-from-yahoo-fred-etc/
from pandas import ols, DataFrame
from pandas.stats.moments import rolling_std
from pandas.io.data import DataReader
import datetime
sp500 = DataReader("^GSPC", "yahoo", start=datetime.datetime(1990, 1, 1))
sp500_returns = sp500["adj clos"].shift(-250)/sp500["adj clos"] - 1
gdp = DataReader("GDP", "fred", start=datetime.datetime(1990, 1, 1))["value"]
gdp_returns = (gdp/gdp.shift(1) - 1)
gdp_std = rolling_std(gdp_returns, 10)
gdp_standard = gdp_returns / gdp_std
gdp_on_sp = ols(y=sp500_returns, x=DataFrame({"gdp": gdp_standard}))
sp500.plot()
gdp.plot()
When I run the code I get the following error:
Traceback (most recent call last):
File "/Users/MyName/PycharmProjects/test/mytest", line 3, in <module>
from pandas.io.data import DataReader
ImportError: No module named data
I see that PyCharm does not know how to unresolve the reference 'data'.
My python paths are set as follows:
import sys
from pprint import pprint as pp
pp(sys.path)
['/private/var/folders/st/stQUFIfOG28bmpY9dCspTk+++TI/-Tmp-',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scikits.statsmodels-0.3.1-py2.7.egg',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python27.zip',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/plat-darwin',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/plat-mac',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/lib-tk',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/lib-old',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/lib-dynload',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/PIL']
What is puzzling is that PyCharm can resolve pandas.stats.moments but can't resolve pandas.io.data. I checked that both directories have the __init__.py file (the files are blank).
At this point I am not sure how to move forward. Greatly appreciate the help.
UPDATE:
$ cat __egginst__.txt
# egginst metadata
egg_name = 'pandas-0.3.0-3.egg'
prefix = '/Library/Frameworks/EPD64.framework/Versions/7.1'
installed_size = 1454562
rel_files = [
'EGG-INFO/pandas/__egginst__.txt',
'lib/python2.7/site-packages/pandas-0.3.0-3.egg-info',
Seems like deleting PyCharm's python interpreter configuration and re-configuring solved the problem. Strange... but fixed