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

error with @rename macro #307

Open
pstaabp opened this issue May 24, 2020 · 1 comment
Open

error with @rename macro #307

pstaabp opened this issue May 24, 2020 · 1 comment

Comments

@pstaabp
Copy link

pstaabp commented May 24, 2020

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.

@danvinci
Copy link

danvinci commented Jan 27, 2021

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.

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