Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nrow and ncol #285

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tables"
uuid = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
authors = ["quinnj <[email protected]>"]
version = "1.7.0"
version = "1.7.1"

[deps]
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
Expand Down
3 changes: 1 addition & 2 deletions src/Tables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,7 @@ function Base.show(io::IO, x::T) where {T <: AbstractRow}
end

function Base.show(io::IO, table::AbstractColumns; max_cols = 20)
ncols = length(columnnames(table))
print(io, "$(typeof(table)) with $(rowcount(table)) rows, $(ncols) columns, and ")
print(io, "$(typeof(table)) with $(nrow(table)) rows, $(ncol(table)) columns, and ")
sch = schema(table)
if sch !== nothing
print(io, "schema:\n")
Expand Down
10 changes: 7 additions & 3 deletions src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

# Turn any AbstractColumns into an AbstractRow iterator

# get the number of rows in the incoming table
function rowcount(cols)
"Return the number of rows in the incoming table."
rikhuijzer marked this conversation as resolved.
Show resolved Hide resolved
function nrow(cols)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bkamins, does this not pirate a default definition? I'm a little uncomfortable with this definition because technically rowcount was only meant to be called on the result of Tables.columns, but now it "looks like" it works generically on any table. And for the most part, it probably will, but we're not really following the principles of the interface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this not pirate a default definition?

DataAPI.jl does not define any method for this function. Same for ncol - DataAPI.jl does not define any method for this function.

As I have commented - it does pirate as it add the definition to ncol with ::Any signature. The problem is exactly what you have commented - some tables might not have a well defined row count (maybe also the same problem is for ncol). So what do you think would be best to do? What I would ideally have is some definition that works by default correctly. I think that if default errors it is not a huge problem if it does not error on "standard" tables.

Maybe what we should do is:

  • leave rowcount to be internal;
  • define nrow and ncol only for tables defined in Tables.jl tables (rowtable, coltable, dictrowtable, dictcolumntable, table maybe some more? - you know best)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I would prefer (only define for Tables.jl-defined tables. I'm also happy to add recommendations to the docs that table types should strongly consider implementing nrow/ncol on their own table types as apart of the API.

names = columnnames(cols)
isempty(names) && return 0
return length(getcolumn(cols, names[1]))
end
@deprecate rowcount(x) nrow(x)

"Return the number of columns in the incoming table."
ncol(cols) = length(columnnames(cols))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here; columnnames and getcolumn are only technically allowed to be called on the result of Tables.columns or on individual iterated rows from Tables.rows, so this makes me a bit uncomfortable.


# a lazy row view into a AbstractColumns object
struct ColumnsRow{T} <: AbstractRow
Expand Down Expand Up @@ -86,7 +90,7 @@ function rows(x::T) where {T}
# first check if it supports column access, and if so, wrap it in a RowIterator
if columnaccess(x)
cols = columns(x)
return RowIterator(cols, Int(rowcount(cols)))
return RowIterator(cols, Int(nrow(cols)))
# otherwise, if the input is at least iterable, we'll wrap it in an IteratorWrapper
# which will iterate the input, validating that elements support the AbstractRow interface
# and unwrapping any DataValues that are encountered
Expand Down
2 changes: 1 addition & 1 deletion src/matrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function matrix(table; transpose::Bool=false)
cols = Columns(table)
types = schema(cols).types
T = reduce(promote_type, types)
n, p = rowcount(cols), length(types)
n, p = nrow(cols), length(types)
if !transpose
matrix = Matrix{T}(undef, n, p)
for (i, col) in enumerate(cols)
Expand Down
2 changes: 1 addition & 1 deletion src/namedtuples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ end

# NamedTuple of arrays of matching dimensionality
const ColumnTable = NamedTuple{names, T} where {names, T <: NTuple{N, AbstractArray{S, D} where S}} where {N, D}
rowcount(c::ColumnTable) = length(c) == 0 ? 0 : length(c[1])
nrow(c::ColumnTable) = length(c) == 0 ? 0 : length(c[1])

# interface implementation
istable(::Type{<:ColumnTable}) = true
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ end

rt = [(a=1, b=4.0, c="7"), (a=2, b=5.0, c="8"), (a=3, b=6.0, c="9")]
nt = (a=[1,2,3], b=[4.0, 5.0, 6.0], c=["7", "8", "9"])
@test Tables.rowcount(nt) == 3
@test_deprecated Tables.rowcount(nt)
@test Tables.nrow(nt) == 3
@test Tables.ncol(nt) == 3
@test Tables.schema(nt) == Tables.Schema((:a, :b, :c), Tuple{Int, Float64, String})
@test Tables.istable(typeof(nt))
@test Tables.columnaccess(typeof(nt))
Expand Down