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

Could the CSV.write argument order be interchanged ? #819

Closed
Lincoln-Hannah opened this issue Mar 25, 2021 · 5 comments
Closed

Could the CSV.write argument order be interchanged ? #819

Lincoln-Hannah opened this issue Mar 25, 2021 · 5 comments

Comments

@Lincoln-Hannah
Copy link

For most DataFrame functions, the DataFrame is the first argument.
This is useful when working with @chain that fills in the first argument as the previous line's output.
If the CSV argument order were swapped. the final line below would be just CSV.write( outputFile )
(The underscore could be omitted)

@chain CSV.read(  inputFile  ) begin
    stack([:Curve,:Tenor] |> Not)
    @where( :value .!= 0 )
    insertcols!(_, ([:Curve2,:Tenor2] .=>  split.( _.variable, '/' ) |> invert )...)
    groupby( [:Curve,:Curve2] )
    combine( :value => sum => :sumval  )    
    CSV.write( outputFile, _ )    
    end
@quinnj
Copy link
Member

quinnj commented Mar 25, 2021

This is already supported? i.e. we have the definition CSV.write(file; kwargs...) = x->CSV.write(file, x; kwargs...)

@Lincoln-Hannah
Copy link
Author

Thanks for your response Jacob. I'm still no clear though sorry.
I'd like to be able to write:

   CSV.write(  df_name,    file_name )

That is, put the DataFrame before the file name, so that it can be omitted within a @chain block.

Is this possible? I couldn't ascertain that from your response.

@quinnj
Copy link
Member

quinnj commented Mar 29, 2021

Yeah, I'm saying you can just do:

@chain CSV.read(  inputFile  ) begin
    stack([:Curve,:Tenor] |> Not)
    @where( :value .!= 0 )
    insertcols!(_, ([:Curve2,:Tenor2] .=>  split.( _.variable, '/' ) |> invert )...)
    groupby( [:Curve,:Curve2] )
    combine( :value => sum => :sumval  )    
    CSV.write( outputFile)    
    end

@Lincoln-Hannah
Copy link
Author

Here is a simpler example:

@chain DataFrame(A=1:4) begin    
    CSV.write( fileName, _  )
end

Removing the ,_ gives the error:

ERROR: ArgumentError: 'String' iterates 'Char' values, which doesn't satisfy the Tables.jl `AbstractRow` interface
Stacktrace:

[1] invalidtable(#unused#::String, #unused#::Char)

@ Tables ~.julia\packages\Tables\UxLRG\src\tofromdatavalues.jl:42

[2] iterate

@ ~.julia\packages\Tables\UxLRG\src\tofromdatavalues.jl:48 [inlined]

[3] write(::Nothing, rows::Tables.IteratorWrapper{String}, file::DataFrame, opts::CSV.Options{UInt8, UInt8, Nothing, Tuple{}, CSV.var"#60#63"}; append::Bool, header::Bool, bufsize::Int64)

@ CSV ~.julia\packages\CSV\CJfFO\src\write.jl:231

[4] write(file::DataFrame, itr::String; delim::Char, quotechar::Char, openquotechar::Nothing, closequotechar::Nothing, escapechar::Char, newline::Char, decimal::Char, dateformat::Nothing, quotestrings::Bool, missingstring::String, transform::CSV.var"#60#63", bom::Bool, append::Bool, writeheader::Nothing, partition::Bool, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})

@ CSV ~.julia\packages\CSV\CJfFO\src\write.jl:191

[5] write(file::DataFrame, itr::String)

@ CSV ~.julia\packages\CSV\CJfFO\src\write.jl:152

[6] top-level scope

@ h:\Documents\Julia Code\Jacobian_Analysis.jl:36

Closes block expression.

@quinnj
Copy link
Member

quinnj commented Mar 30, 2021

Ah, I see. Chain.jl is doing their own transformation to the CSV.write line that prevents the functor form from getting called. Alternatively, if you use regular piping syntax, this works df |> CSV.write("data.csv"). I don't think there's much we can do for this. It's not really that much worse/weird to do CSV.write(file, _) and things are already designed to work w/ piping syntax.

@quinnj quinnj closed this as completed Mar 30, 2021
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

No branches or pull requests

2 participants