numpy.loadtxt does not read file with complex numbers - python

I am trying to read a file with complex numbers in the form :
data.dat
1.5795219122457646E-11-3.852906516379872E-15i -3.5949335665378405E-12-1.626143709108086E-15i
-6.720365121161621E-15-5.377186331212649E-17i -3.736251476362349E-15-3.0190920417856674E-17i
I use the following code to read the file :
import numpy as np
c_complex = np.loadtxt('data.dat', delimiter='\t', dtype=np.complex128)
But it gives me the following error :
TypeError: complex() argument must be a string or a number, not 'bytes'
What could I do to solve this problem ?
Thank you very much for your help

This seems to have been a bug in older versions of numpy (Issue). Either update your numpy to the latest version of their github repository or use the function numpy.genfromtxt().
c.complex = np.genfromtxt('data.dat', delimiter='\t', dtype=np.complex128)

Related

TypeError: NumPy Text File Import

Text File
I'm trying to import the text file above using the following code:
import numpy as np
Climate_data =np.genfromtxt('C:\\Users\\vishv\\Desktop\\Stats With Python\\Stats_with_python\\CSV Files\\Climate Data.txt', delimiter= ',', skip_header='1')
print(Climate_data)
But it gives me the following TypeError:'str' object cannot be interpreted as an integer
Any ideas?
You are passing skip_header as a string, it should be an integer (skip_header=1).
Also, please don't post (even parts of) code as images, it makes it harder to help.

NumPy Reads Binary Data Incorrectly?

I'm working with a binary file "uint64.bin" whose entire contents can be represented by: 0x0f2d1e6002df9000
My python code is as follows:
import numpy as np
import pandas as pd
my_dtype = np.dtype([('mytag','>u8')])
with open("uint64.bin", 'rb') as fh:
data = np.fromfile(file=fh, dtype=my_dtype)
df = pd.DataFrame(data, columns=data.dtype.names)
print(df.get_values()[0])
What prints is 1093563682234798080 whereas the output should be 1093563682234798100 (the difference is in the 0x14 bits). What's going on?
I'm on Windows 64-bit and using Python 3.7.
Just do print(0x0f2d1e6002df9000) in Python. It gives you:
1093563682234798080
So the answer you're getting is correct, your assumption about what it should be is incorrect.

csv file overwritten instead of appended in python [duplicate]

I am trying to append data to a file using numpy's savetxt function. Below is the minimum working example
#!/usr/bin/env python3
import numpy as np
f=open('asd.dat','a')
for iind in range(4):
a=np.random.rand(10,10)
np.savetxt(f,a)
f.close()
The error that I got is something about the type of the error
File "/usr/lib/python3/dist-packages/numpy/lib/npyio.py", line 1073,
in savetxt
fh.write(asbytes(format % tuple(row) + newline)) TypeError: must be str, not bytes
This error doesn't occur in python2 so I am wondering what the issue could be. Can anyone help me out?
You should open file by binary mode.
#!/usr/bin/env python3
import numpy as np
f=open('asd.dat','ab')
for iind in range(4):
a=np.random.rand(10,10)
np.savetxt(f,a)
f.close()
reference:
python - How to write a numpy array to a csv file? - Stack Overflow How to write a numpy array to a csv file?

talib ADX function error

Using python pandas dataframe (d) downloaded from yahoo finance which has the format:
Date,Open,High,Low,Close,Volume,Adj Close
2015-01-13,1.290,1.290,1.200,1.225,421600,1.225
I can successfully use talib functions like this:
talib.abstract.EMA(d, timeperiod=8, price='Close')
talib.abstract.SMA(d, timeperiod=13, price='Close')
talib.abstract.RSI(d, timeperiod=25, price='Close')
From the documentation (http://mrjbq7.github.io/ta-lib/func_groups/momentum_indicators.html) they take the form:
real = XYZ(close, timeperiod=14)
However when trying to use the talib functions with the form:
real = XYZ(high, low, close, timeperiod=14) such as the ADX I cant figure out the correct syntax needed.
I've tried this:
talib.abstract.ADX(d,high='High',low='Low',close='Close',Timeperiod=10)
Exception: input_arrays parameter missing required data keys: high, low, close
and this:
talib.abstract.ADX(high=d.High,low=d.Low,close=d.Close,Timeperiod=10)
TypeError: only length-1 arrays can be converted to Python scalars
Any ideas on the correct format for the parameters needed for this and the other talib python wrappers that have more than one input parameter ?
Any help on the correct format would be greatly appreciated !!!
Thanks in advance
Depends on the shape of your arrays in some cases. If you really need the function as a matter of urgency, just call it from the library:
import talib
import numpy as np
h = np.array(high)
l = np.array(low)
c = np.array(close)
output_atr = np.array(talib.ATR(h,l,c,14))
This works fine.

Construct 1 dimensional array from several data files

I'm trying to code a small Python script to do some data analysis. I have several data files, each one being a single column of data, I know how to import each one in python using numpy.loadtxt giving me back a ndarray. But I can't figure out how to concatenate these ndarrays, numpy.concatenate or numpy.append always giving me back error messages even if I try to flatten them first.
Are you aware of a solution?
Ok, as you were asking for code and data details. Here is what my data file look like:
1.4533423
1.3709900
1.7832323
...
Just a column of float numbers, I have no problem import a single file using:
data = numpy.loadtxt("data_filename")
My code trying to concatenate the arrays looks like that now (after trying numpy.concatenate and numpy.append I'm now trying numpy.insert ) :
data = numpy.zeros(0) #creating an empty first array that will be incremented by each file after
for filename in sys.argv[1:]:
temp = numpy.loadtxt(filename)
numpy.insert(data, numpy.arange(len(temp), temp))
I'm passing the filenames when running my script with:
./my_script.py ALL_THE_DATAFILES
And the error message I get is:
TypeError: only length-1 arrays can be converted to Python scalars
numpy.concatenate will definitely be a valid choice - without sample data and sample code and corresponding error messages we cannot help further.
Alternatives would be numpy.r_, numpy.s_
EDIT
This code snippet:
import sys
import numpy as np
filenames = sys.argv[1:]
arrays = [np.loadtxt(filename) for filename in filenames]
final_array = np.concatenate(arrays, axis=0)

Categories