text file mdates.strpdate2num error - python

I keep getting an error using the numpy loadtxt converter.
Your help is greatly appreciated
import numpy as np
import time
import datetime
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
from matplotlib.finance import candlestick
from matplotlib.dates import strpdate2num
import urllib2
## global variables
eachStock = 'AAPL','GOOG','MSFT','AMZN','CMG'
for stock in eachStock:
stockFile = stock+'.txt'
date, closep, highp, lowp, openp, volume = np.loadtxt(eachStock, delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})
dFrame = Series(closep)
here is the first line in my text file
20040322,13.5200,13.6800,12.6100,12.6850,15850720
here is the error I keep getting
Traceback (most recent call last):
File "C:\Users\antoniozeus\Desktop\BuyAndHold.py", line 27, in <module>
converters={ 0: mdates.strpdate2num('%Y%m%d')})
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 796, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 233, in __call__
return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))
File "C:\Python27\lib\_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "C:\Python27\lib\_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data 'AAPL' does not match format '%Y%m%d'

It seems like you mistyped stockFile (filename) as eachStock.
date, closep, highp, lowp, openp, volume = np.loadtxt(
stockFile, delimiter=',', unpack=True,
converters={ 0: mdates.strpdate2num('%Y%m%d')})

Related

Convert js Date().toISOString() to Python datetime

