PowerFlowData.jl icon indicating copy to clipboard operation
PowerFlowData.jl copied to clipboard

implement iteration over components

Open jd-lara opened this issue 3 years ago • 3 comments

Would it be possible to implement iteration over the data tables?

for i in net.buses
       @show i 
       end

jd-lara avatar Jun 15 '22 00:06 jd-lara

Yes, iterating over the tables should definitely be something we support!

I think we have two options here:

  1. Take the DataFrames.jl route and say that tables are fundamentally 2-dimensional, so for i in table is ambiguous and you need to specify explicitly that you want to iterate over the rows
    • this is possible with Tables.rows or Tables.namedtupleiterator, like:
    for i in Tables.row(net.buses)
      ...
    end
    
    • potentially we could add define eachrow(x::Records) = Tables.rows(x) if we think eachrow is a preferable spelling for this. Although i don't think it's such a bad thing for users to think about whether it is rows, namedtupleiterator or rowtable that they actually want.
  2. Take the CSV.jl route and say that row-wise iteration is what people want when iterating a table, so for i in table should iterate rows
    • this i'd need to think about how best to implement
    • CSV.jl does this via defining CSV.File as <: AbstractVector{CSV.Row} (even though CSV.File is fundamentally implemented as a collection of columns, just like PowerFlowData.Records).

Do you think using Tables.jl is sufficient? e.g.

for i in Tables.rows(net.buses)
       @show i 
end

If so, that already works (well, except i need to fix https://github.com/nickrobinson251/PowerFlowData.jl/issues/76)

Or do you think we should take option 2 and figure out how to make for i in net.buses do the same thing as for i in Tables.rows(net.buses)?


xref #4

nickrobinson251 avatar Jun 15 '22 13:06 nickrobinson251

@nickrobinson251 there is no need really to do for i in Tables.row(net.buses) since the field net.buses is already a vector we can iterate over.

The best option would be to iterate over the whole row so that the fields can be accessed like a DataFrame.

jd-lara avatar Jun 16 '22 00:06 jd-lara

I don't think i understand, sorry 😊 could you show me what you'd like to be able to write and what the behaviour would be?

nickrobinson251 avatar Jun 16 '22 08:06 nickrobinson251