I load the following CSV data into QGIS 3 (specs below) as a table with no geometry using default options.
"V1","time","V2"
-49,"2021-12-28T00:12:17.725753Z",13.5
-50,"2021-12-28T00:11:17.839767Z",14.5
-49,"2021-12-28T00:10:17.958035Z",14.2
-52,"2021-12-28T00:09:18.078015Z",14.2
I open a python terminal to access the layer with table = QgsProject.instance().mapLayersByName("table")[0]
When I call getFeatures()
I see that there are, correctly, 4 features
>>> list(table.getFeatures())
[<qgis._core.QgsFeature object at 0x00000215D6518948>, <qgis._core.QgsFeature object at 0x00000215D65189D8>,
<qgis._core.QgsFeature object at 0x00000215D6518A68>, <qgis._core.QgsFeature object at 0x00000215D6518AF8>]
>>>
But when I call getFeature()
I can access indices 0 through 5, suggesting that there are 6 features. The attributes of the first two features are unusual.
>>> table.getFeature(0).attributes()
[NULL, 'time', NULL]
>>> table.getFeature(1).attributes()
[NULL, 'time', NULL]
>>> table.getFeature(2).attributes()
[-49, '2021-12-28T00:12:17.725753Z', 13.5]
>>> table.getFeature(5).attributes()
[-52, '2021-12-28T00:09:18.078015Z', 14.2]
My assumption was that these two functions accessed the same data. But this suggests they don't and now I'm confused. What's going on, and how do I program defensively around it?
QGIS version
3.8.3-Zanzibar
QGIS code revision
685d8b15d2
Compiled against Qt
5.11.2
Running against Qt
5.11.2
Compiled against GDAL/OGR
2.4.1
Running against GDAL/OGR
2.4.1
Compiled against GEOS
3.7.2-CAPI-1.11.0
Running against GEOS
3.8.0-CAPI-1.13.1
PostgreSQL Client Version
10.8
SpatiaLite Version
4.3.0
QWT Version
6.1.3
QScintilla2 Version
2.10.8
Compiled against PROJ
5.2.0
Running against PROJ
Rel. 5.2.0, September 15th, 2018
OS Version
Windows 10 (10.0)
type(table.getFeature(0))
say?type(table.getFeature(0))
returns<class 'qgis._core.QgsFeature'>
type(table.getFeature(10000))
returns aQgsFeature
, too.