How to test if a date is null

8257
6
01-15-2015 05:01 AM
DanEvans
Occasional Contributor II

I am writing a python script geoprocessing tool and need to use an UpdateCursor to populate a text field with either 'Y' or 'N' depending on whether a date field is null or not. Here is my code:

with arcpy.da.UpdateCursor(arcpy.env.scratchGDB + r"/cn42n_table", (cn42n_status_field_validated, cn42n_cancel_date_field_validated, "CANC", "PARC")) as cursor:
  for row in cursor:
       if "CANC" in row[0] or not row[1] is None:
            row[2] = "Y"
       else:
            row[2] = "N"

       if "PARC" in row[0]:
            row[3] = "Y"
       else:
            row[3] = "N"

I have tried several ways of checking if row[1] (my date field) is not null but whatever I try the 'CANC' field is populated with a 'Y'.

Could someone please tell me the correct way to do this?

Thanks

Dan

Tags (4)
0 Kudos
6 Replies
Luis_ÁngelPascual_Camino
New Contributor III

Hi Dan.

I'm not able to test it right now but, have you tried this?

if "CANC" in row[0] or not row[1] == '#'

I hope it helps.

Luis

0 Kudos
TimDine
Occasional Contributor II

if "CANC" is in row[0], row[2] will always equal "Y" because row[0] = "CANC" or (something that doesn't matter, first option is already true).  The only way that row[2] could = "N" is if row[0] is not "CANC" and row[1] has a date in it.

I think I've got my Y/N mixed up in the samples below, but these should give results similar to what you're looking for.  You've got your checking for a date code right and the script is doing what you're asking, it's your AND/OR logic that looks wonky.

##------------------------

if "CANC" in row[0] and row[1] is None:

     row[2] = "Y" 

elif "CANC" in row[0] and not (row[1] is None):

     row[2] = "N"

else:

     print "Probably PARC"

##---------------------------

if "CANC" in row[0]:

     if row[1] is None:

          row[2] = "Y"

     else:

          row[2] = "N"

0 Kudos
DanEvans
Occasional Contributor II

Hi Tim,

Thanks for the suggestion. I don't think it's quite right for what I want though. Let me explain:

The dataset I'm working on is a table representing projects. One row in that table is one project. When a project is cancelled, the status has to be changed to include the string "CANC", and the cancel date field should be filled in. Unfortunately people sometimes forget to do one or the other, so I want to check if EITHER the Status field contains the string "CANC" OR the cancel date has a date in (is not null) OR both.

row[0] is the status field

row[1] is the cancel date fields

row[2] is the CANC field (Y/N)

row[3] is the PARC field (Y/N)

'PARC' is just another status meaning that the project is archived - that field gets populated correctly but the CANC field is all Y's, regardless of whether row[0] contains "CANC" or whether row[1] contains a date or is null. It's as if python is not recognising the nulls as None.

Dan

0 Kudos
DanEvans
Occasional Contributor II
It's as if python is not recognising the nulls as None.

I have confirmed this by commenting out the first part so it's just:

if not row[1] is None:
     row[2] = "Y"
else:
     row[2] = "N"

And still the result is 'Y' for everything...

How do date fields come across into Python? Are they datetime objects? Whatever type python sees them as, could it be converting the nulls to some other value (January 1st 1970 or something?)

Dan

0 Kudos
Zeke
by
Regular Contributor III

Well, without going through everything, you could try testing if len(row[1]) < 1. You could also try printing row[1] in the loop. see what values it's finding. Or enclose row[1] is None in (), or even try Null instead of None, although I don't expect that to work.

If nothing else, these methods might give you some clue as to what's going on.

0 Kudos
DanEvans
Occasional Contributor II

Ah! The problem was that the field parameter I was passing to ValidateFieldName was the status field again, not the cancel field!

Thanks for the help anyway...

Dan

0 Kudos