Rename column if its name matches to a name in given list

OK. You’ll need to check the actual error you get (instead of only printing that there is a generic error) - and try to debug it.

At this point, it seems that you only know that there is an error.

The catch block should not be used in a way that will hide what is actually going on from you.

2 Likes

On adding rethrow() in code it says that df is not defined. How can i define df above if function change() is outside for loop.

catch e
println("error ") 
rethrow(e)

Indeed. Now, please inspect your change function. You can see that you are using the df, which you didn’t pass to the function as an argument (and it is also not accessible from the function scope).

2 Likes

@algunion @kellertuer Good night :smiling_face_with_three_hearts:
see changes.I will respond tomorrow .

Summary
using HTTP, CSV, DataFrames
function doanalysis()
    dfg=nothing
	function change(df)
          		name=names(df)
   			    list = ["time", "Tmid", "T0", "t-T0", "t-to"] # rename  to "Time"
    			listb = ["mag", "Limit", "Magnitude", "OT", "magn"]
    			for i in 1:length(name)
        			if name[i] in list ;  rename!(df,Dict("name[i]"=>"Time")); end
        			if name[i] in listb ; rename!(df,Dict("name[i]"=>"Mag")); end
    			i=+1
    			end
	end
    for x in 31520
    print("\r peeking at GCN $x ")
        try
            url = "https://gcn.nasa.gov/circulars/$x.txt"
            resp = HTTP.get(url) 
            status=resp.status
            print(" ",status," "); 
            if status == 404 ; println("status=",status); continue; end          
            txt = String(resp.body)
            grb_rexp=r"GRB ?\d{6}([A-G]|(\.\d{2}))?"
				m=match(grb_rexp,txt)
				grb="nogrb"
                if occursin(grb_rexp,txt)
				    print(m.match)
					grb=m.match
			    end
            if occursin("IKI", txt)
                println(" IKI report")                
                he=first(findfirst(r"^(Date)|(UT start)|(T0+)"m,txt))
                lr=first(findnext(r"^\nThe |(Photometry)"m,txt,he))-1
                cltxt=replace(txt[he:lr], ","=>" ",r"(�+)"=>" " , "+/-"=>" ",">"=>" ","(mid)"=>" ",r"UT ?start"i=>"UT_start")
                df=CSV.read(IOBuffer(cltxt), DataFrame, skipto=3, delim=" ", ignorerepeated=true)
                df.GCN=[x for i in 1:nrow(df)]
                df.GRB=[m.match for i in 1:nrow(df)] 
				change(df)
				if isnothing(dfg) 
                     dfg=df
                else
                    @show dfg=vcat(dfg,df,cols=:union)
                end # if x is first
            end # if occursin
					catch e
		rethrow(e)
        end # trycatch
    end # for loop
end
doanalysis()

@raman_kumar,

From our interaction so far (e.g., code snippets you presented and the type of questions), I suggest you go over a Julia tutorial/manual to get a basic handle on how the language works (function definition, scope, debugging, etc.).

Also, it is essential you follow up with the error messages (which, in this scenario, are pretty informative) and change your code accordingly.

You can always come here and ask for help - and I am sure you’ll find lots of people willing to help, myself included, but I think it is important that your questions are related to either the language itself or some packages issues that are not properly covered by the package documentation.

My impression (both derived from this thread interaction but also from other topics we interacted on) is that your posts are either requesting some code delivery or step-by-step instructions on what you need to do to achieve some functionality you demand.

I think just consuming code/instructions is not an actual learning experience, and you need to return for more of the same thing. Again and again.

Please read this through the right lens: I am trying to help here. So don’t feel embarrassed - we are all on a learning path (regardless of our level of experience). I am learning daily from how others answer challenging questions and reading great code others wrote.

Also - don’t get me wrong: I do not want to discourage you from a hands-on approach - there is obvious value in that (and the best way to learn).

3 Likes

I am still stuck @algunion

see code
using HTTP, CSV, DataFrames
function doanalysis()
    dfg=nothing
	function change(df)
          		@show name=names(df)
				@show name[3]
   			    list = ["time", "Tmid", "T0", "t-T0", "t-to"] # rename  to "Time"
    			listb = ["mag", "Limit", "Magnitude", "OT", "magn"]
    			for i in 1:length(name)
        			if @show name[i] in list
						rename!(df,Dict("name[i]"=>"Time"))
					elseif name[i] in listb ; rename!(df,Dict("name[i]"=>"Mag"))
					else 
						continue
					end
    			 i=+1
    			end
	end
    for x in 31520
    print("\r peeking at GCN $x ")
        try
            url = "https://gcn.nasa.gov/circulars/$x.txt"
            resp = HTTP.get(url) 
            status=resp.status
            print(" ",status," "); 
            if status == 404 ; println("status=",status); continue; end          
            txt = String(resp.body)
            grb_rexp=r"GRB ?\d{6}([A-G]|(\.\d{2}))?"
				m=match(grb_rexp,txt)
				grb="nogrb"
                if occursin(grb_rexp,txt)
				    print(m.match)
					grb=m.match
			    end
            if occursin("IKI", txt)
                println(" IKI report")                
                he=first(findfirst(r"^(Date)|(UT start)|(T0+)"m,txt))
                lr=first(findnext(r"^\nThe |(Photometry)"m,txt,he))-1
                cltxt=replace(txt[he:lr], ","=>" ",r"(�+)"=>" " , "+/-"=>" ",">"=>" ","(mid)"=>" ",r"UT ?start"i=>"UT_start")
                df=CSV.read(IOBuffer(cltxt), DataFrame, skipto=3, delim=" ", ignorerepeated=true)
                df.GCN=[x for i in 1:nrow(df)]
                df.GRB=[m.match for i in 1:nrow(df)] 
				change(df)
				if isnothing(dfg) 
                     dfg=df
                else
                    @show dfg=vcat(dfg,df,cols=:union)
                end # if x is first
            end # if occursin
					catch e
		rethrow(e)
        end # trycatch
    end # for loop
end
doanalysis()

Please inspect the error: it is clearly pointing out that you want to rename :name[i] to something else - but :name[i] doesn’t exists.

If you take a look at your code you can see that you actually use the literal string "name[i]" - using name[i] will attempt to look up the name object and get index i. But "name[i]" is just a string that reads name[i].

1 Like