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

improve DataFrame constructors and conversions for Vector and Matrix #1325

Merged
merged 3 commits into from
Dec 30, 2017

Conversation

bkamins
Copy link
Member

@bkamins bkamins commented Dec 22, 2017

Following discussion in https://discourse.julialang.org/t/how-do-dataframes-jl-compare-to-rs-and-interoperability-between-r-and-julia/7387/14 I propose to:

  1. Add a constructor of DataFrame from a Matrix that can take column names argument;
  2. Add a method to convert allowing conversion from Vector to DataFrame (so that both the constructor and conversion work and work the same way);

Copy link
Member

@nalimilan nalimilan left a comment

Choose a reason for hiding this comment

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

Hmm... I'm not a big fan of these conversions between arrays and data frames, but since they already exist, we may as well support passing names.

Out of curiosity, how does it work in R? I couldn't find it.

return DataFrame(cols, Index(gennames(n)))
end
Base.convert(::Type{DataFrame}, A::AbstractMatrix) = DataFrame(A)
Base.convert(::Type{DataFrame}, V::AbstractVector) = DataFrame(V)
Copy link
Member

Choose a reason for hiding this comment

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

Note that this method is very different from the previous one as it expects the entries to be columns. I'm not sure we should support it, as it could be confusing compared with a 1-column matrix.

Copy link
Member Author

Choose a reason for hiding this comment

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

To say the truth I do not like DataFrame(::Vector) constructor, especially something like DataFrame([1,2,3]) is very non-intuivite. I pass a column vector and get a row of data.

But if we allow it then it is consistent to provide convert also.

Maybe a solution is to restrict the implementation of DataFrame(columns::AbstractVector to check internally:

all(col -> isa(col, AbstractVector), columns) || throw(ArgumentError("columns must be vectors"))

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it's a bit weird. Maybe we could also require specifying the column names when passing a vector, given that this is a rather low-level constructor.

Copy link
Member Author

Choose a reason for hiding this comment

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

agreed. this means that convert should be removed. I add deprecation to calling vector of vectors DataFrame without passing names.

DataFrame(Any[columns[:, i] for i in 1:size(columns, 2)], cnames)
end


Copy link
Member

Choose a reason for hiding this comment

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

One blank line is enough.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@@ -125,6 +126,12 @@ function DataFrame(columns::AbstractVector,
return DataFrame(convert(Vector{Any}, columns), Index(convert(Vector{Symbol}, cnames)))
end

# if #1308 is merged this sould be refactor to include makeunique
function DataFrame(columns::AbstractMatrix, cnames::AbstractVector{Symbol} = gennames(size(columns, 2)))
Copy link
Member

Choose a reason for hiding this comment

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

Better use the short function syntax for one-liners like this.

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

@bkamins
Copy link
Member Author

bkamins commented Dec 22, 2017

In R a matrix is actually our NamedArray - data.frame uses names of columns.
Actually this is something that could be considered do be added in NamedArrays.

@bkamins
Copy link
Member Author

bkamins commented Dec 22, 2017

I will rebase and push this PR when CI works again

@bkamins
Copy link
Member Author

bkamins commented Dec 23, 2017

I decided to recommend allowing DataFrames constructor (and thus convert) from AbstractVector only if it contains AbstractVector. This is breaking so I put deprecation for the time being.

Other issue is that tests on 0.7 fail (for different reasons - I am not sure if this is something to worry about, but in one case Julia crashed trying to precompile CategoricalArrays)

@@ -122,9 +123,17 @@ end

function DataFrame(columns::AbstractVector,
cnames::AbstractVector{Symbol} = gennames(length(columns)))
if !all(col -> isa(col, AbstractVector), columns)
# change to throw(ArgumentError("columns argument mutst be a vector of vectors"))
Copy link
Member

Choose a reason for hiding this comment

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

"must"

Copy link
Member Author

Choose a reason for hiding this comment

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

fixed

return DataFrame(convert(Vector{Any}, columns), Index(convert(Vector{Symbol}, cnames)))
end

# if #1308 is merged this sould be refactor to include makeunique
Copy link
Member

Choose a reason for hiding this comment

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

Let's merge #1308 first.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK. I am pushing the as-is version to have it on GitHub, but I will rebase it when 1308 is merged.

return DataFrame(cols, Index(gennames(n)))
end
Base.convert(::Type{DataFrame}, A::AbstractMatrix) = DataFrame(A)
Base.convert(::Type{DataFrame}, V::AbstractVector) = DataFrame(V)
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it's a bit weird. Maybe we could also require specifying the column names when passing a vector, given that this is a rather low-level constructor.

@@ -120,11 +121,23 @@ function DataFrame(; kwargs...)
end
end

function DataFrame(columns::AbstractVector,
cnames::AbstractVector{Symbol} = gennames(length(columns)))
function DataFrame(columns::AbstractVector)
Copy link
Member

Choose a reason for hiding this comment

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

That function should be moved to deprecated.jl.

Copy link
Member Author

Choose a reason for hiding this comment

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

moved

@bkamins
Copy link
Member Author

bkamins commented Dec 29, 2017

rebased, added makeunique to Matrix constructor and in Vector constructor changed that it is required that at least one element is a vector (not all as was earlier).

@nalimilan
Copy link
Member

I'd rather require all entries to be AbstractVector as you did originally, not just one of them. That's simpler, and since that's a low-level constructor is not a big deal to require the caller to create a vector.

@bkamins
Copy link
Member Author

bkamins commented Dec 30, 2017

OK - fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants