<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to get all dates up to a certain date in python code in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-get-all-dates-up-to-a-certain-date-in/m-p/1312470#M68261</link>
    <description>&lt;P&gt;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&amp;nbsp; that is working perfectly except the date column one. can you please help me where I am not paying attention to make code working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code is as below&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import pandas as pd&lt;BR /&gt;from arcgis.features import GeoAccessor&lt;BR /&gt;from sys import argv&lt;BR /&gt;from datetime import date&lt;/P&gt;&lt;P&gt;# Set the workspace to your geodatabase&lt;BR /&gt;geodatabase = r"Z:\path"&lt;BR /&gt;arcpy.env.workspace = geodatabase&lt;BR /&gt;# Specify the feature class to analyze&lt;BR /&gt;feature_class = "Shape_file"&lt;/P&gt;&lt;P&gt;# Get a list of all fields in the feature class&lt;BR /&gt;fields = arcpy.ListFields(feature_class)&lt;/P&gt;&lt;P&gt;# Create an empty list to store the results&lt;BR /&gt;results = []&lt;/P&gt;&lt;P&gt;# Iterate through each field&lt;BR /&gt;for field in fields:&lt;BR /&gt;field_name = field.name&lt;BR /&gt;field_type = field.type&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;# Count null and zero values using SearchCursor&lt;BR /&gt;null_count = 0&lt;BR /&gt;zero_count = 0&lt;BR /&gt;unknowns = 0&lt;BR /&gt;Default_Date = 0&lt;BR /&gt;with arcpy.da.SearchCursor(feature_class, [field_name]) as cursor:&lt;BR /&gt;for row in cursor:&lt;BR /&gt;if row[0] is None:&lt;BR /&gt;null_count += 1&lt;BR /&gt;elif field.type in ["Double", "Float", "Integer", "SmallInteger"] and row[0] == 0:&lt;BR /&gt;zero_count += 1&lt;BR /&gt;elif row[0] == 'unknown':&lt;BR /&gt;unknowns += 1&lt;BR /&gt;elif field.type in ["Date"] and row[0] &amp;lt;="1950-12-29":&lt;BR /&gt;Default_Date += 1&lt;/P&gt;&lt;P&gt;# Append the results to the list&lt;BR /&gt;results.append({&lt;BR /&gt;"Field": field_name,&lt;BR /&gt;"Type": field_type,&lt;BR /&gt;"Nulls": null_count,&lt;BR /&gt;"Zeroes": zero_count,&lt;BR /&gt;"Unknowns": unknowns,&lt;BR /&gt;"Default Date": Default_Date})&lt;/P&gt;&lt;P&gt;# Create a pandas DataFrame from the results&lt;BR /&gt;df = pd.DataFrame(results)&lt;/P&gt;&lt;P&gt;# Display the DataFrame as a table in the notebook&lt;BR /&gt;display(df)&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jul 2023 10:52:03 GMT</pubDate>
    <dc:creator>ZainAhmad</dc:creator>
    <dc:date>2023-07-27T10:52:03Z</dc:date>
    <item>
      <title>How to get all dates up to a certain date in python code</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-all-dates-up-to-a-certain-date-in/m-p/1312470#M68261</link>
      <description>&lt;P&gt;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&amp;nbsp; that is working perfectly except the date column one. can you please help me where I am not paying attention to make code working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;my code is as below&lt;/P&gt;&lt;P&gt;import arcpy&lt;BR /&gt;import pandas as pd&lt;BR /&gt;from arcgis.features import GeoAccessor&lt;BR /&gt;from sys import argv&lt;BR /&gt;from datetime import date&lt;/P&gt;&lt;P&gt;# Set the workspace to your geodatabase&lt;BR /&gt;geodatabase = r"Z:\path"&lt;BR /&gt;arcpy.env.workspace = geodatabase&lt;BR /&gt;# Specify the feature class to analyze&lt;BR /&gt;feature_class = "Shape_file"&lt;/P&gt;&lt;P&gt;# Get a list of all fields in the feature class&lt;BR /&gt;fields = arcpy.ListFields(feature_class)&lt;/P&gt;&lt;P&gt;# Create an empty list to store the results&lt;BR /&gt;results = []&lt;/P&gt;&lt;P&gt;# Iterate through each field&lt;BR /&gt;for field in fields:&lt;BR /&gt;field_name = field.name&lt;BR /&gt;field_type = field.type&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;# Count null and zero values using SearchCursor&lt;BR /&gt;null_count = 0&lt;BR /&gt;zero_count = 0&lt;BR /&gt;unknowns = 0&lt;BR /&gt;Default_Date = 0&lt;BR /&gt;with arcpy.da.SearchCursor(feature_class, [field_name]) as cursor:&lt;BR /&gt;for row in cursor:&lt;BR /&gt;if row[0] is None:&lt;BR /&gt;null_count += 1&lt;BR /&gt;elif field.type in ["Double", "Float", "Integer", "SmallInteger"] and row[0] == 0:&lt;BR /&gt;zero_count += 1&lt;BR /&gt;elif row[0] == 'unknown':&lt;BR /&gt;unknowns += 1&lt;BR /&gt;elif field.type in ["Date"] and row[0] &amp;lt;="1950-12-29":&lt;BR /&gt;Default_Date += 1&lt;/P&gt;&lt;P&gt;# Append the results to the list&lt;BR /&gt;results.append({&lt;BR /&gt;"Field": field_name,&lt;BR /&gt;"Type": field_type,&lt;BR /&gt;"Nulls": null_count,&lt;BR /&gt;"Zeroes": zero_count,&lt;BR /&gt;"Unknowns": unknowns,&lt;BR /&gt;"Default Date": Default_Date})&lt;/P&gt;&lt;P&gt;# Create a pandas DataFrame from the results&lt;BR /&gt;df = pd.DataFrame(results)&lt;/P&gt;&lt;P&gt;# Display the DataFrame as a table in the notebook&lt;BR /&gt;display(df)&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 10:52:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-all-dates-up-to-a-certain-date-in/m-p/1312470#M68261</guid>
      <dc:creator>ZainAhmad</dc:creator>
      <dc:date>2023-07-27T10:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all dates up to a certain date in python code</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-all-dates-up-to-a-certain-date-in/m-p/1312477#M68262</link>
      <description>&lt;P&gt;To post formatted code:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1689266678674.png" style="width: 546px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/75345i1DB988B7B4303219/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_0-1689266678674.png" alt="JohannesLindner_0-1689266678674.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_1-1689266693900.png" style="width: 469px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/75346i06615A9413162052/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_1-1689266693900.png" alt="JohannesLindner_1-1689266693900.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SearchCursor reads values from date fields as datetime.datetime objects, so you can compare to that.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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 &amp;lt;= 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"))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1690457547979.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/76503i20FF61FD5E5855AE/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_0-1690457547979.png" alt="JohannesLindner_0-1690457547979.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 11:32:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-all-dates-up-to-a-certain-date-in/m-p/1312477#M68262</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-07-27T11:32:35Z</dc:date>
    </item>
    <item>
      <title>Re: How to get all dates up to a certain date in python code</title>
      <link>https://community.esri.com/t5/python-questions/how-to-get-all-dates-up-to-a-certain-date-in/m-p/1312546#M68265</link>
      <description>&lt;P&gt;Thank you Johannes. It worked.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 14:21:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-get-all-dates-up-to-a-certain-date-in/m-p/1312546#M68265</guid>
      <dc:creator>ZainAhmad</dc:creator>
      <dc:date>2023-07-27T14:21:03Z</dc:date>
    </item>
  </channel>
</rss>

