Datasets 文档
Table Classes
并获得增强的文档体验
开始使用
表类
每个 Dataset 对象都由一个 PyArrow 表支持。表可以从磁盘(内存映射)或内存中加载。有几种表类型可用,它们都继承自 table.Table。
Table
通过组合包装 pyarrow Table。这是 InMemoryTable、MemoryMappedTable 和 ConcatenationTable 的基类。
它实现了 pyarrow Table 类的所有基本属性/方法,除了 Table 转换方法:slice, filter, flatten, combine_chunks, cast, add_column, append_column, remove_column, set_column, rename_columns 和 drop。
这些方法的实现因子类而异。
validate
< source >( *args **kwargs )
执行验证检查。如果验证失败,则抛出异常。
默认情况下,只运行廉价的验证检查。传递 full=True 以进行彻底的验证检查(可能是 O(n))。
equals
< source >( *args **kwargs ) → bool
检查两个表的内容是否相等。
to_batches
< source >( *args **kwargs )
将 Table 转换为(连续的)RecordBatch 对象列表。
将 Table 转换为 dict 或 OrderedDict。
to_pandas
< source >( *args **kwargs ) → pandas.Series or pandas.DataFrame
参数
- memory_pool (
MemoryPool, defaults toNone) — 用于分配的 Arrow MemoryPool。如果未传递,则使用默认的内存池。 - strings_to_categorical (
bool, defaults toFalse) — 将字符串 (UTF8) 和二进制类型编码为pandas.Categorical。 - categories (
list, defaults toempty) — 应作为pandas.Categorical返回的字段列表。仅适用于类似表的数据结构。 - zero_copy_only (
bool, defaults toFalse) — 如果此函数调用需要复制底层数据,则引发ArrowException。 - integer_object_nulls (
bool, defaults toFalse) — 将带有 nulls 的整数转换为对象。 - date_as_object (
bool, defaults toTrue) — 将日期转换为对象。如果为False,则转换为datetime64[ns]dtype。 - timestamp_as_object (
bool, defaults toFalse) — 将非纳秒时间戳 (np.datetime64) 转换为对象。如果您有不适合纳秒时间戳正常日期范围(1678 CE-2262 CE)的时间戳,这很有用。如果为False,所有时间戳都将转换为datetime64[ns]dtype。 - use_threads (
bool, defaults toTrue) — 是否使用多线程并行转换。 - deduplicate_objects (
bool, defaults toFalse) — 创建 Python 对象时不要创建多个副本,以节省内存使用。转换速度会变慢。 - ignore_metadata (
bool, defaults toFalse) — 如果为True,则不使用“pandas”元数据来重建 DataFrame 索引(如果存在)。 - safe (
bool, defaults toTrue) — 对于某些数据类型,需要进行转换才能将数据存储在 pandas DataFrame 或 Series 中(例如,时间戳在 pandas 中始终存储为纳秒)。此选项控制是否为安全转换。 - split_blocks (
bool, defaults toFalse) — 如果为True,则在从RecordBatch或Table创建 pandas.DataFrame 时,为每列生成一个内部“块”。虽然这可以暂时减少内存,但请注意各种 pandas 操作可能会触发“合并”,从而导致内存使用量激增。 - self_destruct (
bool, defaults toFalse) — 实验性:如果为True,则在将 Arrow 对象转换为 pandas 时尝试释放原始 Arrow 内存。如果在调用to_pandas时使用此选项后使用该对象,程序将崩溃。 - types_mapper (
function, defaults toNone) — 将 pyarrow DataType 映射到 pandasExtensionDtype的函数。这可用于覆盖内置 pyarrow 类型转换的默认 pandas 类型,或者在 Table 模式中没有pandas_metadata的情况下使用。函数接收一个 pyarrow DataType,并应返回一个 pandasExtensionDtype或None(如果应使用该类型的默认转换)。如果有一个字典映射,您可以将dict.get作为函数传递。
返回
pandas.Series or pandas.DataFrame
pandas.Series 或 pandas.DataFrame,取决于对象类型
转换为 pandas 兼容的 NumPy 数组或 DataFrame,视情况而定。
按列名或数字索引选择模式字段。
按列名或数字索引选择列。
按数字顺序迭代所有列。
表及其列的模式。
所有列按数字顺序排列的列表。
此表中的列数。
表的维度:(#行, #列)。
表元素消耗的总字节数。
InMemoryTable
当表加载到用户的 RAM 中时,它被称为内存中表(in-memory table)。
对其进行 pickle 操作会复制所有数据到内存中。它的实现很简单,直接使用底层 pyarrow Table 方法。
这与 MemoryMapped 表不同,后者在进行 pickle 操作时不会复制所有数据到内存中。对于 MemoryMapped 表,反序列化时会从磁盘重新加载表。
当数据适合内存时,必须使用 InMemoryTable,而 MemoryMapped 则保留用于大于内存的数据或当您希望应用程序的内存占用保持较低时。
validate
< source >( *args **kwargs )
执行验证检查。如果验证失败,则抛出异常。
默认情况下,只运行廉价的验证检查。传递 full=True 以进行彻底的验证检查(可能是 O(n))。
equals
< source >( *args **kwargs ) → bool
检查两个表的内容是否相等。
to_batches
< source >( *args **kwargs )
将 Table 转换为(连续的)RecordBatch 对象列表。
将 Table 转换为 dict 或 OrderedDict。
to_pandas
< source >( *args **kwargs ) → pandas.Series or pandas.DataFrame
参数
- memory_pool (
MemoryPool, defaults toNone) — 用于分配的 Arrow MemoryPool。如果未传递,则使用默认的内存池。 - strings_to_categorical (
bool, defaults toFalse) — 将字符串 (UTF8) 和二进制类型编码为pandas.Categorical。 - categories (
list, defaults toempty) — 应作为pandas.Categorical返回的字段列表。仅适用于类似表的数据结构。 - zero_copy_only (
bool, defaults toFalse) — 如果此函数调用需要复制底层数据,则引发ArrowException。 - integer_object_nulls (
bool, defaults toFalse) — 将带空值的整数转换为对象。 - date_as_object (
bool, defaults toTrue) — 将日期转换为对象。如果为False,则转换为datetime64[ns]dtype。 - timestamp_as_object (
bool, defaults toFalse) — 将非纳秒时间戳 (np.datetime64) 转换为对象。如果您有不适合纳秒时间戳正常日期范围(公元 1678 年至公元 2262 年)的时间戳,这会很有用。如果为False,所有时间戳都将转换为datetime64[ns]dtype。 - use_threads (
bool, defaults toTrue) — 是否使用多线程并行转换。 - deduplicate_objects (
bool, defaults toFalse) — 在创建 Python 对象时不创建多个副本以节省内存使用。转换速度会变慢。 - ignore_metadata (
bool, defaults toFalse) — 如果为True,则不使用“pandas”元数据(如果存在)来重构 DataFrame 索引。 - safe (
bool, defaults toTrue) — 对于某些数据类型,需要进行转换才能将数据存储在 pandas DataFrame 或 Series 中(例如,时间戳在 pandas 中始终存储为纳秒)。此选项控制是否进行安全转换。 - split_blocks (
bool, defaults toFalse) — 如果为True,则在从RecordBatch或Table创建 pandas.DataFrame 时,为每个列生成一个内部“块”。虽然这可以暂时减少内存,但请注意,各种 pandas 操作可能会触发“合并”,从而导致内存使用量激增。 - self_destruct (
bool, defaults toFalse) — 实验性:如果为True,则在将 Arrow 对象转换为 pandas 时尝试释放原始 Arrow 内存。如果在调用to_pandas时使用此选项后使用该对象,程序将崩溃。 - types_mapper (
function, defaults toNone) — 将 pyarrow DataType 映射到 pandasExtensionDtype的函数。这可用于覆盖内置 pyarrow 类型转换的默认 pandas 类型,或者在表模式中缺少pandas_metadata时使用。该函数接收一个 pyarrow DataType,并应返回一个 pandasExtensionDtype或None(如果应使用该类型的默认转换)。如果有一个字典映射,可以传递dict.get作为函数。
返回
pandas.Series or pandas.DataFrame
pandas.Series 或 pandas.DataFrame,取决于对象类型
转换为 pandas 兼容的 NumPy 数组或 DataFrame,视情况而定。
按列名或数字索引选择模式字段。
按列名或数字索引选择列。
按数字顺序迭代所有列。
表及其列的模式。
所有列按数字顺序排列的列表。
此表中的列数。
表的维度:(#行, #列)。
表元素消耗的总字节数。
表的列名。
slice
< source >( offset = 0 length = None )
计算此表的零拷贝切片。
从表中选择记录。有关完整用法,请参阅 pyarrow.compute.filter。
flatten
< source >( *args **kwargs )
展平此表。每个具有 struct 类型的列都被展平为每个 struct 字段一个列。其他列保持不变。
cast
< source >( *args **kwargs )
将表值转换为另一种模式。
replace_schema_metadata
< source >( *args **kwargs ) → datasets.table.Table
实验性:通过用指定的新元数据(可以是 None,这将删除任何现有元数据)替换架构键值元数据来创建表的浅拷贝。
append_column
< source >( *args **kwargs ) → datasets.table.Table
在列尾添加列。
remove_column
< source >( *args **kwargs ) → datasets.table.Table
创建一个删除指定列的新表。
set_column
< source >( *args **kwargs ) → datasets.table.Table
替换表中指定位置的列。
创建一个将列重命名为指定名称的新表。
drop
< source >( *args **kwargs ) → datasets.table.Table
删除一个或多个列并返回一个新表。
from_pandas
< source >( *args **kwargs ) → datasets.table.Table
参数
- df (
pandas.DataFrame) — - schema (
pyarrow.Schema, optional) — The expected schema of the Arrow Table. This can be used to indicate the type of columns if we cannot infer it automatically. If passed, the output will have exactly this schema. Columns specified in the schema that are not found in the DataFrame columns or its index will raise an error. Additional columns or index levels in the DataFrame which are not specified in the schema will be ignored. - preserve_index (
bool, optional) — Whether to store the index as an additional column in the resultingTable. The default of None will store the index as a column, except for RangeIndex which is stored as metadata only. Usepreserve_index=Trueto force it to be stored as a column. - nthreads (
int, defaults toNone(may use up to system CPU count threads)) — If greater than 1, convert columns to Arrow in parallel using indicated number of threads. - columns (
List[str], optional) — List of column to be converted. IfNone, use all columns. - safe (
bool, defaults toTrue) — Check for overflows or other unsafe conversions,
返回
datasets.table.Table
Convert pandas.DataFrame to an Arrow Table.
The column types in the resulting Arrow Table are inferred from the dtypes of the pandas.Series in the DataFrame. In the case of non-object Series, the NumPy dtype is translated to its Arrow equivalent. In the case of object, we need to guess the datatype by looking at the Python objects in this Series.
Be aware that Series of the object dtype don’t carry enough information to always lead to a meaningful Arrow type. In the case that we cannot infer a type, e.g. because the DataFrame is of length 0 or the Series only contains None/nan objects, the type is set to null. This behavior can be avoided by constructing an explicit schema and passing it to this function.
from_arrays
< source >( *args **kwargs )
参数
- arrays (
List[Union[pyarrow.Array, pyarrow.ChunkedArray]]) — Equal-length arrays that should form the table. - names (
List[str], optional) — Names for the table columns. If not passed, schema must be passed. - schema (
Schema, defaults toNone) — Schema for the created table. If not passed, names must be passed. - metadata (
Union[dict, Mapping], defaults toNone) — Optional metadata for the schema (if inferred).
Construct a Table from Arrow arrays.
from_pydict
< source >( *args **kwargs )
Construct a Table from Arrow arrays or columns.
from_batches
< source >( *args **kwargs ) → datasets.table.Table
Construct a Table from a sequence or iterator of Arrow RecordBatches.
MemoryMappedTable
class datasets.table.MemoryMappedTable
< source >( table: Table path: str replays: typing.Optional[list[tuple[str, tuple, dict]]] = None )
The table is said memory mapped when it doesn’t use the user’s RAM but loads the data from the disk instead.
Pickling it doesn’t copy the data into memory. Instead, only the path to the memory mapped arrow file is pickled, as well as the list of transforms to “replay” when reloading the table from the disk.
Its implementation requires to store an history of all the transforms that were applied to the underlying pyarrow Table, so that they can be “replayed” when reloading the Table from the disk.
This is different from the InMemoryTable table, for which pickling does copy all the data in memory.
当数据适合内存时,必须使用 InMemoryTable,而 MemoryMapped 则保留用于大于内存的数据或当您希望应用程序的内存占用保持较低时。
validate
< source >( *args **kwargs )
执行验证检查。如果验证失败,则抛出异常。
默认情况下,只运行廉价的验证检查。传递 full=True 以进行彻底的验证检查(可能是 O(n))。
equals
< source >( *args **kwargs ) → bool
参数
- other (Table) — Table to compare against.
- check_metadata
bool, defaults toFalse) — Whether schema metadata equality should be checked as well.
返回
布尔值
检查两个表的内容是否相等。
to_batches
< source >( *args **kwargs )
将 Table 转换为(连续的)RecordBatch 对象列表。
将 Table 转换为 dict 或 OrderedDict。
to_pandas
< source >( *args **kwargs ) → pandas.Series or pandas.DataFrame
参数
- memory_pool (
MemoryPool, defaults toNone) — Arrow MemoryPool to use for allocations. Uses the default memory pool is not passed. - strings_to_categorical (
bool, defaults toFalse) — Encode string (UTF8) and binary types topandas.Categorical. - categories (
list, defaults toempty) — List of fields that should be returned aspandas.Categorical. Only applies to table-like data structures. - zero_copy_only (
bool, defaults toFalse) — Raise anArrowExceptionif this function call would require copying the underlying data. - integer_object_nulls (
bool, defaults toFalse) — Cast integers with nulls to objects. - date_as_object (
bool, defaults toTrue) — Cast dates to objects. IfFalse, convert todatetime64[ns]dtype. - timestamp_as_object (
bool, defaults toFalse) — Cast non-nanosecond timestamps (np.datetime64) to objects. This is useful if you have timestamps that don’t fit in the normal date range of nanosecond timestamps (1678 CE-2262 CE). IfFalse, all timestamps are converted todatetime64[ns]dtype. - use_threads (
bool, defaults toTrue) — Whether to parallelize the conversion using multiple threads. - deduplicate_objects (
bool, defaults toFalse) — Do not create multiple copies Python objects when created, to save on memory use. Conversion will be slower. - ignore_metadata (
bool, defaults toFalse) — IfTrue, do not use the ‘pandas’ metadata to reconstruct the DataFrame index, if present. - safe (
bool, defaults toTrue) — For certain data types, a cast is needed in order to store the data in a pandas DataFrame or Series (e.g. timestamps are always stored as nanoseconds in pandas). This option controls whether it is a safe cast or not. - split_blocks (
bool, defaults toFalse) — IfTrue, generate one internal “block” for each column when creating a pandas.DataFrame from aRecordBatchorTable. While this can temporarily reduce memory note that various pandas operations can trigger “consolidation” which may balloon memory use. - self_destruct (
bool, defaults toFalse) — EXPERIMENTAL: IfTrue, attempt to deallocate the originating Arrow memory while converting the Arrow object to pandas. If you use the object after callingto_pandaswith this option it will crash your program. - types_mapper (
function, defaults toNone) — A function mapping a pyarrow DataType to a pandasExtensionDtype. This can be used to override the default pandas type for conversion of built-in pyarrow types or in absence ofpandas_metadatain the Table schema. The function receives a pyarrow DataType and is expected to return a pandasExtensionDtypeorNoneif the default conversion should be used for that type. If you have a dictionary mapping, you can passdict.getas function.
返回
pandas.Series or pandas.DataFrame
pandas.Series 或 pandas.DataFrame,取决于对象类型
转换为 pandas 兼容的 NumPy 数组或 DataFrame,视情况而定。
field
< source >( *args **kwargs )
按列名或数字索引选择模式字段。
column
< source >( *args **kwargs )
按列名或数字索引选择列。
按数字顺序迭代所有列。
表及其列的模式。
所有列按数字顺序排列的列表。
此表中的列数。
表的维度:(#行, #列)。
表元素消耗的总字节数。
表的列名。
slice
< source >( offset = 0 length = None )
计算此表的零拷贝切片。
从表中选择记录。有关完整用法,请参阅 pyarrow.compute.filter。
flatten
< source >( *args **kwargs )
展平此表。每个具有 struct 类型的列都被展平为每个 struct 字段一个列。其他列保持不变。
combine_chunks
< source >( *args **kwargs )
通过组合此表拥有的块来创建一个新表。
All the underlying chunks in the ChunkedArray of each column are concatenated into zero or one chunk.
cast
< source >( *args **kwargs )
Cast table values to another schema
replace_schema_metadata
< source >( *args **kwargs ) → datasets.table.Table
EXPERIMENTAL: Create shallow copy of table by replacing schema key-value metadata with the indicated new metadata (which may be None, which deletes any existing metadata.
append_column
< source >( *args **kwargs ) → datasets.table.Table
在列尾添加列。
remove_column
< source >( *args **kwargs ) → datasets.table.Table
创建一个删除指定列的新表。
set_column
< source >( *args **kwargs ) → datasets.table.Table
替换表中指定位置的列。
创建一个将列重命名为指定名称的新表。
drop
< source >( *args **kwargs ) → datasets.table.Table
删除一个或多个列并返回一个新表。
ConcatenationTable
该表由多个名为“块”(blocks)的表连接而成。它支持沿轴0(附加行)和轴1(附加列)进行连接。
底层表称为“块”,可以是 InMemoryTable 或 MemoryMappedTable 对象。这允许组合来自内存或内存映射的表。当 ConcatenationTable 被序列化(pickled)时,每个块都会被序列化
InMemoryTable对象通过复制所有数据到内存中进行序列化。MemoryMappedTable对象在不复制数据到内存的情况下进行序列化。相反,只序列化内存映射 arrow 文件的路径,以及从磁盘重新加载表时要“重放”的转换列表。
其实现要求单独存储每个块。blocks 属性存储一个块的列表的列表。第一个轴沿轴0(附加行)连接表,而第二个轴沿轴1(附加列)连接表。
如果在沿轴0连接时缺少某些列,则会用空值填充。这是使用 pyarrow.concat_tables(tables, promote=True) 完成的。
您可以通过访问 ConcatenationTable.table 属性来访问完全组合的表,并通过访问 ConcatenationTable.blocks 属性来访问块。
validate
< source >( *args **kwargs )
执行验证检查。如果验证失败,则抛出异常。
默认情况下,只运行廉价的验证检查。传递 full=True 以进行彻底的验证检查(可能是 O(n))。
equals
< source >( *args **kwargs ) → bool
检查两个表的内容是否相等。
to_batches
< source >( *args **kwargs )
将 Table 转换为(连续的)RecordBatch 对象列表。
将 Table 转换为 dict 或 OrderedDict。
to_pandas
< source >( *args **kwargs ) → pandas.Series or pandas.DataFrame
参数
- memory_pool (
MemoryPool, defaults toNone) — 用于分配的 Arrow 内存池。如果未传入,则使用默认内存池。 - strings_to_categorical (
bool, defaults toFalse) — 将字符串(UTF8)和二进制类型编码为pandas.Categorical。 - categories (
list, defaults toempty) — 应作为pandas.Categorical返回的字段列表。仅适用于类似表的数据结构。 - zero_copy_only (
bool, defaults toFalse) — 如果此函数调用需要复制底层数据,则引发ArrowException。 - integer_object_nulls (
bool, defaults toFalse) — 将带空值的整数转换为对象。 - date_as_object (
bool, defaults toTrue) — 将日期转换为对象。如果为False,则转换为datetime64[ns]dtype。 - timestamp_as_object (
bool, defaults toFalse) — 将非纳秒时间戳(np.datetime64)转换为对象。如果您有不适合纳秒时间戳正常日期范围(1678 CE-2262 CE)的时间戳,这很有用。如果为False,所有时间戳都将转换为datetime64[ns]dtype。 - use_threads (
bool, defaults toTrue) — 是否使用多线程并行转换。 - deduplicate_objects (
bool, defaults toFalse) — 创建 Python 对象时不要创建多个副本,以节省内存使用。转换速度会变慢。 - ignore_metadata (
bool, defaults toFalse) — 如果为True,则不使用“pandas”元数据来重建 DataFrame 索引(如果存在)。 - safe (
bool, defaults toTrue) — 对于某些数据类型,需要进行转换才能将数据存储在 pandas DataFrame 或 Series 中(例如,时间戳在 pandas 中始终存储为纳秒)。此选项控制是否进行安全转换。 - split_blocks (
bool, defaults toFalse) — 如果为True,则在从RecordBatch或Table创建 pandas.DataFrame 时,为每个列生成一个内部“块”。虽然这可以暂时减少内存,但请注意,各种 pandas 操作可能会触发“合并”,从而导致内存使用量激增。 - self_destruct (
bool, defaults toFalse) — 实验性:如果为True,则在将 Arrow 对象转换为 pandas 时尝试释放原始 Arrow 内存。如果在调用to_pandas时使用此选项后使用该对象,程序将崩溃。 - types_mapper (
function, defaults toNone) — 将 pyarrow DataType 映射到 pandasExtensionDtype的函数。这可用于覆盖内置 pyarrow 类型的默认 pandas 类型转换,或者在表 schema 中缺少pandas_metadata时使用。该函数接收一个 pyarrow DataType,并应返回一个 pandasExtensionDtype,如果应使用该类型的默认转换,则返回None。如果您有一个字典映射,您可以将dict.get作为函数传入。
返回
pandas.Series or pandas.DataFrame
pandas.Series 或 pandas.DataFrame,取决于对象类型
转换为 pandas 兼容的 NumPy 数组或 DataFrame,视情况而定。
按列名或数字索引选择模式字段。
按列名或数字索引选择列。
按数字顺序迭代所有列。
表及其列的模式。
所有列按数字顺序排列的列表。
此表中的列数。
表的维度:(#行, #列)。
表元素消耗的总字节数。
表的列名。
slice
< source >( offset = 0 length = None )
计算此表的零拷贝切片。
从表中选择记录。有关完整用法,请参阅 pyarrow.compute.filter。
flatten
< source >( *args **kwargs )
展平此表。每个具有 struct 类型的列都被展平为每个 struct 字段一个列。其他列保持不变。
cast
< source >( target_schema *args **kwargs )
将表值转换为另一种模式。
replace_schema_metadata
< source >( *args **kwargs ) → datasets.table.Table
实验性:通过用指定的新元数据(可以是 None,这将删除任何现有元数据)替换架构键值元数据来创建表的浅拷贝。
append_column
< source >( *args **kwargs ) → datasets.table.Table
在列尾添加列。
remove_column
< source >( i *args **kwargs ) → datasets.table.Table
创建一个删除指定列的新表。
set_column
< source >( *args **kwargs ) → datasets.table.Table
替换表中指定位置的列。
创建一个将列重命名为指定名称的新表。
drop
< source >( columns *args **kwargs ) → datasets.table.Table
删除一个或多个列并返回一个新表。
from_tables
< source >( tables: list axis: int = 0 )
Create ConcatenationTable from list of tables.
Utils
datasets.table.concat_tables
< source >( tables: list axis: int = 0 ) → datasets.table.Table
Concatenate tables.
datasets.table.list_table_cache_files
< source >( table: Table ) → List[str]
返回
List[str]
A list of paths to the cache files loaded by the table.
获取由表格加载的缓存文件。缓存文件用于表格部分数据来自磁盘且通过内存映射加载的情况。