Select to view content in your preferred language

Calculate field string into int and float => error

3197
6
Jump to solution
10-06-2019 08:11 AM
PanGIS
by
Regular Contributor

Hi,

I am running a model.

1. for field 1(string)  I have "integer numbers" and a value "nan" 

      I use field calc with the expression: !ACmodeString!.replace("nan","<Null>")

2. Once I have the null records I run  again Filed Calc to copy these values from the string field into a integer field with a expression like Field1(string) = Field2(integer)   [ I have tried both VB and Python]

a. with python I get this error:

ERROR 999999: Error executing function.
The value type is incompatible with the field type.

b. with python again

int(!acmodestring!)

erro:

ERROR 000539: Error running expression: int(u"<Null>")
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '<Null>'

c. with VB I get an alert:

If I click yes it finishes.

the same happens for a filed that needs to be converted in float.

I need to get rid of this alert.

The issue seems to be the null value.

Thanks!

0 Kudos
1 Solution

Accepted Solutions
PanGIS
by
Regular Contributor
6 Replies
DanPatterson_Retired
MVP Emeritus

nan does not exist for integers, only float values.  You should show the table that you are working with.  The field is either text or float

0 Kudos
PanGIS
by
Regular Contributor

Hi Dan, 

Thank you for your reply.

I did not explained myself well.

The original table has a field (string type) with numbers and some "nan" values.

When I say nan I mean the word "nan".

Since I need to convert this values into numbers I remove the word nan by using the python exp.: !ACmodeString!.replace("nan","<Null>").

Now I have that string field populated by numbers and <Null> records.

What I need to do is to  convert all these values into integer in a new field (short type).

I'll try to upload some data as soon as possible.

Here a not very nice example:

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Regarding

Since I need to convert this values into numbers I remove the word nan by using the python exp.: !ACmodeString!.replace("nan","<Null>").

The Python expression above hasn't changed "nan" to Null, it has changed "nan" to "<Null>".  What you have done is changed the string value to another string value with the characters that spell out Null, is isn't actually Null.  If you open the Select Layer By Attribute tool and query the table like, "field IS NULL" (use actual field name), you will see no records are selected.

Do you want the "nan" records to get converted and stay NULL, or do you want to eventually change them to some number like 0, -9999, etc....?

PanGIS
by
Regular Contributor

Thank you Joshua Bixby

I need to covert the first column into the last (string to numeric).

I attempted the Replace because I thought I could simplify it but, as you pointed out.....I did not.

I cannot convert them to 0 because it will false my analysis (they are temp and other values)

I need a NO VALUE

0 Kudos
PanGIS
by
Regular Contributor
curtvprice
MVP Esteemed Contributor

I found it hard to find the answer in the linked post, so for the good of the thread:

Here's a Python expression for Calculate Field to replace the string "nan" to null in a string field. This solution uses a very handy (but IMHO not terribly Pythonic) construct known as a ternary operator. Thanks Dan Patterson‌ for turning me on to these, they are very helpful when doing field calculations!

!ACmodeString! if !ACmodeString! == "nan" else None‍‍‍