I'm fairly new to Swift programming so I might be overlooking something, but I'm puzzled why the following code doesn't properly insert a row in a DataFrame. The goal is to move a row at a given index to a new index. I would normally:
- Copy the row that I want to move
- Remove the row from the original dataset
- Insert the copy to the new position
The CSV I'm using is from Wikipedia:
Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00
1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00
My code (Swift playground):
import Foundation
import TabularData
let fileUrl = Bundle.main.url(forResource: "data", withExtension: "csv")
let options = CSVReadingOptions(hasHeaderRow: true, delimiter: ",")
var dataFrame = try! DataFrame(contentsOfCSVFile: fileUrl!, options: options)
print("Original data")
print(dataFrame)
let rowToMove: Int = 2
let row = dataFrame.rows[rowToMove]
print("Row to move")
print(row)
dataFrame.removeRow(at: rowToMove)
print("After removing")
print(dataFrame)
dataFrame.insert(row: row, at: 0)
print("After inserting")
print(dataFrame)
This results in the following:
Original data
┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃
┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃
┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ 0 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │
│ 1 │ 1,999 │ Chevy │ Venture "Extended Edition" │ │ 4,900.0 │
│ 2 │ 1,999 │ Chevy │ Venture "Extended Edition, Very Large" │ │ 5,000.0 │
│ 3 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │
└───┴───────┴──────────┴────────────────────────────────────────┴───────────────────────────────────┴──────────┘
4 rows, 5 columns
Row to move
┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃
┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃
┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ 2 │ 1,999 │ Chevy │ Venture "Extended Edition, Very Large" │ │ 5,000.0 │
└───┴───────┴──────────┴────────────────────────────────────────┴─────────────┴──────────┘
1 row, 5 columns
After removing
┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃
┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃
┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ 0 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │
│ 1 │ 1,999 │ Chevy │ Venture "Extended Edition" │ │ 4,900.0 │
│ 2 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │
└───┴───────┴──────────┴────────────────────────────┴───────────────────────────────────┴──────────┘
3 rows, 5 columns
After inserting
┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓
┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃
┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃
┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩
│ 0 │ 1,999 │ Chevy │ │ │ 5,000.0 │
│ 1 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │
│ 2 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │
│ 3 │ nil │ nil │ nil │ nil │ nil │
└───┴───────┴──────────┴────────────────────────────────────────┴───────────────────────────────────┴──────────┘
4 rows, 5 columns
Everything is fine up until inserting. I spot a few issues:
- A row gets deleted (original data row 1)
- A row filled with nil's is added (at index 3)
- the row I want to insert isn't properly inserted (notice how the 'model' text has gone).
I assume I'm missing something - does it have to do with the row copy keeping its index (2)? How can I fix this?