Xiaoshen Sun Xiaoshen Sun -4 years ago 232
Python Question

invalid literal for float(): 01/06/2017 22:00

The data is from csv file and I read it with pandas.I used pyplot to plot, it should be just like: x as the time, y as the wind speed. I think the problem is caused by selection or indexing. I only want the data from row 22-25 and then plot it. Here are the data and codes:

time Unnamed: 2
22 01/06/2017 13:00 23
23 01/06/2017 16:00 22
24 01/06/2017 19:00 15
25 01/06/2017 22:00 10

import pandas as pd
import matplotlib.pyplot as plt
a = pd.read_csv('J:/internship/forecast/6.1/forecast_report_Bahrain.csv')
m10wind = a['Unnamed: 2'][22:26]
time= a['time'][22:26]
fig, ax = plt.subplots()
fig,ax.set_ylabel("10m wind speed (knots)")
ax.plot(time, m10wind, 'r', label='GFS Forecast')
leg = ax.legend(loc=2, bbox_to_anchor=(1.05, 1.0))
plt.savefig('1st June wind of Bahrain.png', bbox_inches='tight')

full error messages:

Traceback (most recent call last):

File "<ipython-input-37-9ab81b2b3775>", line 1, in <module>
runfile('C:/Users/xiaoshen.sun/.spyder/temp.py', wdir='C:/Users/xiaoshen.sun/.spyder')

File "C:\tools\Python2.7\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
execfile(filename, namespace)

File "C:\tools\Python2.7\lib\site-packages\spyder\utils\site\sitecustomize.py", line 87, in execfile
exec(compile(scripttext, filename, 'exec'), glob, loc)

File "C:/Users/xiaoshen.sun/.spyder/temp.py", line 18, in <module>
ax.plot(time, m10wind, 'r', label='GFS Forecast')

File "C:\tools\Python2.7\lib\site-packages\matplotlib\__init__.py", line 1891, in inner
return func(ax, *args, **kwargs)

File "C:\tools\Python2.7\lib\site-packages\matplotlib\axes\_axes.py", line 1407, in plot

File "C:\tools\Python2.7\lib\site-packages\matplotlib\axes\_base.py", line 1787, in add_line

File "C:\tools\Python2.7\lib\site-packages\matplotlib\axes\_base.py", line 1809, in _update_line_limits
path = line.get_path()

File "C:\tools\Python2.7\lib\site-packages\matplotlib\lines.py", line 989, in get_path

File "C:\tools\Python2.7\lib\site-packages\matplotlib\lines.py", line 676, in recache
x = np.asarray(xconv, np.float_)

File "C:\tools\Python2.7\lib\site-packages\numpy\core\numeric.py", line 482, in asarray
return array(a, dtype, copy=False, order=order)

ValueError: invalid literal for float(): 01/06/2017 22:00

Answer Source

Your issue (likely) relates to the datetime column not being formatted as such. I was able to convert the column using pd.to_datetime() and plot this data without any issues. Your plotting is also incorrect and will probably throw an error- fig, ax is meant to unpack a tuple. ax contains the plot method (see below).

                  time  Unnamed: 2
22 2017-01-06 13:00:00          23
23 2017-01-06 16:00:00          22
24 2017-01-06 19:00:00          15
25 2017-01-06 22:00:00          10


df.time = pd.to_datetime(df.time)


fig, ax = plt.subplots()
ax.plot(df.time, df['Unnamed: 2'])

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download