本文整理汇总了Python中odps.df.DataFrame.to_pandas方法的典型用法代码示例。如果您正苦于以下问题:Python DataFrame.to_pandas方法的具体用法?Python DataFrame.to_pandas怎么用?Python DataFrame.to_pandas使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类odps.df.DataFrame
的用法示例。
在下文中一共展示了DataFrame.to_pandas方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testToPandas
# 需要导入模块: from odps.df import DataFrame [as 别名]
# 或者: from odps.df.DataFrame import to_pandas [as 别名]
def testToPandas(self):
table_name = tn('pyodps_test_mixed_engine_to_pandas')
self.odps.delete_table(table_name, if_exists=True)
table2 = self.odps.create_table(name=table_name,
schema=Schema.from_lists(['col%s' % i for i in range(7)],
['bigint', 'double', 'string', 'datetime',
'boolean', 'decimal', 'datetime']))
expr2 = DataFrame(table2)
data2 = [
[1234567, 3.14, 'test', datetime(2016, 6, 1), True, Decimal('3.14'), None]
]
self.odps.write_table(table2, 0, data2)
pd_df = expr2.to_pandas()
self.assertSequenceEqual(data2[0], pd_df.ix[0].tolist())
wrapeed_pd_df = expr2.to_pandas(wrap=True)
self.assertSequenceEqual(data2[0], list(next(wrapeed_pd_df.execute())))
示例2: testPandasPersist
# 需要导入模块: from odps.df import DataFrame [as 别名]
# 或者: from odps.df.DataFrame import to_pandas [as 别名]
def testPandasPersist(self):
import pandas as pd, numpy as np
self.odps.to_global()
tmp_table_name = tn('pyodps_test_mixed_persist')
self.odps.delete_table(tmp_table_name, if_exists=True)
pd_df = pd.DataFrame(np.arange(9).reshape(3, 3), columns=list('abc'))
df = DataFrame(pd_df).persist(tmp_table_name)
self.assertPandasEqual(df.to_pandas(), pd_df)
self.odps.delete_table(tmp_table_name)
示例3: Test
# 需要导入模块: from odps.df import DataFrame [as 别名]
# 或者: from odps.df.DataFrame import to_pandas [as 别名]
class Test(TestBase):
def setup(self):
import pandas as pd
odps_data = [
['name1', 1],
['name2', 2],
['name1', 3],
]
pd_data = [
['name1', 5],
['name2', 6]
]
names = ['name', 'id']
types = ['string', 'bigint']
table = tn('pyodps_df_mixed')
self.odps.delete_table(table, if_exists=True)
self.t = self.odps.create_table(table, Schema.from_lists(names, types))
with self.t.open_writer() as w:
w.write([self.t.new_record(r) for r in odps_data])
self.odps_df = DataFrame(self.t)
self.pd_df = DataFrame(pd.DataFrame(pd_data, columns=names))
self.engine = MixedEngine(self.odps)
self.pd_engine = PandasEngine(self.odps)
def teardown(self):
self.t.drop()
def assertPandasEqual(self, df1, df2):
from odps.compat import six
from odps import types as o_types
from pandas.util.testing import assert_frame_equal
# compare column types
def get_odps_type(p_type):
for data_type, builtin_type in six.iteritems(o_types._odps_primitive_to_builtin_types):
if issubclass(p_type.type, builtin_type):
return data_type
types1 = [get_odps_type(dt) for dt in df1.dtypes]
types2 = [get_odps_type(dt) for dt in df2.dtypes]
self.assertSequenceEqual(types1, types2)
assert_frame_equal(df1, df2, check_dtype=False)
def testJoin(self):
expr = self.odps_df.join(self.pd_df, 'name').sort('id_x')
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
expected = self.pd_engine.execute(df.join(self.pd_df, 'name').sort('id_x')).values
self.assertTrue(result.equals(expected))
def testUnion(self):
expr = self.odps_df.union(self.pd_df).sort(['id', 'name'])
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
expected = self.pd_engine.execute(df.union(self.pd_df).sort(['id', 'name'])).values
self.assertTrue(result.equals(expected))
def testIsIn(self):
expr = self.odps_df['name'].isin(self.pd_df['name']).rename('isin')
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
expected = self.pd_engine.execute(df['name'].isin(self.pd_df['name']).rename('isin')).values
self.assertTrue(result.equals(expected))
def testMixed(self):
expr = self.odps_df.union(
self.odps_df.join(self.pd_df, 'name')[
lambda x: x.name,
lambda x: x.id_x.rename('id')
]).sort(['name', 'id'])
expr = expr[expr['name'].isin(self.pd_df['name'])]
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
test_expr = df.union(
df.join(self.pd_df, 'name')[
lambda x: x.name,
lambda x: x.id_x.rename('id')
]).sort(['name', 'id'])
test_expr = test_expr[test_expr['name'].isin(self.pd_df['name'])]
expected = self.pd_engine.execute(test_expr).values
self.assertTrue(result.equals(expected))
def testPandasPersist(self):
import pandas as pd, numpy as np
self.odps.to_global()
tmp_table_name = tn('pyodps_test_mixed_persist')
self.odps.delete_table(tmp_table_name, if_exists=True)
#.........这里部分代码省略.........
示例4: Test
# 需要导入模块: from odps.df import DataFrame [as 别名]
# 或者: from odps.df.DataFrame import to_pandas [as 别名]
class Test(TestBase):
def setup(self):
import pandas as pd
odps_data = [["name1", 1], ["name2", 2], ["name1", 3]]
pd_data = [["name1", 5], ["name2", 6]]
names = ["name", "id"]
types = ["string", "bigint"]
table = tn("pyodps_df_mixed")
self.odps.delete_table(table, if_exists=True)
self.t = self.odps.create_table(table, Schema.from_lists(names, types))
with self.t.open_writer() as w:
w.write([self.t.new_record(r) for r in odps_data])
self.odps_df = DataFrame(self.t)
self.pd_df = DataFrame(pd.DataFrame(pd_data, columns=names))
self.engine = MixedEngine(self.odps)
self.pd_engine = PandasEngine(self.odps)
def teardown(self):
self.t.drop()
def testGroupReduction(self):
expr = self.odps_df.select(self.odps_df, id2=self.odps_df.id.map(lambda x: x + 1))
expr = expr.groupby("name").id2.sum()
expected = [["name1", 6], ["name2", 3]]
res = self.engine.execute(expr)
result = self._get_result(res)
self.assertEqual(sorted([[r[1]] for r in expected]), sorted(result))
def assertPandasEqual(self, df1, df2):
from odps.compat import six
from odps import types as o_types
from pandas.util.testing import assert_frame_equal
# compare column types
def get_odps_type(p_type):
for data_type, builtin_type in six.iteritems(o_types._odps_primitive_to_builtin_types):
if issubclass(p_type.type, builtin_type):
return data_type
types1 = [get_odps_type(dt) for dt in df1.dtypes]
types2 = [get_odps_type(dt) for dt in df2.dtypes]
self.assertSequenceEqual(types1, types2)
assert_frame_equal(df1, df2, check_dtype=False)
def testJoin(self):
expr = self.odps_df.join(self.pd_df, "name").sort("id_x")
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
expected = self.pd_engine.execute(df.join(self.pd_df, "name").sort("id_x")).values
self.assertTrue(result.equals(expected))
def testUnion(self):
expr = self.odps_df.union(self.pd_df).sort(["id", "name"])
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
expected = self.pd_engine.execute(df.union(self.pd_df).sort(["id", "name"])).values
self.assertTrue(result.equals(expected))
def testIsIn(self):
expr = self.odps_df["name"].isin(self.pd_df["name"]).rename("isin")
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
expected = self.pd_engine.execute(df["name"].isin(self.pd_df["name"]).rename("isin")).values
self.assertTrue(result.equals(expected))
def testMixed(self):
expr = self.odps_df.union(
self.odps_df.join(self.pd_df, "name")[lambda x: x.name, lambda x: x.id_x.rename("id")]
).sort(["name", "id"])
expr = expr[expr["name"].isin(self.pd_df["name"])]
result = self.engine.execute(expr).values
df = DataFrame(self.odps_df.to_pandas())
test_expr = df.union(df.join(self.pd_df, "name")[lambda x: x.name, lambda x: x.id_x.rename("id")]).sort(
["name", "id"]
)
test_expr = test_expr[test_expr["name"].isin(self.pd_df["name"])]
expected = self.pd_engine.execute(test_expr).values
self.assertTrue(result.equals(expected))
def testPandasPersist(self):
import pandas as pd, numpy as np
self.odps.to_global()
tmp_table_name = tn("pyodps_test_mixed_persist")
self.odps.delete_table(tmp_table_name, if_exists=True)
pd_df = pd.DataFrame(np.arange(9).reshape(3, 3), columns=list("abc"))
#.........这里部分代码省略.........