Searching within a datetime object python
<p>I am trying to locate where the years are within a specified bounds for a datetime object. I have tried doing a for loop a few different ways but unfortunately I cannot seem to get it to work. I know I am able to search for months and years when I convert a datetime object to a pandas array, but unfortunately the software I am using does not have the pandas library and I am unable to download it to my schools server. </p>

<p>Below is how I read in my time data and it works beautifully (it eliminated a lot of time it would have taken to convert my time a different way)</p>

<pre><code>date = netCDF4.Dateset('filename.nc', mode = 'r')
raw_time = data.variables['time']
time_converted = netCDF4.num2date(raw_time[:], raw_time.units)
</code></pre>

<p>The time_converted variable is a datetime object that appears as follows:</p>

<pre><code>   ....
   datetime.datetime(2006, 1, 1, 0, 0),
   datetime.datetime(2006, 2, 1, 0, 0),
   datetime.datetime(2006, 3, 1, 0, 0),
   datetime.datetime(2006, 4, 1, 0, 0),
   datetime.datetime(2006, 5, 1, 0, 0),
   datetime.datetime(2006, 6, 1, 0, 0),
   datetime.datetime(2006, 7, 1, 0, 0),
   datetime.datetime(2006, 8, 1, 0, 0),
   datetime.datetime(2006, 9, 1, 0, 0),
   ..... 
</code></pre>

<p>The loop below is my most recent attempt and it returns the following error:</p>

<pre><code>time = []
for i in time_converted:
    if i.year&gt;= 2006 and i.year&lt;2016:
        time.append(i)

Type Error: 'int' object is not callable
</code></pre>

<p>Within my loop I have also tried using datetime.datetime.year(i) but that returns:</p>

<pre><code>Type Error: 'getset_descriptor' is not callable
</code></pre>

<p>Turns out I needed to use the following loop instead:</p>

<pre><code>for i in range(len(time_converted)):
    if time_converted[i].year &gt;= 2006 and time_converted[i].year &lt; 2016:
        time.append(time_converted[i])
</code></pre>

python datetime search indexing