我们应该使用 pandas.compat.StringIO 还是 Python 2/3 StringIO? [英] Should we use pandas.compat.StringIO or 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.
StringIO.StringIO
(Python 2)/io.StringIO
(Python 3)- 优点:对于面向未来的代码更稳定,但迫使我们进行版本分叉,例如请参阅 EmilH 底部的代码.
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 是一个 2/3 兼容包 ("无需 2to3") 在 0.13.0(2014 年 1 月)中引入
- pandas.compat 包仍被标记为私有"0.22 并且没有计划公开" 说警告 pandas.core、pandas.compat 和 pandas.util 顶级模块被认为是私有的.这些模块中功能的稳定性不是保证." 虽然它们基本上从 0.13 开始就没有坏过
- pandas.compat 源 定义导入
builtins、StringIO/cStringIO、BytesIO、cPickle、httplib
、范围、过滤器、映射和 zip 的迭代器版本,以及 Python 3 兼容性的其他必要元素 - 请参阅 0.13.0 最新消息
- 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
自 pandas 0.13.0(2014 年 1 月)以来就作为 pandas 中的一个子包存在- 它还似乎已作为独立包发布:0.1.0(6 月 10 日,2017) 和 0.1.1(2017 年 6 月 10 日)
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 withfrom io import StringIO
for Python 3.
这篇关于我们应该使用 pandas.compat.StringIO 还是 Python 2/3 StringIO?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!