maxframe.dataframe.read_odps_table#

maxframe.dataframe.read_odps_table(table_name: str | Table, partitions: None | str | List[str] = None, columns: List[str] | None = None, index_col: None | str | List[str] = None, *, odps_entry: ODPS = None, string_as_binary: bool = None, append_partitions: bool = False, dtype_backend: str = None, default_index_type: DefaultIndexType = None, filters: str | List[List[Tuple]] = None, **kw)[源代码]#

从 MaxCompute (ODPS) 表中读取数据到 DataFrame。

支持将某些列指定为索引。如果未指定,将生成 RangeIndex。

参数:
  • table_name (Union[str, Table]) -- 要读取的表名。

  • partitions (Union[None, str, List[str]]) -- 要读取的表分区或分区列表。

  • columns (Optional[List[str]]) -- 要读取的表列。您也可以在此处指定分区列。如果未指定,将包含所有表列(或者如果 append_partitions 为 True,则包含分区列)。

  • index_col (Union[None, str, List[str]]) -- 指定为索引的列。

  • append_partitions (bool) -- 如果为 True,当未指定 columns 时,将添加所有分区列作为选定列,

  • dtype_backend ({'numpy', 'pyarrow'}, default 'numpy') -- 应用于结果 DataFrame 的后端数据类型(仍处于实验阶段)。

  • filters (Union[str, List[List[Tuple]]], default None) -- 读取数据时应用的过滤表达式。 - 字符串格式: 直接传递给 StorageAPI 的 SQL WHERE 子句。 - 列表格式: 嵌套的元组列表。 格式: 内部列表使用 AND 连接,外部列表使用 OR 连接。 示例: [[('col1', '==', 'value'), ('col2', '>', 10)]] 支持的操作符: ==, !=, <, >, <=, >=, in, not in。 .. note:: 给定实现,不能保证此参数的完整过滤功能。

返回:

result -- 从 MaxCompute (ODPS) 表中读取的 DataFrame

返回类型:

DataFrame

示例

在使用 read_odps_table 之前,您需要创建一个 ODPS 条目,其参数将在当前进程中全局存储。

>>> import maxframe.dataframe as md
>>> from odps import ODPS
>>>
>>> o = ODPS(...)  # Fill account information here

简单地按名称读取表。

>>> df = md.read_odps_table("simple_table")

按分区(或多个分区)读取表。

>>> # Read partitioned table
>>> df = md.read_odps_table("partitioned_table", partitions="pt=20230101")
>>> # Read with multiple partitions
>>> df = md.read_odps_table("partitioned_table", partitions=["pt=20230101", "pt=20230102"])

带列选择的读取。

>>> df = md.read_odps_table("table_name", columns=["col1", "col2", "col3"])

将列作为索引进行读取。

>>> # Read with index columns
>>> df = md.read_odps_table("table_name", index_col="id")
>>> # Read with multiple index columns
>>> df = md.read_odps_table("table_name", index_col=["id", "timestamp"])

带过滤条件的读取。注意不能保证完整的过滤功能。

>>> # Read table with string filter
>>> df = md.read_odps_table("source_table", filters="age > 18")
>>> # Read with list filter
>>> df = md.read_odps_table(
...     "table_name",
...     filters=[[('age', '>', 18), ('city', '==', 'Beijing')]]
... )