4.5 Sentiment Over Time Check the structure of the date column print(df['date'].head())…
- User
4.5 Sentiment Over Time
Check the structure of the date column
print(df['date'].head())
Convert the 'date' column to datetime format
df['date'] = pd.to_datetime(df['date'], errors='coerce')
Drop any rows where 'date' couldn't be converted (if any exist)
df = df.dropna(subset=['date'])
Resample and count sentiments over time
sentiment_over_time = df.set_index('date').resample('D').sentiment.value_counts().unstack().fillna(0)
Plot sentiment over time
plt.figure(figsize=(15,7)) sentiment_over_time.plot(kind='line', marker='o') plt.title('Sentiment Over Time') plt.xlabel('Date') plt.ylabel('Number of Tweets') plt.legend(title='Sentiment') plt.show()
error: Series([], Name: date, dtype: datetime64[ns])
TypeError Traceback (most recent call last) in <cell line: 15>() 13 # Plot sentiment over time 14 plt.figure(figsize=(15,7)) ---> 15 sentiment_over_time.plot(kind='line', marker='o') 16 plt.title('Sentiment Over Time') 17 plt.xlabel('Date')
3 frames /usr/local/lib/python3.10/dist-packages/pandas/plotting/_matplotlib/core.py in _compute_plot_data(self) 634 # no non-numeric frames or series allowed 635 if is_empty: --> 636 raise TypeError("no numeric data to plot") 637 638 self.data = numeric_data.apply(self._convert_to_ndarray)
TypeError: no numeric data to plot
additional info: colums of database: Check the columns of the DataFrame print(df.columns)
Index(['sentiment', 'id', 'date', 'query', 'user', 'tweet', 'clean_tweet', 'tweet_length'], dtype='object')
- Assistant