Update Cursor skipping last row? Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Create Python script to update field based on change in another field ArcGISUpdateCursor only takes last value of SearchCursorWhy is Update Cursor adding Object IDs incorrectly?Calculate Row if Row Value Equals Value in ListIdentify closed polylines in ArcGIS using PythonIteratively Updating Just Bottom Row in Table using ArcPy?Comparing value with value from the next rowArcPy rollback update / insert cursors if error occursUsing cursor.next() is resetting “row in cursor” causing it to skip every other rowSwitching from Nested Search Cursors to Dictionaries
Do you forfeit tax refunds/credits if you aren't required to and don't file by April 15?
Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?
Bonus calculation: Am I making a mountain out of a molehill?
What causes the vertical darker bands in my photo?
How to motivate offshore teams and trust them to deliver?
What does the "x" in "x86" represent?
If a contract sometimes uses the wrong name, is it still valid?
How do I keep my slimes from escaping their pens?
Letter Boxed validator
How much radiation do nuclear physics experiments expose researchers to nowadays?
What is this single-engine low-wing propeller plane?
What would be the ideal power source for a cybernetic eye?
Stars Make Stars
What are the pros and cons of Aerospike nosecones?
Gastric acid as a weapon
Is there any avatar supposed to be born between the death of Krishna and the birth of Kalki?
Is a manifold-with-boundary with given interior and non-empty boundary essentially unique?
Proof involving the spectral radius and the Jordan canonical form
How do I stop a creek from eroding my steep embankment?
Why does Python start at index -1 when indexing a list from the end?
Is 1 ppb equal to 1 μg/kg?
When to stop saving and start investing?
How discoverable are IPv6 addresses and AAAA names by potential attackers?
Should I discuss the type of campaign with my players?
Update Cursor skipping last row?
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Create Python script to update field based on change in another field ArcGISUpdateCursor only takes last value of SearchCursorWhy is Update Cursor adding Object IDs incorrectly?Calculate Row if Row Value Equals Value in ListIdentify closed polylines in ArcGIS using PythonIteratively Updating Just Bottom Row in Table using ArcPy?Comparing value with value from the next rowArcPy rollback update / insert cursors if error occursUsing cursor.next() is resetting “row in cursor” causing it to skip every other rowSwitching from Nested Search Cursors to Dictionaries
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have created a python function within field calculator to calculate the time difference between a timestamp (yyyy-MM-dd HH:mm:ss) in a row and the row after (the interval field in seconds). I have used a update cursor with field calculator to do this, however I am receiving null values for both the first and last row.
The first is correctly missed due to the function being reliant on having a value set by the previous row.
I am unsure as to why the last row is being skipped?
The first row of the attribute is formatted as:
The last:
I have had this issue on several other update cursor functions including those on simple single row condition statements.
Am I missing a statement to close the update cursor loop?
My code is:
import arcpy, time, datetime
from time import strftime
from datetime import timedelta, datetime
from arcpy import da
def FindTime(table,date,interval):
firstRow = True
with arcpy.da.UpdateCursor(table, ["interval", "date"]) as cursor:
for row in cursor:
gap2 = row[1]
if firstRow == True:
gap1 = gap2
firstRow = False
continue
timedelta = gap2 - gap1
row[0] = timedelta.days * 24 * 3600 + timedelta.seconds
gap1 = gap2
cursor.updateRow(row)
*Updated from below comment's solution:
import arcpy
def FindTime(fc, datefield, daydiff_field):
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
arcpy modelbuilder field-calculator cursor python-parser
add a comment |
I have created a python function within field calculator to calculate the time difference between a timestamp (yyyy-MM-dd HH:mm:ss) in a row and the row after (the interval field in seconds). I have used a update cursor with field calculator to do this, however I am receiving null values for both the first and last row.
The first is correctly missed due to the function being reliant on having a value set by the previous row.
I am unsure as to why the last row is being skipped?
The first row of the attribute is formatted as:
The last:
I have had this issue on several other update cursor functions including those on simple single row condition statements.
Am I missing a statement to close the update cursor loop?
My code is:
import arcpy, time, datetime
from time import strftime
from datetime import timedelta, datetime
from arcpy import da
def FindTime(table,date,interval):
firstRow = True
with arcpy.da.UpdateCursor(table, ["interval", "date"]) as cursor:
for row in cursor:
gap2 = row[1]
if firstRow == True:
gap1 = gap2
firstRow = False
continue
timedelta = gap2 - gap1
row[0] = timedelta.days * 24 * 3600 + timedelta.seconds
gap1 = gap2
cursor.updateRow(row)
*Updated from below comment's solution:
import arcpy
def FindTime(fc, datefield, daydiff_field):
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
arcpy modelbuilder field-calculator cursor python-parser
1
There is no row[2] in your first try, you only have two fields. Are you trying to fetch the value from next row? That is not possible
– BERA
Apr 9 at 19:23
Edited pasting to SE typo!
– Will
Apr 10 at 9:42
add a comment |
I have created a python function within field calculator to calculate the time difference between a timestamp (yyyy-MM-dd HH:mm:ss) in a row and the row after (the interval field in seconds). I have used a update cursor with field calculator to do this, however I am receiving null values for both the first and last row.
The first is correctly missed due to the function being reliant on having a value set by the previous row.
I am unsure as to why the last row is being skipped?
The first row of the attribute is formatted as:
The last:
I have had this issue on several other update cursor functions including those on simple single row condition statements.
Am I missing a statement to close the update cursor loop?
My code is:
import arcpy, time, datetime
from time import strftime
from datetime import timedelta, datetime
from arcpy import da
def FindTime(table,date,interval):
firstRow = True
with arcpy.da.UpdateCursor(table, ["interval", "date"]) as cursor:
for row in cursor:
gap2 = row[1]
if firstRow == True:
gap1 = gap2
firstRow = False
continue
timedelta = gap2 - gap1
row[0] = timedelta.days * 24 * 3600 + timedelta.seconds
gap1 = gap2
cursor.updateRow(row)
*Updated from below comment's solution:
import arcpy
def FindTime(fc, datefield, daydiff_field):
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
arcpy modelbuilder field-calculator cursor python-parser
I have created a python function within field calculator to calculate the time difference between a timestamp (yyyy-MM-dd HH:mm:ss) in a row and the row after (the interval field in seconds). I have used a update cursor with field calculator to do this, however I am receiving null values for both the first and last row.
The first is correctly missed due to the function being reliant on having a value set by the previous row.
I am unsure as to why the last row is being skipped?
The first row of the attribute is formatted as:
The last:
I have had this issue on several other update cursor functions including those on simple single row condition statements.
Am I missing a statement to close the update cursor loop?
My code is:
import arcpy, time, datetime
from time import strftime
from datetime import timedelta, datetime
from arcpy import da
def FindTime(table,date,interval):
firstRow = True
with arcpy.da.UpdateCursor(table, ["interval", "date"]) as cursor:
for row in cursor:
gap2 = row[1]
if firstRow == True:
gap1 = gap2
firstRow = False
continue
timedelta = gap2 - gap1
row[0] = timedelta.days * 24 * 3600 + timedelta.seconds
gap1 = gap2
cursor.updateRow(row)
*Updated from below comment's solution:
import arcpy
def FindTime(fc, datefield, daydiff_field):
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
arcpy modelbuilder field-calculator cursor python-parser
arcpy modelbuilder field-calculator cursor python-parser
edited Apr 10 at 9:15
Will
asked Apr 9 at 8:56
WillWill
695
695
1
There is no row[2] in your first try, you only have two fields. Are you trying to fetch the value from next row? That is not possible
– BERA
Apr 9 at 19:23
Edited pasting to SE typo!
– Will
Apr 10 at 9:42
add a comment |
1
There is no row[2] in your first try, you only have two fields. Are you trying to fetch the value from next row? That is not possible
– BERA
Apr 9 at 19:23
Edited pasting to SE typo!
– Will
Apr 10 at 9:42
1
1
There is no row[2] in your first try, you only have two fields. Are you trying to fetch the value from next row? That is not possible
– BERA
Apr 9 at 19:23
There is no row[2] in your first try, you only have two fields. Are you trying to fetch the value from next row? That is not possible
– BERA
Apr 9 at 19:23
Edited pasting to SE typo!
– Will
Apr 10 at 9:42
Edited pasting to SE typo!
– Will
Apr 10 at 9:42
add a comment |
1 Answer
1
active
oldest
votes
If your field is type date, code below should work. Last line is not calculated since there is no row after. Or do you want to calculate for example second rows diff as second row-first row?
import arcpy
fc = 'somedates'
datefield = 'date123'
daydiff_field = 'seconddiff_long'
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
#next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
3
This is why I love using SE, I have never used the iter() function before, had to go away and look it up, like it! Is there a performance boost using that approach or was it for convenience?
– Hornbydd
Apr 9 at 10:29
3
Nice! Dont know if there is a performance boost. I use it because it is a simple way of fetching items in a list instead of trying to iterate over a cursor and list at the same time.
– BERA
Apr 9 at 10:31
2
If anything, it will actually be slower, because the table is traversed twice instead of once. But for small tables that may not be an issue.
– Berend
Apr 9 at 12:52
1
Slower than what approach?
– BERA
Apr 9 at 12:54
1
Slower than using just an update cursor, as OP does
– Berend
Apr 9 at 12:54
|
show 2 more comments
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "79"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f318213%2fupdate-cursor-skipping-last-row%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your field is type date, code below should work. Last line is not calculated since there is no row after. Or do you want to calculate for example second rows diff as second row-first row?
import arcpy
fc = 'somedates'
datefield = 'date123'
daydiff_field = 'seconddiff_long'
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
#next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
3
This is why I love using SE, I have never used the iter() function before, had to go away and look it up, like it! Is there a performance boost using that approach or was it for convenience?
– Hornbydd
Apr 9 at 10:29
3
Nice! Dont know if there is a performance boost. I use it because it is a simple way of fetching items in a list instead of trying to iterate over a cursor and list at the same time.
– BERA
Apr 9 at 10:31
2
If anything, it will actually be slower, because the table is traversed twice instead of once. But for small tables that may not be an issue.
– Berend
Apr 9 at 12:52
1
Slower than what approach?
– BERA
Apr 9 at 12:54
1
Slower than using just an update cursor, as OP does
– Berend
Apr 9 at 12:54
|
show 2 more comments
If your field is type date, code below should work. Last line is not calculated since there is no row after. Or do you want to calculate for example second rows diff as second row-first row?
import arcpy
fc = 'somedates'
datefield = 'date123'
daydiff_field = 'seconddiff_long'
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
#next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
3
This is why I love using SE, I have never used the iter() function before, had to go away and look it up, like it! Is there a performance boost using that approach or was it for convenience?
– Hornbydd
Apr 9 at 10:29
3
Nice! Dont know if there is a performance boost. I use it because it is a simple way of fetching items in a list instead of trying to iterate over a cursor and list at the same time.
– BERA
Apr 9 at 10:31
2
If anything, it will actually be slower, because the table is traversed twice instead of once. But for small tables that may not be an issue.
– Berend
Apr 9 at 12:52
1
Slower than what approach?
– BERA
Apr 9 at 12:54
1
Slower than using just an update cursor, as OP does
– Berend
Apr 9 at 12:54
|
show 2 more comments
If your field is type date, code below should work. Last line is not calculated since there is no row after. Or do you want to calculate for example second rows diff as second row-first row?
import arcpy
fc = 'somedates'
datefield = 'date123'
daydiff_field = 'seconddiff_long'
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
#next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
If your field is type date, code below should work. Last line is not calculated since there is no row after. Or do you want to calculate for example second rows diff as second row-first row?
import arcpy
fc = 'somedates'
datefield = 'date123'
daydiff_field = 'seconddiff_long'
all_dates = [i[0] for i in arcpy.da.SearchCursor(fc,datefield)]
diff = [(d1-d0).seconds for d0,d1 in zip(all_dates, all_dates[1:])]
givediff = iter(diff)
with arcpy.da.UpdateCursor(fc, daydiff_field) as cursor:
#next(cursor) #uncomment this line if you want to calculate row2 diff as row2-row1, =first row no diff
for row in cursor:
try:
row[0] = next(givediff) #Fetch diffs until list is empty...
cursor.updateRow(row)
except StopIteration: #...then break the cursor
break
edited Apr 9 at 13:08
answered Apr 9 at 9:34
BERABERA
17.2k62044
17.2k62044
3
This is why I love using SE, I have never used the iter() function before, had to go away and look it up, like it! Is there a performance boost using that approach or was it for convenience?
– Hornbydd
Apr 9 at 10:29
3
Nice! Dont know if there is a performance boost. I use it because it is a simple way of fetching items in a list instead of trying to iterate over a cursor and list at the same time.
– BERA
Apr 9 at 10:31
2
If anything, it will actually be slower, because the table is traversed twice instead of once. But for small tables that may not be an issue.
– Berend
Apr 9 at 12:52
1
Slower than what approach?
– BERA
Apr 9 at 12:54
1
Slower than using just an update cursor, as OP does
– Berend
Apr 9 at 12:54
|
show 2 more comments
3
This is why I love using SE, I have never used the iter() function before, had to go away and look it up, like it! Is there a performance boost using that approach or was it for convenience?
– Hornbydd
Apr 9 at 10:29
3
Nice! Dont know if there is a performance boost. I use it because it is a simple way of fetching items in a list instead of trying to iterate over a cursor and list at the same time.
– BERA
Apr 9 at 10:31
2
If anything, it will actually be slower, because the table is traversed twice instead of once. But for small tables that may not be an issue.
– Berend
Apr 9 at 12:52
1
Slower than what approach?
– BERA
Apr 9 at 12:54
1
Slower than using just an update cursor, as OP does
– Berend
Apr 9 at 12:54
3
3
This is why I love using SE, I have never used the iter() function before, had to go away and look it up, like it! Is there a performance boost using that approach or was it for convenience?
– Hornbydd
Apr 9 at 10:29
This is why I love using SE, I have never used the iter() function before, had to go away and look it up, like it! Is there a performance boost using that approach or was it for convenience?
– Hornbydd
Apr 9 at 10:29
3
3
Nice! Dont know if there is a performance boost. I use it because it is a simple way of fetching items in a list instead of trying to iterate over a cursor and list at the same time.
– BERA
Apr 9 at 10:31
Nice! Dont know if there is a performance boost. I use it because it is a simple way of fetching items in a list instead of trying to iterate over a cursor and list at the same time.
– BERA
Apr 9 at 10:31
2
2
If anything, it will actually be slower, because the table is traversed twice instead of once. But for small tables that may not be an issue.
– Berend
Apr 9 at 12:52
If anything, it will actually be slower, because the table is traversed twice instead of once. But for small tables that may not be an issue.
– Berend
Apr 9 at 12:52
1
1
Slower than what approach?
– BERA
Apr 9 at 12:54
Slower than what approach?
– BERA
Apr 9 at 12:54
1
1
Slower than using just an update cursor, as OP does
– Berend
Apr 9 at 12:54
Slower than using just an update cursor, as OP does
– Berend
Apr 9 at 12:54
|
show 2 more comments
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f318213%2fupdate-cursor-skipping-last-row%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
There is no row[2] in your first try, you only have two fields. Are you trying to fetch the value from next row? That is not possible
– BERA
Apr 9 at 19:23
Edited pasting to SE typo!
– Will
Apr 10 at 9:42