本文整理汇总了Python中pandas.compat.StringIO方法的典型用法代码示例。如果您正苦于以下问题:Python compat.StringIO方法的具体用法?Python compat.StringIO怎么用?Python compat.StringIO使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pandas.compat
的用法示例。
在下文中一共展示了compat.StringIO方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_loc_setitem_consistency_slice_column_len
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_loc_setitem_consistency_slice_column_len(self):
# .loc[:,column] setting with slice == len of the column
# GH10408
data = """Level_0,,,Respondent,Respondent,Respondent,OtherCat,OtherCat
Level_1,,,Something,StartDate,EndDate,Yes/No,SomethingElse
Region,Site,RespondentID,,,,,
Region_1,Site_1,3987227376,A,5/25/2015 10:59,5/25/2015 11:22,Yes,
Region_1,Site_1,3980680971,A,5/21/2015 9:40,5/21/2015 9:52,Yes,Yes
Region_1,Site_2,3977723249,A,5/20/2015 8:27,5/20/2015 8:41,Yes,
Region_1,Site_2,3977723089,A,5/20/2015 8:33,5/20/2015 9:09,Yes,No"""
df = pd.read_csv(StringIO(data), header=[0, 1], index_col=[0, 1, 2])
df.loc[:, ('Respondent', 'StartDate')] = pd.to_datetime(df.loc[:, (
'Respondent', 'StartDate')])
df.loc[:, ('Respondent', 'EndDate')] = pd.to_datetime(df.loc[:, (
'Respondent', 'EndDate')])
df.loc[:, ('Respondent', 'Duration')] = df.loc[:, (
'Respondent', 'EndDate')] - df.loc[:, ('Respondent', 'StartDate')]
df.loc[:, ('Respondent', 'Duration')] = df.loc[:, (
'Respondent', 'Duration')].astype('timedelta64[s]')
expected = Series([1380, 720, 840, 2160.], index=df.index,
name=('Respondent', 'Duration'))
tm.assert_series_equal(df[('Respondent', 'Duration')], expected)
示例2: test_transform_casting
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_transform_casting():
# 13046
data = """
idx A ID3 DATETIME
0 B-028 b76cd912ff "2014-10-08 13:43:27"
1 B-054 4a57ed0b02 "2014-10-08 14:26:19"
2 B-076 1a682034f8 "2014-10-08 14:29:01"
3 B-023 b76cd912ff "2014-10-08 18:39:34"
4 B-023 f88g8d7sds "2014-10-08 18:40:18"
5 B-033 b76cd912ff "2014-10-08 18:44:30"
6 B-032 b76cd912ff "2014-10-08 18:46:00"
7 B-037 b76cd912ff "2014-10-08 18:52:15"
8 B-046 db959faf02 "2014-10-08 18:59:59"
9 B-053 b76cd912ff "2014-10-08 19:17:48"
10 B-065 b76cd912ff "2014-10-08 19:21:38"
"""
df = pd.read_csv(StringIO(data), sep=r'\s+',
index_col=[0], parse_dates=['DATETIME'])
result = df.groupby('ID3')['DATETIME'].transform(lambda x: x.diff())
assert is_timedelta64_dtype(result.dtype)
result = df[['ID3', 'DATETIME']].groupby('ID3').transform(
lambda x: x.diff())
assert is_timedelta64_dtype(result.DATETIME.dtype)
示例3: test_repr
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_repr(self):
buf = StringIO()
# small one
foo = repr(self.frame)
self.frame.info(verbose=False, buf=buf)
# even smaller
self.frame.reindex(columns=['A']).info(verbose=False, buf=buf)
self.frame.reindex(columns=['A', 'B']).info(verbose=False, buf=buf)
# exhausting cases in DataFrame.info
# columns but no index
no_index = DataFrame(columns=[0, 1, 3])
foo = repr(no_index) # noqa
# no columns or index
self.empty.info(buf=buf)
df = DataFrame(["a\n\r\tb"], columns=["a\n\r\td"], index=["a\n\r\tf"])
assert "\t" not in repr(df)
assert "\r" not in repr(df)
assert "a\n" not in repr(df)
示例4: test_info_wide
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_info_wide(self):
from pandas import set_option, reset_option
io = StringIO()
df = DataFrame(np.random.randn(5, 101))
df.info(buf=io)
io = StringIO()
df.info(buf=io, max_cols=101)
rs = io.getvalue()
assert len(rs.splitlines()) > 100
xp = rs
set_option('display.max_info_columns', 101)
io = StringIO()
df.info(buf=io)
assert rs == xp
reset_option('display.max_info_columns')
示例5: test_query_with_nested_strings
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_query_with_nested_strings(self, parser, engine):
skip_if_no_pandas_parser(parser)
raw = """id event timestamp
1 "page 1 load" 1/1/2014 0:00:01
1 "page 1 exit" 1/1/2014 0:00:31
2 "page 2 load" 1/1/2014 0:01:01
2 "page 2 exit" 1/1/2014 0:01:31
3 "page 3 load" 1/1/2014 0:02:01
3 "page 3 exit" 1/1/2014 0:02:31
4 "page 1 load" 2/1/2014 1:00:01
4 "page 1 exit" 2/1/2014 1:00:31
5 "page 2 load" 2/1/2014 1:01:01
5 "page 2 exit" 2/1/2014 1:01:31
6 "page 3 load" 2/1/2014 1:02:01
6 "page 3 exit" 2/1/2014 1:02:31
"""
df = pd.read_csv(StringIO(raw), sep=r'\s{2,}', engine='python',
parse_dates=['timestamp'])
expected = df[df.event == '"page 1 load"']
res = df.query("""'"page 1 load"' in event""", parser=parser,
engine=engine)
assert_frame_equal(expected, res)
示例6: test_dtype_per_column
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_dtype_per_column(all_parsers):
parser = all_parsers
data = """\
one,two
1,2.5
2,3.5
3,4.5
4,5.5"""
expected = DataFrame([[1, "2.5"], [2, "3.5"], [3, "4.5"], [4, "5.5"]],
columns=["one", "two"])
expected["one"] = expected["one"].astype(np.float64)
expected["two"] = expected["two"].astype(object)
result = parser.read_csv(StringIO(data), dtype={"one": np.float64,
1: str})
tm.assert_frame_equal(result, expected)
示例7: test_categorical_dtype_chunksize_infer_categories
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_categorical_dtype_chunksize_infer_categories(all_parsers):
# see gh-10153
parser = all_parsers
data = """a,b
1,a
1,b
1,b
2,c"""
expecteds = [DataFrame({"a": [1, 1],
"b": Categorical(["a", "b"])}),
DataFrame({"a": [1, 2],
"b": Categorical(["b", "c"])},
index=[2, 3])]
actuals = parser.read_csv(StringIO(data), dtype={"b": "category"},
chunksize=2)
for actual, expected in zip(actuals, expecteds):
tm.assert_frame_equal(actual, expected)
示例8: test_categorical_dtype_chunksize_explicit_categories
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_categorical_dtype_chunksize_explicit_categories(all_parsers):
# see gh-10153
parser = all_parsers
data = """a,b
1,a
1,b
1,b
2,c"""
cats = ["a", "b", "c"]
expecteds = [DataFrame({"a": [1, 1],
"b": Categorical(["a", "b"],
categories=cats)}),
DataFrame({"a": [1, 2],
"b": Categorical(["b", "c"],
categories=cats)},
index=[2, 3])]
dtype = CategoricalDtype(cats)
actuals = parser.read_csv(StringIO(data), dtype={"b": dtype}, chunksize=2)
for actual, expected in zip(actuals, expecteds):
tm.assert_frame_equal(actual, expected)
示例9: test_categorical_category_dtype
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_categorical_category_dtype(all_parsers, categories, ordered):
parser = all_parsers
data = """a,b
1,a
1,b
1,b
2,c"""
expected = DataFrame({
"a": [1, 1, 1, 2],
"b": Categorical(["a", "b", "b", "c"],
categories=categories,
ordered=ordered)
})
dtype = {"b": CategoricalDtype(categories=categories,
ordered=ordered)}
result = parser.read_csv(StringIO(data), dtype=dtype)
tm.assert_frame_equal(result, expected)
示例10: test_usecols_with_parse_dates
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_usecols_with_parse_dates(all_parsers, usecols):
# see gh-9755
data = """a,b,c,d,e
0,1,20140101,0900,4
0,1,20140102,1000,4"""
parser = all_parsers
parse_dates = [[1, 2]]
cols = {
"a": [0, 0],
"c_d": [
Timestamp("2014-01-01 09:00:00"),
Timestamp("2014-01-02 10:00:00")
]
}
expected = DataFrame(cols, columns=["c_d", "a"])
result = parser.read_csv(StringIO(data), usecols=usecols,
parse_dates=parse_dates)
tm.assert_frame_equal(result, expected)
示例11: test_usecols_with_parse_dates2
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_usecols_with_parse_dates2(all_parsers):
# see gh-13604
parser = all_parsers
data = """2008-02-07 09:40,1032.43
2008-02-07 09:50,1042.54
2008-02-07 10:00,1051.65"""
names = ["date", "values"]
usecols = names[:]
parse_dates = [0]
index = Index([Timestamp("2008-02-07 09:40"),
Timestamp("2008-02-07 09:50"),
Timestamp("2008-02-07 10:00")],
name="date")
cols = {"values": [1032.43, 1042.54, 1051.65]}
expected = DataFrame(cols, index=index)
result = parser.read_csv(StringIO(data), parse_dates=parse_dates,
index_col=0, usecols=usecols,
header=None, names=names)
tm.assert_frame_equal(result, expected)
示例12: test_usecols_with_parse_dates3
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_usecols_with_parse_dates3(all_parsers):
# see gh-14792
parser = all_parsers
data = """a,b,c,d,e,f,g,h,i,j
2016/09/21,1,1,2,3,4,5,6,7,8"""
usecols = list("abcdefghij")
parse_dates = [0]
cols = {"a": Timestamp("2016-09-21"),
"b": [1], "c": [1], "d": [2],
"e": [3], "f": [4], "g": [5],
"h": [6], "i": [7], "j": [8]}
expected = DataFrame(cols, columns=usecols)
result = parser.read_csv(StringIO(data), usecols=usecols,
parse_dates=parse_dates)
tm.assert_frame_equal(result, expected)
示例13: test_usecols_with_unicode_strings
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_usecols_with_unicode_strings(all_parsers):
# see gh-13219
data = """AAA,BBB,CCC,DDD
0.056674973,8,True,a
2.613230982,2,False,b
3.568935038,7,False,a"""
parser = all_parsers
exp_data = {
"AAA": {
0: 0.056674972999999997,
1: 2.6132309819999997,
2: 3.5689350380000002
},
"BBB": {0: 8, 1: 2, 2: 7}
}
expected = DataFrame(exp_data)
result = parser.read_csv(StringIO(data), usecols=[u"AAA", u"BBB"])
tm.assert_frame_equal(result, expected)
示例14: test_usecols_with_single_byte_unicode_strings
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_usecols_with_single_byte_unicode_strings(all_parsers):
# see gh-13219
data = """A,B,C,D
0.056674973,8,True,a
2.613230982,2,False,b
3.568935038,7,False,a"""
parser = all_parsers
exp_data = {
"A": {
0: 0.056674972999999997,
1: 2.6132309819999997,
2: 3.5689350380000002
},
"B": {0: 8, 1: 2, 2: 7}
}
expected = DataFrame(exp_data)
result = parser.read_csv(StringIO(data), usecols=[u"A", u"B"])
tm.assert_frame_equal(result, expected)
示例15: test_usecols_with_multi_byte_characters
# 需要导入模块: from pandas import compat [as 别名]
# 或者: from pandas.compat import StringIO [as 别名]
def test_usecols_with_multi_byte_characters(all_parsers, usecols):
data = """あああ,いい,ううう,ええええ
0.056674973,8,True,a
2.613230982,2,False,b
3.568935038,7,False,a"""
parser = all_parsers
exp_data = {
"あああ": {
0: 0.056674972999999997,
1: 2.6132309819999997,
2: 3.5689350380000002
},
"いい": {0: 8, 1: 2, 2: 7}
}
expected = DataFrame(exp_data)
result = parser.read_csv(StringIO(data), usecols=usecols)
tm.assert_frame_equal(result, expected)