Select to view content in your preferred language

How to get all dates up to a certain date in python code

865
2
Jump to solution
07-27-2023 03:52 AM
ZainAhmad
New Contributor

Hi guys, I am working on a python script and trying to filter fields having date before 1950 and putting in the count in a column as I did the number of zeroes, nulls and unknowns  that is working perfectly except the date column one. can you please help me where I am not paying attention to make code working. 

my code is as below

import arcpy
import pandas as pd
from arcgis.features import GeoAccessor
from sys import argv
from datetime import date

# Set the workspace to your geodatabase
geodatabase = r"Z:\path"
arcpy.env.workspace = geodatabase
# Specify the feature class to analyze
feature_class = "Shape_file"

# Get a list of all fields in the feature class
fields = arcpy.ListFields(feature_class)

# Create an empty list to store the results
results = []

# Iterate through each field
for field in fields:
field_name = field.name
field_type = field.type



# Count null and zero values using SearchCursor
null_count = 0
zero_count = 0
unknowns = 0
Default_Date = 0
with arcpy.da.SearchCursor(feature_class, [field_name]) as cursor:
for row in cursor:
if row[0] is None:
null_count += 1
elif field.type in ["Double", "Float", "Integer", "SmallInteger"] and row[0] == 0:
zero_count += 1
elif row[0] == 'unknown':
unknowns += 1
elif field.type in ["Date"] and row[0] <="1950-12-29":
Default_Date += 1

# Append the results to the list
results.append({
"Field": field_name,
"Type": field_type,
"Nulls": null_count,
"Zeroes": zero_count,
"Unknowns": unknowns,
"Default Date": Default_Date})

# Create a pandas DataFrame from the results
df = pd.DataFrame(results)

# Display the DataFrame as a table in the notebook
display(df)

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

To post formatted code:

JohannesLindner_0-1689266678674.pngJohannesLindner_1-1689266693900.png

 

SearchCursor reads values from date fields as datetime.datetime objects, so you can compare to that.

import arcpy
import datetime
import pandas as pd


def analyze(in_table):
    fields = arcpy.ListFields(in_table)
    result = [
        {"Field": f.name, "Type": f.type, "Nulls": 0, "Zeroes": 0, "Unknowns": 0, "DefaultDate": 0}
        for f in fields
    ]
    with arcpy.da.SearchCursor(in_table, [f.name for f in fields]) as cursor:
        for row in cursor:
            for i, val in enumerate(row):
                if val is None:
                    result[i]["Nulls"] += 1
                elif val == 0:
                    result[i]["Zeroes"] += 1
                elif val == "unknown":
                    result[i]["Unknowns"] += 1
                elif result[i]["Type"] == "Date" and val <= datetime.datetime(1950, 12, 29):
                    result[i]["DefaultDate"] += 1
    df = pd.DataFrame(result)
    return df

display(analyze("G:/ArcGIS/data/Arcade_Test_Data.gdb/TestPoints"))

 

JohannesLindner_0-1690457547979.png

 


Have a great day!
Johannes

View solution in original post

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

To post formatted code:

JohannesLindner_0-1689266678674.pngJohannesLindner_1-1689266693900.png

 

SearchCursor reads values from date fields as datetime.datetime objects, so you can compare to that.

import arcpy
import datetime
import pandas as pd


def analyze(in_table):
    fields = arcpy.ListFields(in_table)
    result = [
        {"Field": f.name, "Type": f.type, "Nulls": 0, "Zeroes": 0, "Unknowns": 0, "DefaultDate": 0}
        for f in fields
    ]
    with arcpy.da.SearchCursor(in_table, [f.name for f in fields]) as cursor:
        for row in cursor:
            for i, val in enumerate(row):
                if val is None:
                    result[i]["Nulls"] += 1
                elif val == 0:
                    result[i]["Zeroes"] += 1
                elif val == "unknown":
                    result[i]["Unknowns"] += 1
                elif result[i]["Type"] == "Date" and val <= datetime.datetime(1950, 12, 29):
                    result[i]["DefaultDate"] += 1
    df = pd.DataFrame(result)
    return df

display(analyze("G:/ArcGIS/data/Arcade_Test_Data.gdb/TestPoints"))

 

JohannesLindner_0-1690457547979.png

 


Have a great day!
Johannes
0 Kudos
ZainAhmad
New Contributor

Thank you Johannes. It worked. 

0 Kudos