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

error with @rename macro

Open pstaabp opened this issue 5 years ago • 1 comments

If we use:

using Query, DataFrames
df = DataFrame(a=[1,2,3],b=["a","b","c"])

The command:

df |> @rename(:a=>:d)

does the expected rename of the column, however if:

df |> @rename(Symbol("a") => :d)

returns the error:

MethodError: no method matching getindex(::Nothing, ::Int64)

Stacktrace:
 [1] @rename(::LineNumberNode, ::Module, ::Vararg{Any,N} where N) at /Users/XXX/.julia/packages/Query/AwBtd/src/table_query_macros.jl:149

but I would expect the same result. I'm using Julia 1.4.1 and Query 0.12.2.

The reason for doing this would be to rename columns with spaces in them.

pstaabp avatar May 24 '20 13:05 pstaabp

Running into the same issue while working with data frames, in some cases using Symbol is mandatory as the column name includes numbers or things like % or .

@pstaabp if it might help, this is my current workaround:

I defined a function to replace characters preventing column names (in my case) being interpreted as symbols:

function tidy_names(old_names)
			
  return new_names = old_names |>
  #replace spaces with underscores
  n -> replace.(n, ' ' => '_') |> 
  
  #remove parenthesis
  n -> replace.(n,'(' => "") |> 
  n -> replace.(n,')' => "") |>
  
  #remove dashes and dots
  n -> replace.(n,'-' => "") |> 
  n -> replace.(n, '.' => "") |>
  
  #all lowercase
  n -> lowercase.(n)
	
end

And then I run:

rename!(df ,names(df) .=> tidy_names(names(df)))

Before feeding the df to the pipe-magic that Query.jl allows.

danvinci avatar Jan 27 '21 22:01 danvinci