我们应该使用 pandas.compat.StringIO 还是 Python 2/3 StringIO? [英] Should we use pandas.compat.StringIO or Python 2/3 StringIO?

查看:30
本文介绍了我们应该使用 pandas.compat.StringIO 还是 Python 2/3 StringIO?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StringIO 是我们在从文本中读取 Pandas 数据帧时使用的类似文件的字符串缓冲区对象,例如如何从字符串创建 Pandas DataFrame?"

StringIO is the file-like string buffer object we use when reading pandas dataframe from text, e.g. "How to create a Pandas DataFrame from a string?"

我们应该将这两个导入中的哪一个用于 StringIO(在 Pandas 中)?这是一个长期存在的问题,四年来从未得到解决.

Which of these two imports should we use for StringIO (within pandas)? This is a long-running question that has never been resolved over four years.

  1. StringIO.StringIO (Python 2)/io.StringIO (Python 3)
    • 优点:对于面向未来的代码更稳定,但迫使我们进行版本分叉,例如请参阅 EmilH 底部的代码.
  1. StringIO.StringIO (Python 2) / io.StringIO (Python 3)
    • Advantages: more stable for futureproofing code, but forces us to version-fork, e.g. see code at bottom from EmilH.
  • pandas.compat is a 2/3 compatibility package ("without the need for 2to3") introduced back in 0.13.0 (Jan 2014)
  • pandas.compat package is still marked 'private' as of 0.22 and no plans to make 'public' says "Warning The pandas.core, pandas.compat, and pandas.util top-level modules are considered to be PRIVATE. Stability of functionality in those modules in not guaranteed." although they essentially haven't broken since 0.13
  • pandas.compat source defines the imports builtins, StringIO/cStringIO, BytesIO, cPickle, httplib, iterator versions of range, filter, map and zip, plus other necessary elements for Python 3 compatibility - see the 0.13.0 whatsnew

用于从标准(来自 EmilH)导入的版本 2/3 分叉代码:

Version 2/3 forking code for imports from standard (from EmilH):

import sys
if sys.version_info[0] < 3: 
    from StringIO import StringIO
else:
    from io import StringIO

# Note: but this is very much a poor-man's version of pandas.compat, which contains much much more

注意:

  • pandas.compat has existed since pandas 0.13.0 (Jan 2014) as a subpackage within pandas
  • it also seems to have been released as a standalone package: 0.1.0 (Jun 10, 2017) and 0.1.1 (Jun 10, 2017)

推荐答案

我知道这是一个老问题,但我在这里关注了面包屑,所以也许仍然值得回答.这不是完全确定的,但当前的 Pandas 文档建议使用内置的 StringIO 而不是它自己的内部方法.

I know this is an old question, but I followed breadcrumbs here, so perhaps still worth answering. It's not totally definitive, but current Pandas documentation suggests using the built in StringIO rather than it's own internal methods.

对于使用 StringIO 类的示例,请确保使用 from io import StringIO for Python 3 导入它.

For examples that use the StringIO class, make sure you import it with from io import StringIO for Python 3.

这篇关于我们应该使用 pandas.compat.StringIO 还是 Python 2/3 StringIO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