Skip to content

Commit eddc211

Browse files
Fix Tables.schema definitions so row iteration with Tables.jl works (#77)
* Fix `Tables.schema(::Records)` definitions - This makes `Tables.namedtupleiterator` and consequently `Tables.rowtable` work as expected. * Document iterating over tabular records
1 parent 6cd1be0 commit eddc211

File tree

7 files changed

+42
-17
lines changed

7 files changed

+42
-17
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PowerFlowData"
22
uuid = "dd99e9e3-7471-40fc-b48d-a10501125371"
33
authors = "Nick Robinson <npr251@gmail.com> and contributors"
4-
version = "1.4.1"
4+
version = "1.4.2"
55

66
[deps]
77
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"

docs/Manifest.toml

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is machine-generated - editing it directly is not advised
22

3-
julia_version = "1.7.1"
3+
julia_version = "1.7.2"
44
manifest_format = "2.0"
55

66
[[deps.ANSIColoredPrinters]]
@@ -19,9 +19,9 @@ uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
1919

2020
[[deps.Compat]]
2121
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "SHA", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
22-
git-tree-sha1 = "b153278a25dd42c65abbf4e62344f9d22e59191b"
22+
git-tree-sha1 = "9be8be1d8a6f44b96482c8af52238ea7987da3e3"
2323
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
24-
version = "3.43.0"
24+
version = "3.45.0"
2525

2626
[[deps.CompilerSupportLibraries_jll]]
2727
deps = ["Artifacts", "Libdl"]
@@ -33,9 +33,9 @@ uuid = "a8cc5b0e-0ffa-5ad4-8c14-923d3ee1735f"
3333
version = "4.1.1"
3434

3535
[[deps.DataAPI]]
36-
git-tree-sha1 = "cc70b17275652eb47bc9e5f81635981f13cea5c8"
36+
git-tree-sha1 = "fb5f5316dd3fd4c5e7c30a24d50643b73e37cd40"
3737
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
38-
version = "1.9.0"
38+
version = "1.10.0"
3939

4040
[[deps.DataFrames]]
4141
deps = ["Compat", "DataAPI", "Future", "InvertedIndices", "IteratorInterfaceExtensions", "LinearAlgebra", "Markdown", "Missings", "PooledArrays", "PrettyTables", "Printf", "REPL", "Reexport", "SortingAlgorithms", "Statistics", "TableTraits", "Tables", "Unicode"]
@@ -45,9 +45,9 @@ version = "1.3.3"
4545

4646
[[deps.DataStructures]]
4747
deps = ["Compat", "InteractiveUtils", "OrderedCollections"]
48-
git-tree-sha1 = "3daef5523dd2e769dad2365274f760ff5f282c7d"
48+
git-tree-sha1 = "d1fff3a548102f48987a52a2e0d114fa97d730f0"
4949
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
50-
version = "0.18.11"
50+
version = "0.18.13"
5151

5252
[[deps.DataValueInterfaces]]
5353
git-tree-sha1 = "bfc1187b79289637fa0ef6d4436ebdfe6905cbd6"
@@ -184,25 +184,25 @@ version = "1.4.1"
184184

185185
[[deps.Parsers]]
186186
deps = ["Dates"]
187-
git-tree-sha1 = "3b429f37de37f1fc603cc1de4a799dc7fbe4c0b6"
187+
git-tree-sha1 = "0044b23da09b5608b4ecacb4e5e6c6332f833a7e"
188188
uuid = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
189-
version = "2.3.0"
189+
version = "2.3.2"
190190

191191
[[deps.Pkg]]
192192
deps = ["Artifacts", "Dates", "Downloads", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Serialization", "TOML", "Tar", "UUIDs", "p7zip_jll"]
193193
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
194194

195195
[[deps.PooledArrays]]
196196
deps = ["DataAPI", "Future"]
197-
git-tree-sha1 = "28ef6c7ce353f0b35d0df0d5930e0d072c1f5b9b"
197+
git-tree-sha1 = "a6062fe4063cdafe78f4a0a81cfffb89721b30e7"
198198
uuid = "2dfb63ee-cc39-5dd5-95bd-886bf059d720"
199-
version = "1.4.1"
199+
version = "1.4.2"
200200

201201
[[deps.PowerFlowData]]
202-
deps = ["DocStringExtensions", "InlineStrings", "Parsers", "PrettyTables", "Tables"]
202+
deps = ["DataFrames", "DocStringExtensions", "InlineStrings", "Parsers", "PrettyTables", "Tables"]
203203
path = ".."
204204
uuid = "dd99e9e3-7471-40fc-b48d-a10501125371"
205-
version = "1.4.0"
205+
version = "1.4.2"
206206

207207
[[deps.PrettyTables]]
208208
deps = ["Crayons", "Formatting", "Markdown", "Reexport", "Tables"]

docs/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
33
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
44
PowerFlowData = "dd99e9e3-7471-40fc-b48d-a10501125371"
5+
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using PowerFlowData
22
using Documenter
3+
using Tables
34

45
DocMeta.setdocmeta!(PowerFlowData, :DocTestSetup, :(using PowerFlowData); recursive=true)
56

docs/src/index.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Comma delimited files `delim=','` and space delimited files `delim=' '` are curr
3434
Usually your data will be in a file, and you'd read it with `parse_network("file.raw")`,
3535
but here we'll pass in the data directly, to show how it matches to the output:
3636

37-
```@repl
37+
```@repl example1
3838
using PowerFlowData, DataFrames
3939
data = IOBuffer("""
4040
0, 100.00 / PSS/E-30.3 WED, SEP 15 2021 21:04
@@ -49,3 +49,12 @@ network = parse_network(data; v=30);
4949
NamedTuple(network.caseid) # Case Identification data is a single row.
5050
DataFrame(network.buses) # Bus data, and all other data, is a table.
5151
```
52+
53+
For working with the tabular data within a [`Network`](@ref) directly you can use Tables.jl.
54+
For example, to iterate the [`Buses`](@ref):
55+
```@repl example1
56+
using Tables
57+
for bus in Tables.rows(network.buses)
58+
@show bus.name
59+
end
60+
```

src/types.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Tables.columnaccess(::Type{<:Records}) = true
5959
Tables.columns(x::Records) = x
6060
Tables.getcolumn(x::Records, i::Int) = getfield(x, i)
6161
Tables.columnnames(R::Type{<:Records}) = fieldnames(R)
62-
Tables.schema(x::R) where {R <: Records} = Tables.Schema(fieldnames(R), fieldtypes(R))
62+
Tables.schema(x::R) where {R <: Records} = Tables.Schema(fieldnames(R), map(eltype, fieldtypes(R)))
6363
Tables.rowcount(x::Records) = length(x) # faster than going via `columnnames`
6464
Base.length(x::Records) = length(getfield(x, 1)::Vector)
6565
Base.size(x::R) where {R <: Records} = (length(x), fieldcount(R))
@@ -1247,7 +1247,7 @@ end
12471247

12481248
function Tables.schema(x::R) where {R <: Transformers}
12491249
cols = Tables.columnnames(x)
1250-
typs = fieldtype.(R, cols)
1250+
typs = eltype.(fieldtype.(R, cols))
12511251
return Tables.Schema(cols, typs)
12521252
end
12531253

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ using Test
3434
@test length(recs) == 3
3535
@test size(recs) == (3, 2)
3636

37+
@test Tables.rowtable(recs)[1] == (x1=1, yy=3.14)
38+
@test NamedTuple(first(Tables.rows(recs))) == (x1=1, yy=3.14)
39+
@test first(Tables.namedtupleiterator(recs)) == (x1=1, yy=3.14)
40+
3741
df = DataFrame(recs)
3842
@test df isa DataFrame
3943
@test size(df) == (3, 2)
@@ -505,6 +509,16 @@ using Test
505509
@test length(qz.zones) == 2
506510
end
507511

512+
@testset "`Tables.namedtupleiterator(::Records)`" begin
513+
# https://github.com/nickrobinson251/PowerFlowData.jl/issues/76
514+
net = parse_network("testfiles/synthetic_data_v30.raw")
515+
buses = net.buses
516+
@test first(Tables.namedtupleiterator(buses)).i == 111
517+
# Transformers has own `Tables.schema` definition so needs testing specifically.
518+
transformers = net.transformers
519+
@test first(Tables.namedtupleiterator(transformers)).i == 112
520+
end
521+
508522
@testset "empty network" begin
509523
empty_net = Network(version=30)
510524
@test isempty(empty_net)

0 commit comments

Comments
 (0)