Load_converter: TypeError: strptime() argument 0 must be str, not

from __future__ import print_function
from matplotlib.dates import strpdate2num
#from matplotlib.mlab import load
import numpy as np
from pylab import figure, show
import matplotlib.cbook as cbook

datafile = cbook.get_sample_data('msft.csv', asfileobj=False)
print('loading', datafile)

dates, closes = np.loadtxt(
    datafile, delimiter=',',
    converters={0:strpdate2num('%%d-%%b-%%y')},
    skiprows=1, usecols=(0,2), unpack=True)

fig = figure()
ax = fig.add_subplot(111)
ax.plot_date(dates, closes, '-')
fig.autofmt_xdate()
show()

Author: Fantashit

1 thought on “Load_converter: TypeError: strptime() argument 0 must be str, not

  1. The problem is that numpy passes a byte string to the converter instead of a string. In python 2, since bytes and str are equivalent it does not matter. For python 3, however, this results in an error as strpdate2num passes it to the standard library’s strptime which only expects a string.

    We could add another converter for byte input to the dates module. Not sure if it really would get enough use to make sense in adding, however.

    @keigolee514, you could use the following to get it working for you:

    from __future__ import print_function
    from matplotlib.dates import strpdate2num
    #from matplotlib.mlab import load
    import numpy as np
    from pylab import figure, show
    import matplotlib.cbook as cbook
    
    def bytespdate2num(fmt, encoding='utf-8'):
        strconverter = strpdate2num(fmt)
        def bytesconverter(b):
            s = b.decode(encoding)
            return strconverter(s)
        return bytesconverter
    
    
    datafile = cbook.get_sample_data('msft.csv', asfileobj=False)
    print('loading', datafile)
    
    dates, closes = np.loadtxt(
        datafile, delimiter=',',
        converters={0:bytespdate2num('%%d-%%b-%%y')},
        skiprows=1, usecols=(0,2), unpack=True)
    
    fig = figure()
    ax = fig.add_subplot(111)
    ax.plot_date(dates, closes, '-')
    fig.autofmt_xdate()
    show()

Comments are closed.