-1

I have many variables which are of type string but contain in fact numbers. In QGIS you can convert those strings to numbers by creating a new variable with the wanted numeric type (e.g. integer or real etc) and then just update the attribute using the Field Calculator as shown in the image (the variable 'PL1D_r' is an empty attribute of type 'real', the variable 'PL1D_str' is of type string).

However, this always requires to first create a new additional variable. So you always end up with two variables, for each variable you want to convert.

Is there a way to do this for multiple attributes 'in place', so really 'transform' the relevant string-variable to a numeric data type within QGIS?

enter image description here

Taras
  • 32,823
  • 4
  • 66
  • 137
i.i.k.
  • 1,427
  • 5
  • 11
  • To clarify, by "variable" are you referring to a field? Furthermore, your question title specifies PyQGIS but your question appears to be about the Field Calculator. – Matt Jun 22 '23 at 14:54
  • Hi @Matt, yes, I refer to fields when I say 'variable'. I'm always a bit confused, as a field may also be an attribute, a column ... I just refer to the field calculator, as I wanted to provide more background information, so how to do it in QGIS manually, per single field. – i.i.k. Jun 29 '23 at 13:40

1 Answers1

4

No, you cannot change the type of field. This is simply not possible.

But you could create a new field and delete the old one.

Another option is to create a new layer, a copy of your original layer, with the same fields, but different field types. If you want to do this, I suggest the "Refactor fields" tool in the Processing Toolbox (Ctrl+Alt+T).

Taras
  • 32,823
  • 4
  • 66
  • 137
MrXsquared
  • 34,292
  • 21
  • 67
  • 117