I was wondering why I get the below error when using numpy.int64 with numpy.timedelta64
ValueError: Could not convert object to NumPy timedelta
For example:
In [10]: np.timedelta64(np.int64(2),'D')
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-10-06ccd44f066c> in <module>()
----> 1 np.timedelta64(np.int64(2),'D')
ValueError: Could not convert object to NumPy timedelta
The reason for my question: I am using scipy.stats.mode and pass the result into numpy.timedelta64 in the interim I do the below int as a fix but interested to find out why such behaviour occurs:
from scipy.stats import mode
import nump as np
np.timedelta64(int(mode([2,2,3,4,1,1,5,5])[0][0]),'D')
Out[15]: numpy.timedelta64(1,'D')
Might be a version issue ?
numpy version = 1.10.4
python version = 2.7.11 (64 bit)
Related
Why am I having this error
import numpy as np
np.array([1,2,3,4])
NameError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_2304/609253908.py in
----> 1 np.array([1,2,3,4])
NameError: name 'np' is not defined
Your error code says that np.array([1, 2, 3, 4]) is in line 1 so you are trying to use it before importing, you need to import numpy first, as shown in your question.
You can just go with numpy
import numpy
numpy.array([1,2,3,4])
Have you installed or updated numpy?
Try this once in your terminal.
>>> pip install numpy
I am trying to open a netcdf file using rioarray:
import rioxarray
import xarray
import raster
xds = rioxarray.open_rasterio(file, crs='+proj=latlong', masked=True)
but:
type(xds)
list
and xds has none of the attributes or methods of an xarray.
xds_lonlat = xds.rio.reproject("epsg:4326")
AttributeError Traceback (most recent call last)
in
----> 1 xds_lonlat = xds.rio.reproject("epsg:4326")
AttributeError: 'list' object has no attribute 'rio'
clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)
AttributeError Traceback (most recent call last)
in
----> 1 clipped = xds.rio.clip(mask.geometry, mask.crs, drop=False, invert=True)
AttributeError: 'list' object has no attribute 'rio'
Can anyone advise?
I recently encountered this when I was opening a netCDF (with rioxarray) that had multiple variables. Since it returns a list, you would not expect it to have any of the rioxarray attributes or methods.
The documentation for the function is here: https://corteva.github.io/rioxarray/stable/rioxarray.html
One of the return types is List[xarray.Dataset], so I think this behavior is expected.
My guess is that you want one of the entries in the list like xds=xds[0], though it's hard to know without having more information about the file that you are opening.
import glob
from os.path import join
import yt
from yt.config import ytcfg
path = ytcfg.get("yt", "test_data_dir")
from mpl_toolkits.mplot3d import Axes3D
my_fns = glob.glob(join(path, "Orbit", "puredef_hdf5_chk_000000"))
my_fns.sort()
fields = ["particle_velocity_x", "particle_velocity_y", "particle_velocity_z"]
ds = yt.load(my_fns[:])
dd = ds.all_data()
indices = dd["particle_index"].astype("int")
print (indices)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-27-1bae40a7b7ba> in <module>
1 ds = yt.load(my_fns[:])
----> 2 dd = ds.all_data()
3 indices = dd["particle_index"].astype("int")
4 print (indices)
AttributeError: 'DatasetSeries' object has no attribute 'all_data'
I have looked at other posts on here, but many of them deal with different aspects of this error that deals with lens or other statements.
I had exactly the same error recently, with a very similar code. First of all, a mistake I did was giving the code the symbolic links to the real data files, while it should work directly with the data.
Another issue was a problem with the installation of the yt library, version 3.6.1. I had installed it using the pip command, but it wasn't working well, so I uninstalled it and I used the "all-in-one" script they provide on their homepage.
Fixing these two things together solved completely this problem.
import tensorflow as tf
print(tf.ones([10, 10]))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-bc8c707e6655> in <module>
----> 1 print(tf.ones([10, 10]))
AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'ones'
First time giving proper answer...but when executing after 5 minutes same COMMAND giving error.
That error can occur due to changes in tensorflow version... As I understood from your comment, you're using tensorflow 2.0 and to print the tensor values you need to use tf.print just like so:
import tensorflow as tf
tf.print(tf.ones([10, 10]))
Hope this answers your question
Here are two lines of code for the purpose of generating a random permutation of size 4:
from numpy import random
t = random.permutation(4)
This can be executed in Python, but not sage, which gives the following error:
TypeError Traceback (most recent call last)
<ipython-input-3-033ef4665637> in <module>()
1 from numpy import random
----> 2 t = random.permutation(Integer(4))
mtrand.pyx in mtrand.RandomState.permutation (numpy/random/mtrand/mtrand.c:34842)()
mtrand.pyx in mtrand.RandomState.shuffle (numpy/random/mtrand/mtrand.c:33796)()
TypeError: len() of unsized object
Why?
A bit more details: I executed the code in Python 3, and the mtrand is also in the Python 3 directory, which should rule out the possibility that sage is calling Python 2 version of the numpy.
To escape Sage's preparser, you can also append the letter r (for "raw") to numerical input.
from numpy import random
t = random.permutation(4r)
The advantage of 4r over int(4) is that 4r bypasses the
preparser, while int(4) is preparsed as int(Integer(4)) so that
the Python integer is transformed into a Sage integer and then
transformed back into a Python integer.
In the same way, 1.5r will give you a pure Python float rather than
a Sage "real number".
The reason this doesn't work in Sage is that Sage preparses its input, turning "4" from a Python int to a Sage Integer. In Sage, this will work:
from numpy import random
t = random.permutation(int(4))
Or you can turn the preparser off:
preparser(False)
t = random.permutation(4)