Select to view content in your preferred language

Deleting rows in attribute table with arcpy

977
3
11-09-2023 05:00 AM
Labels (2)
NadjaHertel
Occasional Contributor

Hi all, 

I am working with ArcGIS Pro 3.0.5 and I am using a python notebook for managing my layers. One layer contains streets (old ones which are out of date and actual ones). So in the attribute table, there is a field with the ToDate for each street segment. My goal is to delet all the old street segments, however, I struggel a bit with the task. 

So, following the script which I have so far:

 

 

# Loading the libraries
import arcpy
import os
import numpy as np
import datetime

# Referencing the current project
aprx = arcpy.mp.ArcGISProject('current')

# Rerencing the appropiate map
mp = aprx.listMaps('Map')[0]

# Referencing the individual map layers in different variables
CLayer = aprx.listMaps()[0].listLayers('C-line')
ALayer = aprx.listMaps()[0].listLayers('achsen')

# Referencint the table
clsTable = aprx.listMaps()[0].listTables('CLS_Table')

# Checking the data type
print(type(clsTable))
print(type(CLayer))
print(type(ALayer))

# Printing the first rows of the table - should work similar for the other variables
with arcpy.da.SearchCursor(clsTable[0], "*") as cursor:
    # Use slicing to get the first 5 rows
    first_five_rows = [row for i, row in enumerate(cursor)][:5]

# Print the first five rows
for row in first_five_rows:
    print(row)

 

 

So far, the script is working. But now it starts:

 

 

# Creating a variable for the current date
cur_date = datetime.datetime.today()

# Cleaning the layer
where_clause1 = """ToDate < '{}'""".format(cur_date.strftime('%Y-%m-%d'))

# Selecting the rows
arcpy.Select_analysis(ALayer, "select_past_dates", where_clause1)

# Deleting the selected rows
arcpy.DeleteFeatures_management("select_past_dates")

 

 

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
In  [8]:
Line 5:     arcpy.Select_analysis(ALayer, "select_past_dates", where_clause1)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\analysis.py, in Select:
Line 138:   raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\analysis.py, in Select:
Line 135:   retval = convertArcObjectToPythonObject(gp.Select_analysis(*gp_fixargs((in_features, out_feature_class, where_clause), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

RuntimeError: Object: Error in executing tool
---------------------------------------------------------------------------

 

And I also tried it this way:

 

 

cursor = arcpy.da.UpdateCursor(ALayer, ["ToDate"])
for row in cursor:
    if row[0] < cur_date:
        cursor.deletRow()
cursor.close()

 

 

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
In  [13]:
Line 1:     cursor = arcpy.da.UpdateCursor(ALayer, ["ToDate"])

RuntimeError: 'in_table' is not a table or a featureclass
---------------------------------------------------------------------------

 

It would be great if someone could help me.

 

Thanks in advance,

Nadja

 

 

 

 

0 Kudos
3 Replies
DuncanHornby
MVP Notable Contributor

You are using the wrong Select tool, you want to be using the Select by Attributes tool.

0 Kudos
NadjaHertel
Occasional Contributor

Thanks for your reply 🙂 I haven't thought about that, thanks! However, I still get an error...

Did you meant something like the one below?

 

arcpy.management.SelectLayerByAttribute(ALayer, "NEW_SELECTION","ToDate < CURRENT_DATE()")

 

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
In  [58]:
Line 1:     arcpy.management.SelectLayerByAttribute(achsenLayer, "NEW_SELECTION","ToDate < CURRENT_DATE()")

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in SelectLayerByAttribute:
Line 10780: raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py, in SelectLayerByAttribute:
Line 10777: retval = convertArcObjectToPythonObject(gp.SelectLayerByAttribute_management(*gp_fixargs((in_layer_or_view, selection_type, where_clause, invert_where_clause), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py, in <lambda>:
Line 512:   return lambda *args: val(*gp_fixargs(args, True))

RuntimeError: Object: Error in executing tool
---------------------------------------------------------------------------

 

 

 

 

0 Kudos
DuncanHornby
MVP Notable Contributor

Yes that's the correct tool, but your where clause is incorrect ToDate is not being referenced correctly. Read the help for this tool and explore the samples to understand how to build the clause correctly.

 

0 Kudos