Print a particular object from json in ruby - ruby-on-rails
How do I print only the text from the json. I am working on ruby on rails app.
tweets = JSON.parse(response.body)
tweets.each do |tweet|
"#{tweet["text"]}"
end
I tried the above code bt it displays the whole json.
Response.body :
[{"created_at"=>"Mon Jun 09 02:49:17 +0000 2014",
"id"=>475831972812423168, "id_str"=>"475831972812423168",
"text"=>"#debalec #DIST1", "source"=>"https://dev.twitter.com/docs/tfw\" rel=\"nofollow\">Twitter for
Websites", "truncated"=>false, "in_reply_to_status_id"=>nil,
"in_reply_to_status_id_str"=>nil, "in_reply_to_user_id"=>2551123651,
"in_reply_to_user_id_str"=>"2551123651",
"in_reply_to_screen_name"=>"debalec", "user"=>{"id"=>1551421092,
"id_str"=>"1551421092", "name"=>"The Third",
"screen_name"=>"thethird", "location"=>"", "description"=>"",
"url"=>nil, "entities"=>{"description"=>{"urls"=>[]}},
"protected"=>false, "followers_count"=>12, "friends_count"=>199,
"listed_count"=>0, "created_at"=>"Thu Jun 27 20:24:56 +0000 2013",
"favourites_count"=>54, "utc_offset"=>nil, "time_zone"=>nil,
"geo_enabled"=>false, "verified"=>false, "statuses_count"=>82,
"lang"=>"en", "contributors_enabled"=>false, "is_translator"=>false,
"is_translation_enabled"=>false, "profile_background_color"=>"59BEE4",
"profile_background_image_url"=>"http://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg",
"profile_background_image_url_https"=>"https://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg",
"profile_background_tile"=>false,
"profile_image_url"=>"http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
"profile_image_url_https"=>"https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
"profile_link_color"=>"8FCAE0",
"profile_sidebar_border_color"=>"4BB7DF",
"profile_sidebar_fill_color"=>"191F22",
"profile_text_color"=>"4BB7DF", "profile_use_background_image"=>true,
"default_profile"=>false, "default_profile_image"=>true,
"following"=>false, "follow_request_sent"=>false,
"notifications"=>false}, "geo"=>nil, "coordinates"=>nil, "place"=>nil,
"contributors"=>nil, "retweet_count"=>0, "favorite_count"=>0,
"entities"=>{"hashtags"=>[{"text"=>"DIST1", "indices"=>[10, 16]}],
"symbols"=>[], "urls"=>[],
"user_mentions"=>[{"screen_name"=>"debalec", "name"=>"DebaElec",
"id"=>2551123651, "id_str"=>"2551123651", "indices"=>[0, 8]}]},
"favorited"=>false, "retweeted"=>false, "lang"=>"und"},
{"created_at"=>"Fri Jun 06 22:41:39 +0000 2014",
"id"=>475044876841938944, "id_str"=>"475044876841938944", "text"=>"hi
#debalec", "source"=>"http://twitter.com/download/android\"
rel=\"nofollow\">Twitter for Android", "truncated"=>false,
"in_reply_to_status_id"=>nil, "in_reply_to_status_id_str"=>nil,
"in_reply_to_user_id"=>nil, "in_reply_to_user_id_str"=>nil,
"in_reply_to_screen_name"=>nil, "user"=>{"id"=>1551421092,
"id_str"=>"1551421092", "name"=>"the third",
"screen_name"=>"the third", "location"=>"", "description"=>"",
"url"=>nil, "entities"=>{"description"=>{"urls"=>[]}},
"protected"=>false, "followers_count"=>12, "friends_count"=>199,
"listed_count"=>0, "created_at"=>"Thu Jun 27 20:24:56 +0000 2013",
"favourites_count"=>54, "utc_offset"=>nil, "time_zone"=>nil,
"geo_enabled"=>false, "verified"=>false, "statuses_count"=>82,
"lang"=>"en", "contributors_enabled"=>false, "is_translator"=>false,
"is_translation_enabled"=>false, "profile_background_color"=>"59BEE4",
"profile_background_image_url"=>"http://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg","profile_background_image_url_https"=>"https://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg",
"profile_background_tile"=>false,"profile_image_url"=>"http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_image_url_https"=>"https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png",
"profile_link_color"=>"8FCAE0",
"profile_sidebar_border_color"=>"4BB7DF",
"profile_sidebar_fill_color"=>"191F22",
"profile_text_color"=>"4BB7DF", "profile_use_background_image"=>true,
"default_profile"=>false, "default_profile_image"=>true,
"following"=>false, "follow_request_sent"=>false,
"notifications"=>false}, "geo"=>nil, "coordinates"=>nil, "place"=>nil,
"contributors"=>nil, "retweet_count"=>0, "favorite_count"=>0,
"entities"=>{"hashtags"=>[], "symbols"=>[], "urls"=>[],
"user_mentions"=>[{"screen_name"=>"debalec", "name"=>"DebaElec",
"id"=>2551123651, "id_str"=>"2551123651", "indices"=>[3, 11]}]},
"favorited"=>false, "retweeted"=>false, "lang"=>"und"}]
Use map instead of each:
tweets = JSON.parse(response.body)
tweets.map do |tweet|
tweet["text"]
end
And there is no need to interpolate tweet["text"].
One problem here is that you're using quotes within quotes. Forgot to escape?
Another issue is that you're not printing anything. Merely returning the text in a loop does nothing and would always return the tweets object when the loop is complete.
So, here you go:
tweets = JSON.parse(response.body)
tweets.each do |tweet|
puts tweet["text"]
end
Update:
Well, I guess the quotes within quotes won't be a problem here as long as you use string interpolation. But it's still not the preferred way. I'd rather go with "#{tweet['text']}".
Related
How to convert a localized string date to Ruby Date object?
I have a date which works fine when using it as English > Date.strptime("Aug 02, 2015", "%b %d, %Y") Sun, 02 Aug 2015 However when I use it another language es, that doesnt work. E.g. > Date.strptime("ago 02, 2015", "%b %d, %Y") ArgumentError: invalid date The text strings ago 02, 2015 itself comes from another service, and I need to standardize it to a particular format such as > I18n.localize(Date.strptime("Aug 02, 2015", "%b %d, %Y"), format: "%m/%d/%Y") "08/02/2015" Is there a way to do this in Ruby, so that > I18n.localize(Date.strptime("ago 02, 2015", "%b %d, %Y"), format: "%m/%d/%Y") "08/02/2015"
I'm assuming you've already tried the following function which handles most scenarios for turning something into a DateTime object. #date = Date.parse("ago 02, 2015") Other than that you can try appending to the date constants so that it properly picks them up. Personally haven't tried this approach but might work for you? require 'date' Date::MONTHNAMES = [nil] + %w( Enero Febrero Marzo Abril Mayo Junio Julio Agosto Septiembre Octubre Noviembre Diciembre ) Date::DAYNAMES = %w( Lunes Martes Miércoles Jueves Viernes Sábado Domingo ) Date::ABBR_MONTHNAMES = [nil] + %w( Ene Feb Mar Abr May Jun Jul Ago Sep Oct Nov Dic ) Date::ABBR_DAYNAMES = %w( Lun Mar Mié Jue Vie Sáb Dom ) Lastly, have you considered using the Chronic Gem for date parsing? I believe it should handle cross language cases.
You can substitute the Spanish words with English first. You can do it with #gsub, I18n yaml lookup, or a dictionary method, e.g. dictionary = { "ago" => "Aug" } date = "ago 02, 2015" words = date.split words[0] = dictionary[words[0]] date = words.join(" ") # "Aug 02, 2015" Refactor it to take advantage of the power of OOP: class DateDictionary LOOKUP = { "ene" => "Jan", "ago" => "Aug" } def translate(date) words = date.split words[0] = LOOKUP[words[0]] date = words.join(" ") end end date = DateDictionary.new.translate("ago 02, 2015") # "Aug 02, 2015"
MS Graph findMeetingTimes API not sorting meeting time suggestions correctly
My problem is that the Microsoft Graph findMeetingTimes API is not sorting the meeting time suggestions like I would expect it. In the documentation it says: If there are multiple meeting time suggestions, the findMeetingTimes action first orders the suggestions by their computed confidence value from high to low. If there are suggestions with the same confidence, the action then orders these suggestions chronologically. But whenever I try the API I get a "meetingTimeSuggestionsResult" sorted like this one: Confidence: 100 || 24.08.2017 16:30:00 -> 24.08.2017 17:00:00 Confidence: 100 || 24.08.2017 16:00:00 -> 24.08.2017 16:30:00 Confidence: 100 || 24.08.2017 08:30:00 -> 24.08.2017 09:00:00 Confidence: 100 || 24.08.2017 09:00:00 -> 24.08.2017 09:30:00 Confidence: 100 || 24.08.2017 14:00:00 -> 24.08.2017 14:30:00 Confidence: 100 || 24.08.2017 14:30:00 -> 24.08.2017 15:00:00 Confidence: 50 || 24.08.2017 13:00:00 -> 24.08.2017 13:30:00 Confidence: 50 || 24.08.2017 13:30:00 -> 24.08.2017 14:00:00 Confidence: 50 || 24.08.2017 15:00:00 -> 24.08.2017 15:30:00 Confidence: 100 || 24.08.2017 11:30:00 -> 24.08.2017 12:00:00 Confidence: 100 || 24.08.2017 10:30:00 -> 24.08.2017 11:00:00 Confidence: 100 || 24.08.2017 11:00:00 -> 24.08.2017 11:30:00 Confidence: 100 || 24.08.2017 10:00:00 -> 24.08.2017 10:30:00 Confidence: 100 || 24.08.2017 09:30:00 -> 24.08.2017 10:00:00 Confidence: 100 || 24.08.2017 12:30:00 -> 24.08.2017 13:00:00 Confidence: 50 || 24.08.2017 15:30:00 -> 24.08.2017 16:00:00 Confidence: 100 || 24.08.2017 12:00:00 -> 24.08.2017 12:30:00
The documentation is outdated. That's the way the API used to work, but it was changed and unfortunately the doc wasn't updated. Instead of a simple sort chronologically, the API tries to be "smart" about how it orders the results. There are a number of factors that go into it, with a major one being the user's past behavior. Showing a tendency to meet at certain times of the day can up that in the ranking.
logstash Twitter error when geo_enable=true
I am trying to connect logstash to twitter. All the twitter that user.geo_enabled=false parsed ok, but of the other hand if user.geo_enabled=true I receive this error: Failed action. {:status=>400, :action=>["index", {:_id=>nil, :_index=>"twitter", :_type=>"logs", :_routing=>nil}, #<LogStash::Event:0xb1b69ba #metadata_accessors=#<LogStash::Util::Accessors:0x74b553ef #store={}, #lut={}>, #cancelled=false, #data={"created_at"=>"Wed Jul 06 16:15:26 +0000 2016", "id"=>750724847626493953, "id_str"=>"750724847626493953", "text"=>"#HillaryClinton I would never vote for you", "source"=>"Twitter for Android", "truncated"=>false, "in_reply_to_status_id"=>nil, "in_reply_to_status_id_str"=>nil, "in_reply_to_user_id"=>1339835893, "in_reply_to_user_id_str"=>"1339835893", "in_reply_to_screen_name"=>"HillaryClinton", "user"=>{"id"=>772001468, "id_str"=>"772001468", "name"=>"charles c hutchison", "screen_name"=>"49_mail", "location"=>nil, "url"=>nil, "description"=>nil, "protected"=>false, "verified"=>false, "followers_count"=>8, "friends_count"=>99, "listed_count"=>0, "favourites_count"=>8, "statuses_count"=>176, "created_at"=>"Tue Aug 21 18:22:06 +0000 2012", "utc_offset"=>nil, "time_zone"=>nil, "geo_enabled"=>true, "lang"=>"en", "contributors_enabled"=>false, "is_translator"=>false, "profile_background_color"=>"C0DEED", "profile_background_image_url"=>"http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https"=>"https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile"=>false, "profile_link_color"=>"0084B4", "profile_sidebar_border_color"=>"C0DEED", "profile_sidebar_fill_color"=>"DDEEF6", "profile_text_color"=>"333333", "profile_use_background_image"=>true, "profile_image_url"=>"http://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "profile_image_url_https"=>"https://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "default_profile"=>true, "default_profile_image"=>true, "following"=>nil, "follow_request_sent"=>nil, "notifications"=>nil}, "geo"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:6d26d63b,'0.384274583E2',9(12)>, #<BigDecimal:43f314b6,'-0.823958636E2',9(12)>]}, "coordinates"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:669bf464,'-0.823958636E2',9(12)>, #<BigDecimal:3d160aa5,'0.384274583E2',9(12)>]}, "place"=>{"id"=>"e4197a23034fa912", "url"=>"https://api.twitter.com/1.1/geo/id/e4197a23034fa912.json", "place_type"=>"city", "name"=>"Huntington", "full_name"=>"Huntington, WV", "country_code"=>"US", "country"=>"United States", "bounding_box"=>{"type"=>"Polygon", "coordinates"=>[[[#<BigDecimal:4feaddd3,'-0.82530433E2',8(12)>, #<BigDecimal:5438cd7c,'0.38375981E2',8(12)>], [#<BigDecimal:413b49ac,'-0.82530433E2',8(12)>, #<BigDecimal:58a6101d,'0.38439347E2',8(12)>], [#<BigDecimal:445e692e,'-0.82349236E2',8(12)>, #<BigDecimal:5f332e20,'0.38439347E2',8(12)>], [#<BigDecimal:46c19531,'-0.82349236E2',8(12)>, #<BigDecimal:71e183de,'0.38375981E2',8(12)>]]]}, "attributes"=>{}}, "contributors"=>nil, "is_quote_status"=>false, "retweet_count"=>0, "favorite_count"=>0, "entities"=>{"hashtags"=>[], "urls"=>[], "user_mentions"=>[{"screen_name"=>"HillaryClinton", "name"=>"Hillary Clinton", "id"=>1339835893, "id_str"=>"1339835893", "indices"=>[0, 15]}], "symbols"=>[]}, "favorited"=>false, "retweeted"=>false, "filter_level"=>"low", "lang"=>"en", "timestamp_ms"=>"1467821726124", "#version"=>"1", "#timestamp"=>"2016-07-06T16:15:26.000Z"}, #metadata={}, #accessors=#<LogStash::Util::Accessors:0x40cb306f #store={"created_at"=>"Wed Jul 06 16:15:26 +0000 2016", "id"=>750724847626493953, "id_str"=>"750724847626493953", "text"=>"#HillaryClinton I would never vote for you", "source"=>"Twitter for Android", "truncated"=>false, "in_reply_to_status_id"=>nil, "in_reply_to_status_id_str"=>nil, "in_reply_to_user_id"=>1339835893, "in_reply_to_user_id_str"=>"1339835893", "in_reply_to_screen_name"=>"HillaryClinton", "user"=>{"id"=>772001468, "id_str"=>"772001468", "name"=>"charles c hutchison", "screen_name"=>"49_mail", "location"=>nil, "url"=>nil, "description"=>nil, "protected"=>false, "verified"=>false, "followers_count"=>8, "friends_count"=>99, "listed_count"=>0, "favourites_count"=>8, "statuses_count"=>176, "created_at"=>"Tue Aug 21 18:22:06 +0000 2012", "utc_offset"=>nil, "time_zone"=>nil, "geo_enabled"=>true, "lang"=>"en", "contributors_enabled"=>false, "is_translator"=>false, "profile_background_color"=>"C0DEED", "profile_background_image_url"=>"http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https"=>"https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile"=>false, "profile_link_color"=>"0084B4", "profile_sidebar_border_color"=>"C0DEED", "profile_sidebar_fill_color"=>"DDEEF6", "profile_text_color"=>"333333", "profile_use_background_image"=>true, "profile_image_url"=>"http://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "profile_image_url_https"=>"https://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "default_profile"=>true, "default_profile_image"=>true, "following"=>nil, "follow_request_sent"=>nil, "notifications"=>nil}, "geo"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:6d26d63b,'0.384274583E2',9(12)>, #<BigDecimal:43f314b6,'-0.823958636E2',9(12)>]}, "coordinates"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:669bf464,'-0.823958636E2',9(12)>, #<BigDecimal:3d160aa5,'0.384274583E2',9(12)>]}, "place"=>{"id"=>"e4197a23034fa912", "url"=>"https://api.twitter.com/1.1/geo/id/e4197a23034fa912.json", "place_type"=>"city", "name"=>"Huntington", "full_name"=>"Huntington, WV", "country_code"=>"US", "country"=>"United States", "bounding_box"=>{"type"=>"Polygon", "coordinates"=>[[[#<BigDecimal:4feaddd3,'-0.82530433E2',8(12)>, #<BigDecimal:5438cd7c,'0.38375981E2',8(12)>], [#<BigDecimal:413b49ac,'-0.82530433E2',8(12)>, #<BigDecimal:58a6101d,'0.38439347E2',8(12)>], [#<BigDecimal:445e692e,'-0.82349236E2',8(12)>, #<BigDecimal:5f332e20,'0.38439347E2',8(12)>], [#<BigDecimal:46c19531,'-0.82349236E2',8(12)>, #<BigDecimal:71e183de,'0.38375981E2',8(12)>]]]}, "attributes"=>{}}, "contributors"=>nil, "is_quote_status"=>false, "retweet_count"=>0, "favorite_count"=>0, "entities"=>{"hashtags"=>[], "urls"=>[], "user_mentions"=>[{"screen_name"=>"HillaryClinton", "name"=>"Hillary Clinton", "id"=>1339835893, "id_str"=>"1339835893", "indices"=>[0, 15]}], "symbols"=>[]}, "favorited"=>false, "retweeted"=>false, "filter_level"=>"low", "lang"=>"en", "timestamp_ms"=>"1467821726124", "#version"=>"1", "#timestamp"=>"2016-07-06T16:15:26.000Z"}, #lut={"in-reply-to"=>[{"created_at"=>"Wed Jul 06 16:15:26 +0000 2016", "id"=>750724847626493953, "id_str"=>"750724847626493953", "text"=>"#HillaryClinton I would never vote for you", "source"=>"Twitter for Android", "truncated"=>false, "in_reply_to_status_id"=>nil, "in_reply_to_status_id_str"=>nil, "in_reply_to_user_id"=>1339835893, "in_reply_to_user_id_str"=>"1339835893", "in_reply_to_screen_name"=>"HillaryClinton", "user"=>{"id"=>772001468, "id_str"=>"772001468", "name"=>"charles c hutchison", "screen_name"=>"49_mail", "location"=>nil, "url"=>nil, "description"=>nil, "protected"=>false, "verified"=>false, "followers_count"=>8, "friends_count"=>99, "listed_count"=>0, "favourites_count"=>8, "statuses_count"=>176, "created_at"=>"Tue Aug 21 18:22:06 +0000 2012", "utc_offset"=>nil, "time_zone"=>nil, "geo_enabled"=>true, "lang"=>"en", "contributors_enabled"=>false, "is_translator"=>false, "profile_background_color"=>"C0DEED", "profile_background_image_url"=>"http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https"=>"https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile"=>false, "profile_link_color"=>"0084B4", "profile_sidebar_border_color"=>"C0DEED", "profile_sidebar_fill_color"=>"DDEEF6", "profile_text_color"=>"333333", "profile_use_background_image"=>true, "profile_image_url"=>"http://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "profile_image_url_https"=>"https://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "default_profile"=>true, "default_profile_image"=>true, "following"=>nil, "follow_request_sent"=>nil, "notifications"=>nil}, "geo"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:6d26d63b,'0.384274583E2',9(12)>, #<BigDecimal:43f314b6,'-0.823958636E2',9(12)>]}, "coordinates"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:669bf464,'-0.823958636E2',9(12)>, #<BigDecimal:3d160aa5,'0.384274583E2',9(12)>]}, "place"=>{"id"=>"e4197a23034fa912", "url"=>"https://api.twitter.com/1.1/geo/id/e4197a23034fa912.json", "place_type"=>"city", "name"=>"Huntington", "full_name"=>"Huntington, WV", "country_code"=>"US", "country"=>"United States", "bounding_box"=>{"type"=>"Polygon", "coordinates"=>[[[#<BigDecimal:4feaddd3,'-0.82530433E2',8(12)>, #<BigDecimal:5438cd7c,'0.38375981E2',8(12)>], [#<BigDecimal:413b49ac,'-0.82530433E2',8(12)>, #<BigDecimal:58a6101d,'0.38439347E2',8(12)>], [#<BigDecimal:445e692e,'-0.82349236E2',8(12)>, #<BigDecimal:5f332e20,'0.38439347E2',8(12)>], [#<BigDecimal:46c19531,'-0.82349236E2',8(12)>, #<BigDecimal:71e183de,'0.38375981E2',8(12)>]]]}, "attributes"=>{}}, "contributors"=>nil, "is_quote_status"=>false, "retweet_count"=>0, "favorite_count"=>0, "entities"=>{"hashtags"=>[], "urls"=>[], "user_mentions"=>[{"screen_name"=>"HillaryClinton", "name"=>"Hillary Clinton", "id"=>1339835893, "id_str"=>"1339835893", "indices"=>[0, 15]}], "symbols"=>[]}, "favorited"=>false, "retweeted"=>false, "filter_level"=>"low", "lang"=>"en", "timestamp_ms"=>"1467821726124", "#version"=>"1", "#timestamp"=>"2016-07-06T16:15:26.000Z"}, "in-reply-to"], "type"=>[{"created_at"=>"Wed Jul 06 16:15:26 +0000 2016", "id"=>750724847626493953, "id_str"=>"750724847626493953", "text"=>"#HillaryClinton I would never vote for you", "source"=>"Twitter for Android", "truncated"=>false, "in_reply_to_status_id"=>nil, "in_reply_to_status_id_str"=>nil, "in_reply_to_user_id"=>1339835893, "in_reply_to_user_id_str"=>"1339835893", "in_reply_to_screen_name"=>"HillaryClinton", "user"=>{"id"=>772001468, "id_str"=>"772001468", "name"=>"charles c hutchison", "screen_name"=>"49_mail", "location"=>nil, "url"=>nil, "description"=>nil, "protected"=>false, "verified"=>false, "followers_count"=>8, "friends_count"=>99, "listed_count"=>0, "favourites_count"=>8, "statuses_count"=>176, "created_at"=>"Tue Aug 21 18:22:06 +0000 2012", "utc_offset"=>nil, "time_zone"=>nil, "geo_enabled"=>true, "lang"=>"en", "contributors_enabled"=>false, "is_translator"=>false, "profile_background_color"=>"C0DEED", "profile_background_image_url"=>"http://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_image_url_https"=>"https://abs.twimg.com/images/themes/theme1/bg.png", "profile_background_tile"=>false, "profile_link_color"=>"0084B4", "profile_sidebar_border_color"=>"C0DEED", "profile_sidebar_fill_color"=>"DDEEF6", "profile_text_color"=>"333333", "profile_use_background_image"=>true, "profile_image_url"=>"http://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "profile_image_url_https"=>"https://abs.twimg.com/sticky/default_profile_images/default_profile_0_normal.png", "default_profile"=>true, "default_profile_image"=>true, "following"=>nil, "follow_request_sent"=>nil, "notifications"=>nil}, "geo"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:6d26d63b,'0.384274583E2',9(12)>, #<BigDecimal:43f314b6,'-0.823958636E2',9(12)>]}, "coordinates"=>{"type"=>"Point", "coordinates"=>[#<BigDecimal:669bf464,'-0.823958636E2',9(12)>, #<BigDecimal:3d160aa5,'0.384274583E2',9(12)>]}, "place"=>{"id"=>"e4197a23034fa912", "url"=>"https://api.twitter.com/1.1/geo/id/e4197a23034fa912.json", "place_type"=>"city", "name"=>"Huntington", "full_name"=>"Huntington, WV", "country_code"=>"US", "country"=>"United States", "bounding_box"=>{"type"=>"Polygon", "coordinates"=>[[[#<BigDecimal:4feaddd3,'-0.82530433E2',8(12)>, #<BigDecimal:5438cd7c,'0.38375981E2',8(12)>], [#<BigDecimal:413b49ac,'-0.82530433E2',8(12)>, #<BigDecimal:58a6101d,'0.38439347E2',8(12)>], [#<BigDecimal:445e692e,'-0.82349236E2',8(12)>, #<BigDecimal:5f332e20,'0.38439347E2',8(12)>], [#<BigDecimal:46c19531,'-0.82349236E2',8(12)>, #<BigDecimal:71e183de,'0.38375981E2',8(12)>]]]}, "attributes"=>{}}, "contributors"=>nil, "is_quote_status"=>false, "retweet_count"=>0, "favorite_count"=>0, "entities"=>{"hashtags"=>[], "urls"=>[], "user_mentions"=>[{"screen_name"=>"HillaryClinton", "name"=>"Hillary Clinton", "id"=>1339835893, "id_str"=>"1339835893", "indices"=>[0, 15]}], "symbols"=>[]}, "favorited"=>false, "retweeted"=>false, "filter_level"=>"low", "lang"=>"en", "timestamp_ms"=>"1467821726124", "#version"=>"1", "#timestamp"=>"2016-07-06T16:15:26.000Z"}, "type"]}>>], :response=>{"create"=>{"_index"=>"twitter", "_type"=>"logs", "_id"=>"AVXA_h0IgT1Xitpna0uT", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse", "caused_by"=>{"type"=>"illegal_state_exception", "reason"=>"Mixing up field types: class org.elasticsearch.index.mapper.core.DoubleFieldMapper$DoubleFieldType != class org.elasticsearch.index.mapper.geo.BaseGeoPointFieldMapper$GeoPointFieldType on field coordinates.coordinates"}}}}, :level=>:warn} I think that the relevent line is {"create"=>{"_index"=>"twitter", "_type"=>"logs", "_id"=>"AVXA_h0IgT1Xitpna0uT", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse", "caused_by"=>{"type"=>"illegal_state_exception", "reason"=>"Mixing up field types: class org.elasticsearch.index.mapper.core.DoubleFieldMapper$DoubleFieldType != class org.elasticsearch.index.mapper.geo.BaseGeoPointFieldMapper$GeoPointFieldType on field coordinates.coordinates"}}}}, :level=>:warn} my template is : { "template": "twitter", "order": 1, "settings": { "number_of_shards": 1 }, "mappings": { "tweet": { "_all": { "enabled": false }, "properties": { "coordinates": { "properties": { "coordinates": { "type": "geo_point" }, "type": { "type": "string" } } } } } } } What can be the problem?? thanks.
The problem is that the field coordinates.coordinates in ES expect a type of data which is not the type received. A solution would be to modify the template, removing the coordinates type. Then you delete your index and reindex your data. In that case the expected type of coordinates.coordinates will be dependent on the data inserted. That should resolve the problem.
Is the Time object suitable to create a calendar?
I want to make a database-backed calendar. Will the Time object make my life easier? It hasn't so far... The .end_of_year method gives me some strange information. If it's contemporary time it works flawlessly: date = '2012-3-2'.to_time(:utc) #=> 2012-03-02 00:00:00 UTC date.end_of_year #=> 2012-12-31 23:59:59 UTC However, if you go back in time things get strange. date = '1399-3-2'.to_time(:utc) #=> 1399-03-02 00:00:00 UTC date.end_of_year #=> 1399-12-23 23:59:59 UTC 23rd of December? Shouldn't that be 31st? It's not even consistent: date = '0000-3-2'.to_time(:utc) #=> 0000-03-02 00:00:00 UTC date.end_of_year #=> 0001-01-02 23:59:59 UTC Um, the 2nd of January? OF THE NEXT YEAR? What is going on? Also, are leap years taken into account by the object?
You could use DateTime instead: date = '2012-3-2'.to_datetime #=> Fri, 02 Mar 2012 00:00:00 +0000 date.end_of_year #=> Mon, 31 Dec 2012 23:59:59 +0000 date = '1399-3-2'.to_datetime #=> Sun, 02 Mar 1399 00:00:00 +0000 date.end_of_year #=> Wed, 31 Dec 1399 23:59:59 +0000 date = '0000-3-2'.to_datetime #=> Tue, 02 Mar 0000 00:00:00 +0000 date.end_of_year #=> Fri, 31 Dec 0000 23:59:59 +0000 It's mora accurate, and you can format the output
I've did some digging. Here's what I found. Let's begin with end_of_year: def end_of_year change(:month => 12).end_of_month end Which relies on change and end_of_month: def end_of_month last_day = ::Time.days_in_month(month, year) last_hour{ days_since(last_day - day) } end The most interesting part is happening inside of days_since: def days_since(days) advance(:days => days) end The advance method is a bit more complex: def advance(options) unless options[:weeks].nil? options[:weeks], partial_weeks = options[:weeks].divmod(1) options[:days] = options.fetch(:days, 0) + 7 * partial_weeks end unless options[:days].nil? options[:days], partial_days = options[:days].divmod(1) options[:hours] = options.fetch(:hours, 0) + 24 * partial_days end d = to_date.advance(options) time_advanced_by_date = change(:year => d.year, :month => d.month, :day => d.day) seconds_to_advance = options.fetch(:seconds, 0) + options.fetch(:minutes, 0) * 60 + options.fetch(:hours, 0) * 3600 if seconds_to_advance.zero? time_advanced_by_date else time_advanced_by_date.since(seconds_to_advance) end end And he is the guy we're looking for : # in rails console time = '0000-01-01'.to_time(:utc) #=> 0000-01-01 00:00:00 UTC time.advance(days: 1) #=> 0000-01-04 00:00:00 UTC time.advance(days: 2) #=> 0000-01-05 00:00:00 UTC time.advance(days: 3) #=> 0000-01-06 00:00:00 UTC That's all for now. I will continue to dig.
Joining Multiple Dataframes with Pandas with overlapping Column Names?
I have multiple (more than 2) dataframes I would like to merge. They all share the same value column: In [431]: [x.head() for x in data] Out[431]: [ AvgStatisticData DateTime 2012-10-14 14:00:00 39.335996 2012-10-14 15:00:00 40.210110 2012-10-14 16:00:00 48.282816 2012-10-14 17:00:00 40.593039 2012-10-14 18:00:00 40.952014, AvgStatisticData DateTime 2012-10-14 14:00:00 47.854712 2012-10-14 15:00:00 55.041512 2012-10-14 16:00:00 55.488026 2012-10-14 17:00:00 51.688483 2012-10-14 18:00:00 57.916672, AvgStatisticData DateTime 2012-10-14 14:00:00 54.171233 2012-10-14 15:00:00 48.718387 2012-10-14 16:00:00 59.978616 2012-10-14 17:00:00 50.984514 2012-10-14 18:00:00 54.924745, AvgStatisticData DateTime 2012-10-14 14:00:00 65.813114 2012-10-14 15:00:00 71.397868 2012-10-14 16:00:00 76.213973 2012-10-14 17:00:00 72.729002 2012-10-14 18:00:00 73.196415, ....etc I read that join can handle multiple dataframes, however I get: In [432]: data[0].join(data[1:]) ... Exception: Indexes have overlapping values: ['AvgStatisticData'] I have tried passing rsuffix=["%i" % (i) for i in range(len(data))] to join and still get the same error. I can workaround this by building my data list in a way where the column names don't overlap, but maybe there is a better way?
In [65]: pd.concat(data, axis=1) Out[65]: AvgStatisticData AvgStatisticData AvgStatisticData AvgStatisticData 2012-10-14 14:00:00 39.335996 47.854712 54.171233 65.813114 2012-10-14 15:00:00 40.210110 55.041512 48.718387 71.397868 2012-10-14 16:00:00 48.282816 55.488026 59.978616 76.213973 2012-10-14 17:00:00 40.593039 51.688483 50.984514 72.729002 2012-10-14 18:00:00 40.952014 57.916672 54.924745 73.196415
I would try pandas.merge using the suffixes= option. import pandas as pd import datetime as dt df_1 = pd.DataFrame({'x' : [dt.datetime(2012,10,21) + dt.timedelta(n) for n in range(10)], 'y' : range(10)}) df_2 = pd.DataFrame({'x' : [dt.datetime(2012,10,21) + dt.timedelta(n) for n in range(10)], 'y' : range(10)}) df = pd.merge(df_1, df_2, on='x', suffixes=['_1', '_2']) I am interested to see if the experts have a more algorithmic approach to merge a list of data frames.