3

I get ValueError: microsecond must be in 0..999999 when I try to plot two series using scatter plot.

I have two datasets(contains the posts made on a platform with the time they where created and number of comments each post receiced) the goal here is to understand what time if a post was created it will likely create a big number of comments.

hn_ask_sorted_data = hn_ask_data.sort_values(by = ['num_comments'],ascending=False)
hn_show_sorted_data = hn_show_data.sort_values(by = ['num_comments'],ascending=False)

hn_ask_sorted_data['created_at'] = pd.to_datetime(hn_ask_sorted_data['created_at'])
hn_show_sorted_data['created_at'] = pd.to_datetime(hn_show_sorted_data['created_at'])

I convert the column that contains time into a datetime object, but I am more interested with the time component of the object hence I only take the time component using the .dt.time

hn_ask_sorted_data['created_at'] = hn_ask_sorted_data['created_at'].dt.time
hn_show_sorted_data['created_at'] = hn_show_sorted_data['created_at'].dt.time

Then I make a scatterplot using two columns one containing number of comments on the post and the time during which the post was posted (ie the above created column) instead of getting the results I get the described error.

plt.scatter(hn_ask_sorted_data['created_at'],hn_ask_sorted_data['num_comments'])
plt.show()
plt.scatter(hn_show_sorted_data['created_at'],hn_show_sorted_data['num_comments'])
plt.show()
  • 1
    how do your timestamps look like, especially the microsecond component? sometimes if you have nanosecond precision (datetime64[ns]), this can cause problems if you pass them to objects that expect 'normal' datetime objects, i.e. with microsecond precision. Anyways, if you don't care about the µs, you could replace them with 0 as a work-around. – FObersteiner May 02 '20 at 13:55
  • 1
    Thank you @MrFuppes , after more research I have found out that plt.scatter was expecting a datetime object which is full ie date and time not time only. So if I feed that instead it pass. I am still researching I will post full finding. – Nickson Ndangalasi May 02 '20 at 14:12
  • Ok sorry if I pointed in the wrong direction there. I wasn't aware of the fact that `scatter` expects full datetime - can't think of a reason why that is either... so I'd be interested what you find out. – FObersteiner May 02 '20 at 14:19

0 Answers0