TypeError : 'method' object is not subscriptable - sklearn-pandas

The error i have is this:
TypeError: 'method' object is not subscriptable
pd.DataFrame({'Pokemon_name':test.Pokemon_name,'CP':new_pred_class}).set_index['Pokemon_name'].to_csv['new_pred_cp.csv']
Im trying to write the results to a new csv file.

.to_csv is a function/method, and you can't use [] after it, it must be ().
So the code should be:
....['Pokemon_name'].to_csv("new_pred_cp.csv")

Related

dashing job undefined map method error

I'm using the dashing dashboard to display some data. Part of my code for one of the jobs uses the map! function in ruby:
vars = arr.map! { |element| element.gsub(/.{3}$/, '' )}
and when I try to run the dashboard using dashing start, I get the following error :
scheduler caught exception:
undefined method map! for #<Hash: 0x......>
If I run the code on its own as a ruby program, I get the correct result.
The documentation for the JSON module indicates that the parse method will "...convert your string into a hash." See http://www.ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html#module-JSON-label-Parsing+JSON.
Try calling just map on your hash instead of calling map!:
vars = arr.map { |element| element.gsub(/.{3}$/, '' )}
The difference is that map will return a new array with the results of running your block once for every element in the Hash. Also, map is defined in the Enumerable module, which is included by Hash, but map! is not defined in Enumerable. See http://www.ruby-doc.org/core-2.0.0/Enumerable.html#method-i-map.

http_request.set_form_data : Getting undefined method map for string for the json parameter

I am trying to pass a json representation to a set_form_data and am running into following error:
undefined method `map' for "{\"first_name\":\"bill\",\"last_name\":\"gates\"}":String
Here is how I create the request:
Net::HTTP::Post.new(uri.request_uri).set_form_data({"first_name" => "steve","last_name" => "jobs"}.to_json)
Anything I am missing ?
The error comes from the line:
/jruby/jruby-1.6.2/lib/ruby/1.9/net/http.rb:1593:in `set_form_data'
I used request.body=form_data instead of request.set_form_data and that worked.
However I dont know for sure why set_form_data did not work.
Marking this as an answer, since that's the one that worked for me so far.
set_form_data wants a hash, not a json, so skip the to_json and it should work better.
ie:
some_data = {:foo => :bar, :meh => :muh}
Net::HTTP::Post.new(uri.request_uri).set_form_data(some_data)

Extract a value from an OpenStruct Ruby object

I get the following Ruby object returned (from a query to the Google Analytics API using the garb gem, comes from the sample call shown on the README.md there, Exits.results(profile, :filters => {:page_path.eql => '/'}))
> data.results
=> [#<OpenStruct page_path="/", exits="3706", pageviews="10440">]
I'd to extract the pageviews value (10440), but cannot figure out how to do it. I see that my object, data.results is class array of length 1, but data.first is class OpenStruct with a return value that looks almost identical:
irb(main):140:0> data.results.class
=> Array
irb(main):141:0> data.results.length
=> 1
irb(main):142:0> data.first
=> #<OpenStruct page_path="/", exits="3706", pageviews="10440">
irb(main):143:0> data.first.class
=> OpenStruct
while data itself seems to be a custom return type called ResultsSet:
irb(main):144:0> data.class
=> Garb::ResultSet
irb(main):145:0> data
=> #<Garb::ResultSet:0x00000002411070 #results=[#<OpenStruct page_path="/", exits="3706", pageviews="10440">], #total_results=1, #sampled=false>
irb(main):146:0>
Lots of data structures, but no idea how to get my desired value out. I gathered OpenStruct was related to a hash, so I thought data.first["pageviews"] would do it,
NoMethodError: undefined method `[]' for #<OpenStruct page_path="/", exits="3706", pageviews="10440">
from (irb):146
from /usr/bin/irb:12:in `<main>'
Meanwhile data.first.keys returns nil. No idea how to get my data out, (short of converting the length-1 array, data.results to a string and parsing with grep, which seems crazy. Any ideas?
Please try this:
data.first.pageviews

TypeError: can't convert Builder::XmlMarkup to Array

I'm having issues with getting access to the raw xml from a Builder::XmlMarkup object.
irb> xml = Builder::XmlMarkup.new(:target => '')
=> <pretty_inspect/>
irb> xml.foo("bar")
=> "<pretty_inspect/><foo>bar</foo>"
irb> puts xml
TypeError: can't convert Builder::XmlMarkup to Array (Builder::XmlMarkup#to_ary gives String)
from (pry):122:in `puts'
In a script where I'm using Builder to create the XML, I'm passing #xml to a POST:
response = HTTParty.post(API_ENDPOINT, :body => #xml)
This gives the same error:
TypeError (can't convert Builder::XmlMarkup to Array (Builder::XmlMarkup#to_ary gives String)):
Of course, if I do #xml.to_xml, it doesn't return an error, but it adds </to_xml> to the xml, meaning it isn't actually converting the XML object to xml. That's not what I want.
So how can I get access access to the xml so that I can pass it to my post without it adding extra nodes to my xml?
Edit: possible solution
Doing #xml.target! seems to resolve the issue, but I'm not sure I understand why.
response = HTTParty.post(API_ENDPOINT, :body => #xml.target!)
Perhaps someone can help me understand what is happening here.
Using
puts xml
is outputting the Builder::XmlMarkup object and hence give the error
Using
puts xml.target!
outputs the current xml string, which is what you want

Ruby: JSON.parse returns undefined method `bytesize' for

response = Typhoeus::Request.get("http://localhost:3000/api/api_email/#{#api_id}.json")
JSON.parse(response.body)
The response is a JSON object but I get an error when trying to parse it.
undefined method `bytesize' for
I want to get access to the JSON object.
Error:
NoMethodError at /api/v1/a71040739d6cc50e89aff56601af67/2011-10-1
undefined method `bytesize' for {"xpto"=>{"email
"=>"test#gmail.com"}}:Hash
file: utils.rb location: bytesize line: 239
BacktracE:
/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/httpserver.rb in service
si.service(req, res)
/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/httpserver.rb in run
server.service(req, res)
/Users/donald/.rvm/rubies/ruby-1.9.2-rc2/lib/ruby/1.9.1/webrick/server.rb in block in start_thread
block ? block.call(sock) : run(sock)
This is how is being generated:
#api_id = params[:api_id]
#bucket = Bucket.where(:api => #api_id)
respond_with(#bucket, :only => [:email])
The .json file being returned contains:
[{"xpto":{"email":"test#gmail.com"}}]
It's weird, as it seems that the response.body is already a Hash! (i.e. parsed JSON string), or, maybe you're seeing this in your webrick's log, and thus the problem is with generating the JSON response, rather than parsing it. The backtrace doesn't make sense :(
This is probably compatibility issue. You will probably not have that problem if you are using ruby1.9, but if you are using other versions/implementations such as ruby 1.8, or IronRuby, String#bytesize might not be defined.

Resources