0

links to "Stackoverflow question" I am new to Python and tried the program in the links however, I am getting the below error, am I missing anything here? Could you point me out?

...............................

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({
          'weight':[1, 2, 3, 4],
          'date':['2011-05-01', '2013-10-10', '2015-12-17', '2017-10-29']
          })
df['date'] = pd.to_datetime(df['date'])

df1 = df.groupby(df['date'].dt.to_period('M')).sum()

df1 = df1.resample('M').asfreq().fillna(0)

plt.figure();

df1.plot(x='date', y='weight', kind='bar')


.......................................... Traceback (most recent call last):

File "/PycharmProjects/hellopython/hello_world", line 24, in

df1.plot(x='date', y='weight', kind='bar')
File "\PycharmProjects\hellopython\venv\lib\site-

packages\pandas\plotting_core.py", line 920, in call

elif not isinstance(data[x], ABCSeries):
File "\PycharmProjects\hellopython\venv\lib\site-packages\pandas\core\frame.py", line 3024, in getitem

indexer = self.columns.get_loc(key)
File "\PycharmProjects\hellopython\venv\lib\site-

packages\pandas\core\indexes\base.py", line 3082, in get_loc

raise KeyError(key) from err
KeyError: 'date'
Jimit Vaghela
  • 768
  • 1
  • 8
  • 31
ksi
  • 3
  • 1

1 Answers1

1

Two options

Option 1

Since date is as index, then we don't need to pass x and y axis to plot. Thanks to @Nk03 for suggesting this option.

Modified below line in existing code

df1.plot(kind='bar')

Code

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({
          'weight':[1, 2, 3, 4],
          'date':['2011-05-01', '2013-10-10', '2015-12-17', '2017-10-29']
          })
df['date'] = pd.to_datetime(df['date'])

df1 = df.groupby(df['date'].dt.to_period('M')).sum()

df1 = df1.resample('M').asfreq().fillna(0)

plt.figure();

df1.plot(kind='bar')

Option 2 - Reset index

Added below line to existing code

df1 = df1.reset_index()

Complete code

import matplotlib.pyplot as plt
import pandas as pd

df = pd.DataFrame({
          'weight':[1, 2, 3, 4],
          'date':['2011-05-01', '2013-10-10', '2015-12-17', '2017-10-29']
          })
df['date'] = pd.to_datetime(df['date'])

df1 = df.groupby(df['date'].dt.to_period('M')).sum()

df1 = df1.resample('M').asfreq().fillna(0)
df1 = df1.reset_index()

plt.figure();

df1.plot(x='date', y='weight', kind='bar')
Utsav
  • 5,572
  • 2
  • 29
  • 43
  • Hi, Thanks a lot. It is working however I just tried it in this way also like Option2 df1.plot(x='weight', y='date', kind='bar') changed x and y and it is giving compiler error as "TypeError("no numeric data to plot") TypeError: no numeric data to plot". Is it okay here to change x and y or it should not be changed? Can you please correct me on this too? Thanks in advance. – ksi Jun 08 '21 at 00:17
  • If you want to plot non-numeric types like date on y axis then plz [visit](https://stackoverflow.com/questions/19054524/horizontal-bar-chart-with-date-in-y-axis) – Utsav Jun 08 '21 at 04:07