Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Load_converter: TypeError: strptime() argument 0 must be str, not <class 'bytes'> #4126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
keigolee514 opened this issue Feb 18, 2015 · 10 comments

Comments

@keigolee514
Copy link

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()
@WeatherGod
Copy link
Member

Which version of matplotlib are you using? Which version of python are you
using? On what OS?

On Wed, Feb 18, 2015 at 3:49 PM, keigolee514 [email protected]
wrote:

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()


Reply to this email directly or view it on GitHub
#4126.

@keigolee514
Copy link
Author

Matplotlib: 1.8.2
Python: 3.4
OS: Windows 8 64-bit

Thank you!

@WeatherGod
Copy link
Member

1.8.2? Are you a time traveler?

On Wed, Feb 18, 2015 at 4:05 PM, keigolee514 [email protected]
wrote:

Matplotlib: 1.8.2
Python: 3.4
OS: Windows 8 64-bit

Thank you!


Reply to this email directly or view it on GitHub
#4126 (comment)
.

@keigolee514
Copy link
Author

sorry, 1.4.2...

@cimarronm
Copy link
Contributor

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()

@tacaswell
Copy link
Member

Closed by #4153

Mausy5043 added a commit to Mausy5043/domog that referenced this issue Sep 3, 2016
Mausy5043 added a commit to Mausy5043/domog that referenced this issue Sep 11, 2016
* Update of README.md

* printing python3-compatibly

* colors adjusted

* 20160521T0928

* 20160521T1258

* prevent destructive lftp

* readibility

* python3 ready

* attempt at preventing lftp from breaking the website

* fail exit always

* Try out for python3.4 compatibility

* Revert "Try out for python3.4 compatibility"

This reverts commit 851acbd.

* `chmod 744`

* Migration to Python 3.4

* one `b` too many. PID file should not be opened in byte-mode.

* Changed `python3.4` to `python3` because on Ubuntu Xenial (16.10.1) the shortcut to `python3.4` nolonger exists :-(

* Make execution of gnuplot script version conditional

* no transparency

* Exception handling updated. Gotta catch em all

* set timeout on lftp command

* catch timeout and process the exception. log a critical error for now.

* pass

* complete list of required packages

* system info on status page

* import platform

* chmod

* add windroos

* chmod 644 graphs

* python3

* retrieve wind data

* webpage update ad windgraph

* graphing wind data

* fix ref: matplotlib/matplotlib#4126

* make graph29

* use correct unit

* plot direction on top of speed for better visibility

* de-erroring pylama
@haunledeshuchang
Copy link

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

date,open,close=np.loadtxt('000001.csv',delimiter=',',converters={0:mdates.strpdate2num('%m/%d/%Y')},skiprows=1,usecols=(0,1,4),unpack=True)

plt,plot(date,open)

for the same problem.
strptime() argument 0 must be str, not <class 'bytes'>

ask for help

@tacaswell
Copy link
Member

@haunledeshuchang This sort of question is best handled on the mailing list ([email protected] you will need to join the list to post un-moderated. We try to keep the issues for bug reports or feature requests.

@kiddten
Copy link

kiddten commented Feb 22, 2018

seems I have same issue as @haunledeshuchang
Python 3.6.4
matplotlib==2.1.2

@pks9862728888
Copy link

Bdw How does the function bytesconverter(b): gets called in,

def bytespdate2num(fmt, encoding='utf-8'):
strconverter = strpdate2num(fmt)
def bytesconverter(b):
s = b.decode(encoding)
return strconverter(s)
return bytesconverter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants