0

I've created the shapefile and can manually enter in all the attributes to come in fine, but when I call it any other way (which I will need to do), the values for the floating types appear as NULL, even after converting to str or int.

When I print dato.iloc[0].tolist(), it spits out the first row of data as a list, and looks as follows: ['1', '1', '31isk', 4613427, 413588.12200000003, 1.19, 'sand'].

If I have feat.setAttributes(dato.iloc[0].tolist()), all values except for the three floats come in fine. If I print and copy the resulting string into feat.setAttributes, it will add the attribute perfectly okay. This is only a quikc fix and not possible when there are hundreds of more attributes to add.

So again, this does NOT work: dato.iloc[0].tolist()

But this DOES work: feat.setAttributes(['1', '1', '31isk', 4613427, 413588.12200000003, 1.19, 'sand'])

This is all done within PyCharm. Fields are as follows (Northing, Easting, and Altitude all appear NULL):

layerFields = QgsFields()
layerFields.append(QgsField('Transect', QVariant.String))
layerFields.append(QgsField('PointNumber', QVariant.String))
layerFields.append(QgsField('SubClass', QVariant.String))
layerFields.append(QgsField('Northing', QVariant.String))
layerFields.append(QgsField('Easting', QVariant.String))
layerFields.append(QgsField('Altitude', QVariant.String))
layerFields.append(QgsField('Notes', QVariant.String))

I've tried setting QVariant to int and converting the float values to ints but still nothinng.

5
  • Did you try set QVariant.Double for your QgsField Northing, Easting, Altitude ? Did you also tried to do [type(i) for i in dato.iloc[0].tolist()] to be sure of type of each element in your list (sometimes what you see is not what you visually think it is)? – ThomasG77 Jun 17 '20 at 20:29
  • @ThomasG77 Yes I started with QVariant.Double actually. I've manually checked all their types but the results were: <class 'str'> <class 'str'> <class 'str'> <class 'numpy.float64'> <class 'numpy.float64'> <class 'numpy.float64'> <class 'str'> do you think it has to do with it being 'numpy.float64'? – Barnard87 Jun 17 '20 at 21:41
  • Try using item method like described at stackoverflow.com/questions/9452775/… Normally, the following code [i.item() if isinstance(i, numpy.generic) else i for i in dato.iloc[0].tolist()] should work to cast all numpy objects to native Python types. I used in the previous expression numpy.generic but it can be np.generic depending if you've done numpy import with import numpy or import numpy as np – ThomasG77 Jun 17 '20 at 21:53
  • Conclusion? Did it works. Normally, with the native Python types instead of numpy, it should. – ThomasG77 Jun 18 '20 at 15:46
  • @ThomasG77 Yes! After doing a little research with the links you provided it managed to transform out of a numpy.float64 using .item() on the fields I needed to. Thanks so much for that pointer. – Barnard87 Jun 23 '20 at 18:15
0

as @ThomasG77 pointed out, calling .item() on the attributes that were not transferring fixed the issue

['Transect'].iloc[y], int(filter_dato['Point Number'].iloc[y]), filter_dato['Subclass'].iloc[y], \
                         filter_dato['Northing'].iloc[y].item(), filter_dato['Easting'].iloc[y].item(), filter_dato['Altitude'].iloc[y].item(), \
                         filter_dato['Notes'].iloc[y]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.