0

From a whole data set, I need to plot the maximum & minimum temperatures for just the months of January and July. Column 2 is the date, and columns 8 and 9 are the 'TMAX' and 'TMIN.' This is what I have so far:

napa3=pd.read_csv('MET 51 Lab #10 data (Pandas, NAPA).csv',usecols=[2,8,9])
time2=pd.to_datetime(napa3['DATE'],format='%Y-%m-%d')
imon=time2.dt.month
jj=(imon==1)&(imon==7)
data_jj=napa3.loc[jj]
data_jj.plot.hist(title='TMAX & TMIN for January and July')
plt.show()

I keep getting the error: "TypeError: no numeric data to plot" Why is this?

Stefan
  • 37
  • 5

1 Answers1

0

The problem can raise because the dates are saved as an "object" or a string. However, I can't see that you have created dataframe?! you do read_csv but you do not make dataframe out of that:

dnapa3 = pd.DataFrame(napa3)

then repeat converting your time data and check:

print(dnapa3.dtypes)

after you became sure that your requested column values are string or object you can change the values of that column to floats:

dnapa3['your_temp_column_label'] = dnapa3['your_date_column_label'].astype(float)

This should work hopefully. Or silmilarly :

dnapa3['your_tem_column_label'] =pd.to_numeric(dnapa3['your_date_column_label'], errors='coerce')
Anya Samadi
  • 673
  • 1
  • 5
  • 13
  • When I type the .astype(float) function, it says "could not convert string to float: '1978-01-01'. When I try the pd.to_numeric function, it says "no numeric data to plot." It's weird though, both your method and my original method still work when I only plot (imon==1) but not both (imon==1) & (imon==7). I wonder why? – Stefan May 17 '20 at 21:24
  • When I type my temp column label name, can I put both temp column names, ('TMAX','TMIN')=dnapa3['DATE'].astype(float) ? – Stefan May 17 '20 at 21:26
  • For your first comment, I can say when you are converting your date data to numbers is there may be some nondefined types that raise the errors for both to enumerate and astype! you can add , errors='ignore', or errors='coerce' as : time2=pd.to_datetime(napa3['DATE'],format='%Y-%m-%d', errors='coerce'). see if it works. – Anya Samadi May 18 '20 at 16:42
  • for your second comment check the examples here: https://www.geeksforgeeks.org/change-data-type-for-one-or-more-columns-in-pandas-dataframe/ or check this post: https://stackoverflow.com/questions/15891038/change-data-type-of-columns-in-pandas/16134561 – Anya Samadi May 18 '20 at 16:44