The format of my csv file is this
Zeitstempel;Iteration;lag
"2022-01-26T22:28:11.347Z","1","2"
"2022-01-26T22:28:11.348Z","2","1"
and my python code is this
import pandas as pd
import matplotlib.pyplot as plt
import datetime as dt
csv_data = 'lag.log'
df = pd.read_csv(csv_data, encoding='latin1', sep=',', header=1,
names=['Zeitstempel', 'Iteration', 'Lag'])
df_cleaned = df.dropna()
x = df_cleaned['Zeitstempel'].values
y = df_cleaned['Lag'].values
#2022-01-26T21:59:30.810Z
dates = [dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.{0}Z").date() for date in x]
plt.plot_date(dates, y)
plt.show()
and the console output is this
Traceback (most recent call last):
File "chart.py", line 19, in <module>
dates = [dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.{0}Z").date() for date in x]
File "chart.py", line 19, in <listcomp>
dates = [dt.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S.{0}Z").date() for date in x]
File "/usr/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
tt, fraction, gmtoff_fraction = _strptime(data_string, format)
File "/usr/lib/python3.8/_strptime.py", line 349, in _strptime
raise ValueError("time data %r does not match format %r" %
ValueError: time data '2022-01-26T22:28:11.348Z' does not match format '%Y-%m-%dT%H:%M:%S.{0}Z'
Is there a way to convert the iso string to a datetime
Python's datetime.fromisoformat does not accept the 'Z' suffix, so you'll need to remove it.
from datetime import datetime
def datetime_from_js_isoformat(string: str) -> datetime:
"""Creates a datetime object from a JavaScript ISO format string."""
if string.endswith('Z'):
return datetime.fromisoformat(string[:-1])
return datetime.fromisoformat(string)

error in plot graph using matlpotlib.pyplot module([ValueError: x and y must have same first dimension, but have shapes (2711, 4) and (678,)]

import pandas as pd
import quandl
import math
import numpy as np
from sklearn import preprocessing, model_selection,svm
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
quandl.ApiConfig.api_key = "FVeuw21FAe86ux3J3ePr"
df=quandl.get("WIKI/GOOGL")
df=df[['Adj. Open','Adj. High','Adj. Low','Adj. Close','Adj. Volume']]
df['HL_PCT']=(df['Adj. High']-df['Adj. Close'])/df['Adj. Close']*100
df['DL_PCT']=(df['Adj. Close']-df['Adj. Open'])/df['Adj. Open']*100
df=df[['Adj. Close','HL_PCT','DL_PCT','Adj. Volume']]
forcast_col='Adj. Close'
df.fillna(-9999,inplace=True)
forcast_out=int(math.ceil(0.01*len(df)))
print(forcast_out)
df['label']=df[forcast_col].shift(-forcast_out)
df.dropna(inplace=True)
X=np.array(df.drop(['label'],1))
y=np.array(df['label'])
X=preprocessing.scale(X)
y=np.array(df['label'])
print (len(X),len(y))
X.shape[0]!=y.shape[0]
X_train,X_text,y_test,y_train=train_test_split(X,y,test_size=0.2)
plt.xlabel('area(m^2)')
plt.ylabel('price(Rs)')
plt.plot(X_train,y_train,color='blue',marker='.')
plt.show()
reg=LinearRegression()
reg.fit(X_train,y_train)
accuracy=reg.score(X_test,y_test)
print(accuracy)
the error i got while running this code
Traceback (most recent call last):
File "c:/Users/user/Desktop/projetcs/machine learning/mc1.py", line 31, in <module>
plt.plot(X_train,y_train,color='blue',marker='.')
File "C:\Users\user\python11\lib\site-packages\matplotlib\pyplot.py", line 2824, in plot
return gca().plot(
File "C:\Users\user\python11\lib\site-packages\matplotlib\axes\_axes.py", line 1743, in plot
lines = [*self._get_lines(*args, data=data, **kwargs)]
File "C:\Users\user\python11\lib\site-packages\matplotlib\axes\_base.py", line 273, in __call__
yield from self._plot_args(this, kwargs)
File "C:\Users\user\python11\lib\site-packages\matplotlib\axes\_base.py", line 399, in _plot_args
raise ValueError(f"x and y must have same first dimension, but "
ValueError: x and y must have same first dimension,

ValueError using np.loadtxt

I am trying to extract data from a txt file with numpy and I get this big error:
Traceback (most recent call last):
File "C:\Python36\machine learning\bayes_classifier.py", line 14, in
<module>
data = np.loadtxt(input_file, delimiter=",")
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 1092, in loadtxt
for x in read_data(_loadtxt_chunksize):
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 1019, in read_data
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 1019, in <listcomp>
items = [conv(val) for (conv, val) in zip(converters, vals)]
File "C:\Python36\lib\site-packages\numpy\lib\npyio.py", line 738, in floatconv
return float(x)
ValueError: could not convert string to float: '2.18'
notice the ValueError, what is the '2.18'?
Here is the full code:
### Naïve bayes classifier ###
import numpy as np
import matplotlib.pyplot as plt
from sklearn.naive_bayes import GaussianNB
from sklearn import cross_validation
from utilities import visualize_classifier
# Input file containing data
input_file = "data_multivar_nb.txt"
# Load data from input file
data = np.loadtxt(input_file, delimiter=",")
x, y = data[:, :-1], data[:, -1]
and here is the file:
file link here

Convert CSV to PNG with matplotlib Issue

I am trying to create a PNG image with some CSV data but I am getting an error related to the date column (meanwhile converted to list). The error is:
Traceback (most recent call last):
File "C:/Users/user1/Desktop/Py/AgentsStatus/testGraph.py", line 57, in <module>
plt.plot(dateCol,okCol,linewidth=5)
File "C:\Python34\lib\site-packages\matplotlib\pyplot.py", line 3154, in plot
ret = ax.plot(*args, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\__init__.py", line 1812, in inner
return func(ax, *args, **kwargs)
File "C:\Python34\lib\site-packages\matplotlib\axes\_axes.py", line 1425, in plot
self.add_line(line)
File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 1708, in add_line
self._update_line_limits(line)
File "C:\Python34\lib\site-packages\matplotlib\axes\_base.py", line 1730, in _update_line_limits
path = line.get_path()
File "C:\Python34\lib\site-packages\matplotlib\lines.py", line 925, in get_path
self.recache()
File "C:\Python34\lib\site-packages\matplotlib\lines.py", line 612, in recache
x = np.asarray(xconv, np.float_)
File "C:\Python34\lib\site-packages\numpy\core\numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: '11-04-2016'
CSV contains:
11-04-2016;37180;6;23852
18-04-2016;37341;9;24105
25-04-2016;37075;18;23788
My code is:
import csv
import matplotlib.pyplot as plt
import os
path = 'C:\\Users\\user1\\Desktop\\Py\\AgentsStatus\\data.csv'
with open (path) as csvfile:
readCSV = csv.reader(csvfile, delimiter=';')
dateCol = [] # date list
for row in readCSV:
if row:
date0 = row[0]
dateCol.append(date0)
with open (path) as csvfile:
readCSV = csv.reader(csvfile, delimiter=';')
okCol = [] # all agents list
for row in readCSV:
if row:
ok0 = row[1]
okCol.append(ok0)
with open (path) as csvfile:
readCSV = csv.reader(csvfile, delimiter=';')
heaCol = [] # healthy list
from matplotlib import pyplot as plt
from matplotlib import style
style.use('ggplot')
# can plot specifically, after just showing the defaults:
plt.plot(dateCol,okCol,linewidth=5)
plt.plot(dateCol,heaCol,linewidth=5)
plt.title('Epic Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()
My goal is to create something like below:
Could you give me please some tips of what am I doing wrong? I think my problem mitght be that I am not setting the List dateCol as the Index for the graph (to not plot it). Could you please help me on that?
Thanks a lot.
For stuff like this Pandas is unbeatable:
import pandas
import matplotlib.pyplot as plt
df = pandas.read_csv('sampledata.csv', delimiter=';',
index_col=0,
parse_dates=[0], dayfirst=True,
names=['date','a','b','c'])
df.plot()
plt.savefig('sampledata.png')
Results in

Key error & Pandas

I wrote a Python script (below) which load data from a text file (using pandas) and checks the values in the columns.
import sys
import pandas as pd
import numpy as np
from numpy import ndarray
import math
import matplotlib.pyplot as plt
from matplotlib.pyplot import *
from skimage import data
from skimage.feature import match_template
if __name__ == '__main__':
data = pd.read_csv('Fe_PSI_spt_refined.txt', sep=" ", header = None)
data.columns = ["Angle_number", "Omega", "Intensity", "X", "Y", "Address", "ID"]#, "flag"]
Number_of_projections = 181
Number_of_lines_in_txt = 3493
numrows = len(data)
counter_array = []
correlation_threshold_value = 0.7
a = np.zeros(Number_of_lines_in_txt)
output_file = ("output.txt")
for i in range(2, (Number_of_projections + 1)):
filename_cutouts_combined = ("cutouts_combined_%03i.txt" % (i))
filename_cutouts_combined_tag = ("cutouts_combined_tag_%03i.txt" % (i))
image = np.loadtxt(filename_cutouts_combined)
image_tagged = np.loadtxt(filename_cutouts_combined_tag)
for j in range(0, Number_of_lines_in_txt - 1):
print data.Angle_number[j], i
After one iteration of j I get the error below. Do you spot any error I should fix? Thanks
`Traceback (most recent call last):
File "Hyperbola_search.py", line 46, in <module>
print data.Angle_number[j], i
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 491, in __getitem__
result = self.index.get_value(self, key)
File "/Users/Alberto/anaconda/lib/python2.7/site-packages/pandas/core/index.py", line 1032, in get_value
return self._engine.get_value(s, k)
File "index.pyx", line 97, in pandas.index.IndexEngine.get_value (pandas/index.c:2661)
File "index.pyx", line 105, in pandas.index.IndexEngine.get_value (pandas/index.c:2476)
File "index.pyx", line 149, in pandas.index.IndexEngine.get_loc (pandas/index.c:3215)
File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6450)
File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6394)
KeyError: 3491`
You load files into image and image_tagged, while a remains unused.
I don't know what data.Angle_number and numrows are, but they appear to be from libraries, not related to your files.

Categories

Resources