Python Convert Datetime from csv for matplotlib
<p>I am trying to plot from a csv file with column 1 as a datetime value, as below</p>

<pre><code> 27-08-2016 08:43   21.38329164
</code></pre>

<p>using this code:</p>

<pre><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()
</code></pre>

<p>I am getting this error:</p>

<pre><code>    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
</code></pre>

<p>Please help! 
Thanks</p>

<p>Source: <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html" rel="nofollow">numpy.genfromtxt</a></p>

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

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

<p>Please update your code as follows:</p>

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

<p>For example,</p>

<p>If your data file is structured like this</p>

<pre><code>col1, col2, col3
   1,    2,    3
  10,   20,   30
 100,  200,  300
</code></pre>

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

<pre><code>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.])
</code></pre>

<p>Since in this case the data is formed like this</p>

<pre><code>row1,   1,  10, 100
row2,   2,  20, 200
row3,   3,  30, 300
</code></pre>

<p>you can achieve something similar using the following code snippet:</p>

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

<p>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. </p>

<p>if you want to plot col1 and col2, you can simply define:</p>

<pre><code>x=data['col1']
y=data['col2']
</code></pre>

python csv pandas matplotlib genfromtxt