我正在使用代码(由Marco Bonzanini编写)来绘制一段时间内推文的频率。我的推文包含在JSONL文件中。不幸的是,当在终端中运行代码时,我得到上面提到的错误。如何操作代码以便Numpy可以正确转换日期时间?
(我使用Python 3.6,我也确实看到了类似的问题,但我不知道如何将答案纳入代码中,因为它完全不同)。
import sys
import json
from datetime import datetime
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
import numpy as np
import pickle
if __name__ == '__main__':
fname = sys.argv[1]
with open(fname, 'r') as f:
all_dates = []
for line in f:
tweet = json.loads(line)
all_dates.append(tweet.get('created_at'))
idx = pd.DatetimeIndex(all_dates)
ones = np.ones(len(all_dates))
# the actual series (at series of 1s for the moment)
my_series = pd.Series(ones, index=idx)
# Resampling / bucketing into 1-minute buckets
per_minute = my_series.resample('1Min', how='sum').fillna(0)
# Plotting the series
fig, ax = plt.subplots()
ax.grid(True)
ax.set_title("Tweet Frequencies")
hours = mdates.MinuteLocator(interval=20)
date_formatter = mdates.DateFormatter('%H:%M')
datemin = datetime(2011, 10, 31, 15, 0)
datemax = datetime(2017, 10, 31, 18, 0)
ax.xaxis.set_major_locator(hours)
ax.xaxis.set_major_formatter(date_formatter)
ax.set_xlim(datemin, datemax)
max_freq = per_minute.max()
ax.set_ylim(0, max_freq)
ax.plot(per_minute.index, per_minute)
plt.savefig('tweet_time_series.png')
Json Lines Data看起来像这样(两条推文,第二行从:{" created_at":" 10月24日星期五......)
{" created_at":" Fri Oct 24 20:21:15 +0000 2014"," id":525743844839522305," id_str&#34 ;:" 525743844839522305","文本&#34 ;:" \ u0628 \ u0644 \ u0623 \ u0645 \ u0644 \ u0627 \ u064b \ u0648 \ u0634 \ u0645 \ u0633 \ u0627 \ u0648 \ u0642 \ u0645 \ u062d \ u0627 \ n \ u062a \ u0639 \ u0644 \ u0645 \ u0646 \ u0627 \ u0645 \ u0646 \ u0643 \ u0633 \ u064a \ u062f \ u064a \ u0628 \ u0633 \ u0637 \ u0627 \ u0644 \ u0631 \ u0648 \ u062d \ u0648 \ u0627 \ u0644 \ u0643 \ u0641 \ u0646 \ n \ u0648 \ u062a \ u0639 \ u0644 \ u0645 \ u0646 \ u0627 \ u0645 \ u0627 \ u0645 \ u0639 \ u0646 \ u0649 \ u0627 \ u0644 \ u0648 \ u0637 \ u0646 \ n \ u0641 \ u0645 \ u0648 \ u0627 \ u062d \ u062f \ u064a \ u0643 \ u0641 \ u064a \ u0644 \ u0643 \ u0644 \ u0627 \ u0644 \ u0644 \ u063a \ u0627 \ u062a \ n \ u0637 \ u0627 \ u0644 \ u0645 \ u0627 \ u0627 \ u0646 \ u0627 \ u0644 \ u0645 \ u0648 \ u062a \ u062d \ u0627 \ u0644 \ u0629 \ u0645 \ u0646 \ u062d \ u0627 \ u0644 \ u0627 \ u062a \ u0627 \ u0644 \ u062d \ u064a \ u0627 \ u0629","截断":false,"实体":{" hashtags":[],"符号":[] ," user_mentions":[],"网址":[]},"来源":" http://twitter.com/download/iphone \" rel = \" nofollow \"> Twitter for iPhone"," in_reply_to_status_id":null," in_reply_to_status_id_str":null," in_reply_to_user_id&# 34;:null," in_reply_to_user_id_str":null," in_reply_to_screen_name":null," user":{" id":1948848392,&# 34; id_str":" 1948848392"," name":" Ali Bazzi"," screen_name":" ViceAliBazzi&# 34;," location":""," description":" \ u0645 \ u0627 \ u0632 \ u0627 \ u0644 \ u0647 \ u0646 \ u0627 \ u0643 \ u0623 \ u0645 \ u0644 \ u064a \ u0633 \ u062a \ u0648 \ u0637 \ u0646 \ u062a \ u062c \ u0627 \ u0639 \ u064a \ u062f \ u0627 \ u0644 \ u0633 \ u0646 \ u064a \ u0646。&# 34;," url":null," entity":{" description":{" urls":[]}},&# 34; protected":false," followers_count":34918," friends_count":362," listed_count":13," created_at" :" Wed Oct 09 07:55:26 +0000 2013"," favourites_count":6016," utc_offse t":null," time_zone":null," geo_enabled":true,"已验证":false," statuses_count":5188 ," lang":" en"," contributors_enabled":false," is_translator":false," is_translation_enabled": false," profile_background_color":" C0DEED"," profile_background_image_url":" http://abs.twimg.com/images/themes/theme1/bg.png"," profile_background_image_url_https&#34 ;:" https://abs.twimg.com/images/themes/theme1/bg.png"," profile_background_tile":false," profile_image_url":" http://pbs.twimg.com/profile_images/883631999499612161/6Y9QbL52_normal.jpg", " profile_image_url_https":" https://pbs.twimg.com/profile_images/883631999499612161/6Y9QbL52_normal.jpg"," profile_banner_url":" https://pbs.twimg.com/profile_banners/1948848392/1415834468"," profile_link_color":" 1DA1F2"," profile_sidebar_border_color":" C0DEED"," profile_sidebar_fill_color":" DDEEF6", " profile_text_color":" 333333"," profile_use_background_image":true," has_extended_profile":false," default_profile":true ," default_profile_image":false, "跟随":false," follow_request_sent":false," notifications":false," translator_type":" none" }," geo":null," coordinates":null," place&#34 ;: null," contributors":null," is_quote_status":false," retweet_count":14," favorite_count":21," favited":false,"转发":false ," lang":" ar"} {" created_at":" Fri Oct 24 20:18:39 +0000 2014"," id":525743192868519936," id_str":& #34; 525743192868519936","文本&#34 ;:" \ u0645 \ u0646 \ u0630 \ u0643 \ u0631 \ u0628 \ u0644 \ u0627 \ u0621 \ u0648 \ u0646 \ u062d \ u0646 \ u0646 \ u0639 \ u0634 \ u0642 \ u0627 \ u0644 \ u062e \ u064a \ u0631 \ u0648 \ u0627 \ u0644 \ u0623 \ u0646 \ u0628 \ u064a \ u0627 \ u0621 \ n \ u062f \ u0645 \ u0646 \ u0627 \ u0639 \ u0644 \ u0649 \ u0627 \ u0644 \ u0648 \ u062f \ u064a \ u0627 \ u0646 \ n \ u0648 \ u0623 \ u0634 \ u0644 \ u0627 \ u0624 \ u0646 \ u0627 \ u0645 \ u0639 \ u0644 \ u0642 \ u0629 \ u0628 \ u064a \ u0646 \ u0627 \ u0644 \ u0648 \ u062c \ u0639 \ u0648 \ u0627 \ u0644 \ u0633 \ u0645 \ u0627 \ u0621 \ n \ u062a \ u0639 \ u0644 \ u0645 \ u0646 \ u0627 \ u0645 \ u0646 \ u0643 \ u0633 \ u064a \ u062f \ u064a \ u0627 \ u0646 \ u0627 \ u0644 \ u0645 \ u0648 \ u062a \ u0644 \ u0627 \ u064a \ u0635 \ u0646 \ u0639 \ u062c \ u0631 \ u062d \ u0627 \ N(\ u062a \ u0627 \ u0628 \ u0639)&#34 ;, "截断":false,"实体":{" hashtags":[],"符号":[]," user_mentions& #34;:[]," urls":[]},"所以urce":" http://twitter.com/download/iphone \" rel = \" nofollow \"> Twitter for iPhone"," in_reply_to_status_id":null," in_reply_to_status_id_str":null," in_reply_to_user_id&# 34;:null," in_reply_to_user_id_str":null," in_reply_to_screen_name":null," user":{" id":1948848392,&# 34; id_str":" 1948848392"," name":" Ali Bazzi"," screen_name":" ViceAliBazzi&# 34;," location":""," description":" \ u0645 \ u0627 \ u0632 \ u0627 \ u0644 \ u0647 \ u0646 \ u0627 \ u0643 \ u0623 \ u0645 \ u0644 \ u064a \ u0633 \ u062a \ u0648 \ u0637 \ u0646 \ u062a \ u062c \ u0627 \ u0639 \ u064a \ u062f \ u0627 \ u0644 \ u0633 \ u0646 \ u064a \ u0646。&# 34;," url":null," entity":{" description":{" urls":[]}},&# 34; protected":false," followers_count":34918," friends_count":362," listed_count":13," created_at" :" Wed Oct 09 07:55:26 +0000 2013"," favourites_count":6016," utc_offse t":null," time_zone":null," geo_enabled":true,"已验证":false," statuses_count":5188 ," lang":" en"," contributors_enabled":false," is_translator":false," is_translation_enabled": false," profile_background_color":" C0DEED"," profile_background_image_url":" http://abs.twimg.com/images/themes/theme1/bg.png"," profile_background_image_url_https&#34 ;:" https://abs.twimg.com/images/themes/theme1/bg.png"," profile_background_tile":false," profile_image_url":" http://pbs.twimg.com/profile_images/883631999499612161/6Y9QbL52_normal.jpg", " profile_image_url_https":" https://pbs.twimg.com/profile_images/883631999499612161/6Y9QbL52_normal.jpg"," profile_banner_url":" https://pbs.twimg.com/profile_banners/1948848392/1415834468"," profile_link_color":" 1DA1F2"," profile_sidebar_border_color":" C0DEED"," profile_sidebar_fill_color":" DDEEF6", " profile_text_color":" 333333"," profile_use_background_image":true," has_extended_profile":false," default_profile":true ," default_profile_image":false, "跟随":false," follow_request_sent":false," notifications":false," translator_type":" none" }," geo":null," coordinates":null," place&#34 ;: null," contributors":null," is_quote_status":false," retweet_count":13," favorite_count":19," favited":false,"转发":false ," lang":" ar"}