本文整理汇总了Python中pandas.compat.BytesIO类的典型用法代码示例。如果您正苦于以下问题:Python BytesIO类的具体用法?Python BytesIO怎么用?Python BytesIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BytesIO类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Excel
class Excel(object):
goal_time = 0.2
params = ['openpyxl', 'xlsxwriter', 'xlwt']
param_names = ['engine']
def setup(self, engine):
N = 2000
C = 5
self.df = DataFrame(np.random.randn(N, C),
columns=['float{}'.format(i) for i in range(C)],
index=date_range('20000101', periods=N, freq='H'))
self.df['object'] = tm.makeStringIndex(N)
self.bio_read = BytesIO()
self.writer_read = ExcelWriter(self.bio_read, engine=engine)
self.df.to_excel(self.writer_read, sheet_name='Sheet1')
self.writer_read.save()
self.bio_read.seek(0)
self.bio_write = BytesIO()
self.bio_write.seek(0)
self.writer_write = ExcelWriter(self.bio_write, engine=engine)
def time_read_excel(self, engine):
read_excel(self.bio_read)
def time_write_excel(self, engine):
self.df.to_excel(self.writer_write, sheet_name='Sheet1')
self.writer_write.save()
示例2: packers_write_excel_xlwt
class packers_write_excel_xlwt(object):
goal_time = 0.2
def setup(self):
self.f = '__test__.msg'
self.N = 100000
self.C = 5
self.index = date_range('20000101', periods=self.N, freq='H')
self.df = DataFrame(dict([('float{0}'.format(i), randn(self.N)) for i in range(self.C)]), index=self.index)
self.N = 100000
self.C = 5
self.index = date_range('20000101', periods=self.N, freq='H')
self.df2 = DataFrame(dict([('float{0}'.format(i), randn(self.N)) for i in range(self.C)]), index=self.index)
self.df2['object'] = [('%08x' % randrange((16 ** 8))) for _ in range(self.N)]
self.remove(self.f)
self.bio = BytesIO()
def time_packers_write_excel_xlwt(self):
self.bio.seek(0)
self.writer = pd.io.excel.ExcelWriter(self.bio, engine='xlwt')
self.df[:2000].to_excel(self.writer)
self.writer.save()
def remove(self, f):
try:
os.remove(self.f)
except:
pass
示例3: _pickle_array
def _pickle_array(arr):
arr = arr.view(np.ndarray)
buf = BytesIO()
write_array(buf, arr)
return buf.getvalue()
示例4: packers_read_excel
class packers_read_excel(_Packers):
def setup(self):
self._setup()
self.bio = BytesIO()
self.writer = pd.io.excel.ExcelWriter(self.bio, engine='xlsxwriter')
self.df[:2000].to_excel(self.writer)
self.writer.save()
def time_packers_read_excel(self):
self.bio.seek(0)
pd.read_excel(self.bio)
示例5: test_stringio_writer
def test_stringio_writer(self):
_skip_if_no_xlsxwriter()
_skip_if_no_xlrd()
path = BytesIO()
with ExcelWriter(path, engine='xlsxwriter', **{'options': {'in-memory': True}}) as ew:
self.frame.to_excel(ew, 'test1', engine='xlsxwriter')
ew.save()
path.seek(0)
ef = ExcelFile(path)
found_df = ef.parse('test1')
tm.assert_frame_equal(self.frame, found_df)
path.close()
示例6: setup
def setup(self, engine):
N = 2000
C = 5
self.df = DataFrame(np.random.randn(N, C),
columns=['float{}'.format(i) for i in range(C)],
index=date_range('20000101', periods=N, freq='H'))
self.df['object'] = tm.makeStringIndex(N)
self.bio_read = BytesIO()
self.writer_read = ExcelWriter(self.bio_read, engine=engine)
self.df.to_excel(self.writer_read, sheet_name='Sheet1')
self.writer_read.save()
self.bio_read.seek(0)
self.bio_write = BytesIO()
self.bio_write.seek(0)
self.writer_write = ExcelWriter(self.bio_write, engine=engine)
示例7: maybe_read_encoded_stream
def maybe_read_encoded_stream(reader, encoding=None, compression=None):
"""read an encoded stream from the reader and transform the bytes to
unicode if required based on the encoding
Parameters
----------
reader : a streamable file-like object
encoding : optional, the encoding to attempt to read
Returns
-------
a tuple of (a stream of decoded bytes, the encoding which was used)
"""
if compat.PY3 or encoding is not None: # pragma: no cover
if encoding:
errors = 'strict'
else:
errors = 'replace'
encoding = 'utf-8'
if compression == 'gzip':
reader = BytesIO(reader.read())
else:
reader = StringIO(reader.read().decode(encoding, errors))
else:
if compression == 'gzip':
reader = BytesIO(reader.read())
encoding = None
return reader, encoding
示例8: setup
def setup(self):
self.f = '__test__.msg'
self.N = 100000
self.C = 5
self.index = date_range('20000101', periods=self.N, freq='H')
self.df = DataFrame(dict([('float{0}'.format(i), randn(self.N)) for i in range(self.C)]), index=self.index)
self.N = 100000
self.C = 5
self.index = date_range('20000101', periods=self.N, freq='H')
self.df2 = DataFrame(dict([('float{0}'.format(i), randn(self.N)) for i in range(self.C)]), index=self.index)
self.df2['object'] = [('%08x' % randrange((16 ** 8))) for _ in range(self.N)]
self.remove(self.f)
self.bio = BytesIO()
示例9: test_utf16_bom_skiprows
def test_utf16_bom_skiprows(self):
# #2298
data = u(
"""skip this
skip this too
A\tB\tC
1\t2\t3
4\t5\t6"""
)
data2 = u(
"""skip this
skip this too
A,B,C
1,2,3
4,5,6"""
)
path = "__%s__.csv" % tm.rands(10)
with tm.ensure_clean(path) as path:
for sep, dat in [("\t", data), (",", data2)]:
for enc in ["utf-16", "utf-16le", "utf-16be"]:
bytes = dat.encode(enc)
with open(path, "wb") as f:
f.write(bytes)
s = BytesIO(dat.encode("utf-8"))
if compat.PY3:
# somewhat False since the code never sees bytes
from io import TextIOWrapper
s = TextIOWrapper(s, encoding="utf-8")
result = self.read_csv(path, encoding=enc, skiprows=2, sep=sep)
expected = self.read_csv(s, encoding="utf-8", skiprows=2, sep=sep)
s.close()
tm.assert_frame_equal(result, expected)
示例10: Excel
class Excel(_Packers):
def setup(self):
self._setup()
self.bio = BytesIO()
def time_write_excel_openpyxl(self):
self.bio.seek(0)
self.writer = pd.io.excel.ExcelWriter(self.bio, engine='openpyxl')
self.df[:2000].to_excel(self.writer)
self.writer.save()
def time_write_excel_xlsxwriter(self):
self.bio.seek(0)
self.writer = pd.io.excel.ExcelWriter(self.bio, engine='xlsxwriter')
self.df[:2000].to_excel(self.writer)
self.writer.save()
def time_write_excel_xlwt(self):
self.bio.seek(0)
self.writer = pd.io.excel.ExcelWriter(self.bio, engine='xlwt')
self.df[:2000].to_excel(self.writer)
self.writer.save()
示例11: compserver
def compserver(payload, serial):
(allow_profiler,
default_profiler_output,
profile_by_default) = _get_profiler_info()
requested_profiler_output = payload.get('profiler_output',
default_profiler_output)
profile = payload.get('profile')
profiling = (allow_profiler and
(profile or (profile_by_default and requested_profiler_output)))
if profile and not allow_profiler:
return ('profiling is disabled on this server', RC.FORBIDDEN)
with ExitStack() as response_construction_context_stack:
if profiling:
from cProfile import Profile
if (default_profiler_output == ':response' and
requested_profiler_output != ':response'):
# writing to the local filesystem is disabled
return ("local filepaths are disabled on this server, only"
" ':response' is allowed for the 'profiler_output' field",
RC.FORBIDDEN)
profiler_output = requested_profiler_output
profiler = Profile()
profiler.enable()
# ensure that we stop profiling in the case of an exception
response_construction_context_stack.callback(profiler.disable)
expr = '<failed to parse expr>'
@response_construction_context_stack.callback
def log_time(start=time()):
flask.current_app.logger.info('compute expr: %s\ntotal time (s): %.3f',
expr,
time() - start)
ns = payload.get('namespace', {})
compute_kwargs = payload.get('compute_kwargs') or {}
odo_kwargs = payload.get('odo_kwargs') or {}
dataset = _get_data()
ns[':leaf'] = symbol('leaf', discover(dataset))
expr = from_tree(payload['expr'], namespace=ns)
assert len(expr._leaves()) == 1
leaf = expr._leaves()[0]
try:
result = serial.materialize(compute(expr,
{leaf: dataset},
**compute_kwargs),
expr.dshape,
odo_kwargs)
except NotImplementedError as e:
return ("Computation not supported:\n%s" % e, RC.NOT_IMPLEMENTED)
except Exception as e:
return ("Computation failed with message:\n%s: %s" % (type(e).__name__, e),
RC.INTERNAL_SERVER_ERROR)
response = {'datashape': pprint(expr.dshape, width=0),
'data': serial.data_dumps(result),
'names': expr.fields}
if profiling:
import marshal
from pstats import Stats
if profiler_output == ':response':
from pandas.compat import BytesIO
file = BytesIO()
else:
file = open(_prof_path(profiler_output, expr), 'wb')
with file:
# Use marshal to dump the stats data to the given file.
# This is taken from cProfile which unfortunately does not have
# an api that allows us to pass the file object directly, only
# a file path.
marshal.dump(Stats(profiler).stats, file)
if profiler_output == ':response':
response['profiler_output'] = {'__!bytes': file.getvalue()}
return serial.dumps(response)
示例12: compserver
def compserver(payload, serial):
expected_keys = {u'namespace',
u'odo_kwargs',
u'compute_kwargs',
u'expr',
u'profile',
u'profiler_output'}
if not set(payload.keys()) < expected_keys:
return ('unexpected keys in payload: %r' % sorted(set(payload.keys()) -
expected_keys),
RC.BAD_REQUEST)
app = flask.current_app
(allow_profiler,
default_profiler_output,
profile_by_default) = _get_profiler_info()
requested_profiler_output = payload.get(u'profiler_output',
default_profiler_output)
profile = payload.get(u'profile')
profiling = (allow_profiler and
(profile or (profile_by_default and requested_profiler_output)))
if profile and not allow_profiler:
return ('profiling is disabled on this server', RC.FORBIDDEN)
with ExitStack() as response_construction_context_stack:
if profiling:
from cProfile import Profile
if (default_profiler_output == ':response' and
requested_profiler_output != ':response'):
# writing to the local filesystem is disabled
return ("local filepaths are disabled on this server, only"
" ':response' is allowed for the 'profiler_output' field",
RC.FORBIDDEN)
profiler_output = requested_profiler_output
profiler = Profile()
profiler.enable()
# ensure that we stop profiling in the case of an exception
response_construction_context_stack.callback(profiler.disable)
expr = '<failed to parse expr>'
@response_construction_context_stack.callback
def log_time(start=time()):
app.logger.info('compute expr: %s\ntotal time (s): %.3f',
expr,
time() - start)
ns = payload.get(u'namespace', {})
compute_kwargs = payload.get(u'compute_kwargs') or {}
odo_kwargs = payload.get(u'odo_kwargs') or {}
dataset = _get_data()
ns[':leaf'] = symbol('leaf', discover(dataset))
expr = from_tree(payload[u'expr'], namespace=ns)
if len(expr._leaves()) != 1:
return ('too many leaves, expected 1 got %d' % len(expr._leaves()),
RC.BAD_REQUEST)
leaf = expr._leaves()[0]
formatter = getattr(flask.current_app, 'log_exception_formatter',
_default_log_exception_formatter)
try:
result = serial.materialize(compute(expr,
{leaf: dataset},
**compute_kwargs),
expr.dshape,
odo_kwargs)
except NotImplementedError as e:
# Note: `sys.exc_info()[2]` holds the current traceback, for
# Python 2 / 3 compatibility. It's important not to store a local
# reference to it.
formatted_tb = formatter(sys.exc_info()[2])
error_msg = "Computation not supported:\n%s\n%s" % (e, formatted_tb)
app.logger.error(error_msg)
return (error_msg, RC.NOT_IMPLEMENTED)
except Exception as e:
formatted_tb = formatter(sys.exc_info()[2])
error_msg = "Computation failed with message:\n%s: %s\n%s" % (type(e).__name__, e, formatted_tb)
app.logger.error(error_msg)
return (error_msg, RC.INTERNAL_SERVER_ERROR)
response = {u'datashape': pprint(expr.dshape, width=0),
u'data': serial.data_dumps(result),
u'names': expr.fields}
if profiling:
import marshal
from pstats import Stats
if profiler_output == ':response':
from pandas.compat import BytesIO
file = BytesIO()
else:
file = open(_prof_path(profiler_output, expr), 'wb')
with file:
#.........这里部分代码省略.........
示例13: setup
def setup(self):
self._setup()
self.bio = BytesIO()
self.writer = pd.io.excel.ExcelWriter(self.bio, engine='xlsxwriter')
self.df[:2000].to_excel(self.writer)
self.writer.save()
示例14: time_write_excel
def time_write_excel(self, engine):
bio_write = BytesIO()
bio_write.seek(0)
writer_write = ExcelWriter(bio_write, engine=engine)
self.df.to_excel(writer_write, sheet_name='Sheet1')
writer_write.save()