wazzahenry wazzahenry - 2 months ago 12
Python Question

Python Convert Datetime from csv for matplotlib

I am trying to plot from a csv file with column 1 as a datetime value, as below

27-08-2016 08:43 21.38329164


using this code:

from matplotlib import pyplot as plt
from matplotlib import style
import numpy as np
import datetime as dt
from datetime import datetime
import matplotlib.dates as mdates

style.use('ggplot')

x,y = np.genfromtxt('I112-1a.csv', unpack=True,dtype=None, delimiter = ',', converters={0: lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M')})


plt.title('Panel Charge')
plt.ylabel('Y axis')
plt.xlabel('X axis')
plt.show()


I am getting this error:

x,y = np.genfromtxt('I112-1a.csv', unpack=True,dtype=None, delimiter = ',', converters={0: lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M')})
ValueError: too many values to unpack


Please help!
Thanks

Answer

Source: numpy.genfromtxt

numpy.genfromtxt  Returns:ndarray
Data read from the text file. If usemask is True, this is a masked array.

@Ev. Kounis pointed out in comment section: numpy.genfromtxt returns only one array and you are assigning it to two parameters x and y.

Please update your code as follows:

data = np.genfromtxt('I112-1a.csv', unpack=True,dtype=None, delimiter = ',', converters={0: lambda x: datetime.strptime(x, '%d-%m-%Y %H:%M')})

For example,

If your data file is structured like this

col1, col2, col3
   1,    2,    3
  10,   20,   30
 100,  200,  300

then numpy.genfromtxt can interpret the first line as column headers using the names=True option. With this you can access the data very conveniently by providing the column header:

data = np.genfromtxt('data.txt', delimiter=',', names=True)
print data['col1']    # Output: array([   1.,   10.,  100.])
print data['col2']    # Output: array([   2.,   20.,  200.])
print data['col3']    # Output: array([   3.,   30.,  300.])

Since in this case the data is formed like this

row1,   1,  10, 100
row2,   2,  20, 200
row3,   3,  30, 300

you can achieve something similar using the following code snippet:

labels = np.genfromtxt('data.txt', delimiter=',', usecols=0, dtype=str)
raw_data = np.genfromtxt('data.txt', delimiter=',')[:,1:]

The first line reads the first column (the labels) into an array of strings. The second line reads all data from the file but discards the first column. This is how you can extract columns in different variables.

if you want to plot col1 and col2, you can simply define:

x=data['col1']
y=data['col2']