| [ Web Proxy ] |
| Viewing: https://arrow.apache.org/docs/python/generated/pyarrow.ChunkedArray.html#pyarrow.ChunkedArray | [Back] [Original] |
Bases: _PandasConvertible
An array-like composed from a (possibly empty) collection of pyarrow.Arrays
Warning
Do not call this classs constructor directly.
Examples
To construct a ChunkedArray object use pyarrow.chunked_array():
>>> import pyarrow as pa
>>> pa.chunked_array([], type=pa.int8())
<pyarrow.lib.ChunkedArray object at ...>
[
...
]
>>> pa.chunked_array([[2, 2, 4], [4, 5, 100]])
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> isinstance(pa.chunked_array([[2, 2, 4], [4, 5, 100]]), pa.ChunkedArray)
True
Methods
|
|
|
Cast array values to another data type |
|
Select a chunk by its index. |
|
Flatten this ChunkedArray into a single non-chunked array. |
|
Compute dictionary-encoded representation of array. |
|
Remove missing values from a chunked array. |
|
Return whether the contents of two chunked arrays are equal. |
|
Replace each null element in values with fill_value. |
|
Select values from the chunked array. |
|
Flatten this ChunkedArray. |
|
DEPRECATED, use pyarrow.ChunkedArray.to_string |
|
The sum of bytes in each buffer referenced by the chunked array. |
|
Find the first index of a value. |
|
Return boolean array indicating the NaN values. |
|
Return boolean array indicating the null values. |
|
Return boolean array indicating the non-null values. |
|
Convert to an iterator of ChunkArrays. |
|
Return length of a ChunkedArray. |
|
Compute zero-copy slice of this ChunkedArray |
|
Sort the ChunkedArray |
|
Select values from the chunked array. |
|
Return a NumPy copy of this array (experimental). |
|
Convert to a pandas-compatible NumPy array or DataFrame, as appropriate |
|
Convert to a list of native Python objects. |
|
Render a "pretty-printed" string representation of the ChunkedArray |
|
Unify dictionaries across all chunks. |
|
Compute distinct elements in array |
|
Perform validation checks. |
|
Compute counts of unique elements in array. |
Attributes
Convert to a list of single-chunked arrays. |
|
Whether all chunks in the ChunkedArray are CPU-accessible. |
|
Total number of bytes consumed by the elements of the chunked array. |
|
Number of null entries |
|
Number of underlying chunks. |
|
Return data type of a ChunkedArray. |
Cast array values to another data type
See pyarrow.compute.cast() for usage.
Array or ChunkedArrayExamples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs.type
DataType(int64)
Change the data type of an array:
>>> n_legs_seconds = n_legs.cast(pa.duration('s'))
>>> n_legs_seconds.type
DurationType(duration[s])
Select a chunk by its index.
intExamples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, None], [4, 5, 100]])
>>> n_legs.chunk(1)
<pyarrow.lib.Int64Array object at ...>
[
4,
5,
100
]
Convert to a list of single-chunked arrays.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, None], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
null
],
[
4,
5,
100
]
]
>>> n_legs.chunks
[<pyarrow.lib.Int64Array object at ...>
[
2,
2,
null
], <pyarrow.lib.Int64Array object at ...>
[
4,
5,
100
]]
Flatten this ChunkedArray into a single non-chunked array.
MemoryPool, default NoneFor memory allocations, if required, otherwise use default pool
ArrayExamples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> n_legs.combine_chunks()
<pyarrow.lib.Int64Array object at ...>
[
2,
2,
4,
4,
5,
100
]
Compute dictionary-encoded representation of array.
See pyarrow.compute.dictionary_encode() for full usage.
str, default maskHow to handle null entries.
ChunkedArrayA dictionary-encoded version of this array.
Examples
>>> import pyarrow as pa
>>> animals = pa.chunked_array((
... ["Flamingo", "Parrot", "Dog"],
... ["Horse", "Brittle stars", "Centipede"]
... ))
>>> animals.dictionary_encode()
<pyarrow.lib.ChunkedArray object at ...>
[
...
-- dictionary:
[
"Flamingo",
"Parrot",
"Dog",
"Horse",
"Brittle stars",
"Centipede"
]
-- indices:
[
0,
1,
2
],
...
-- dictionary:
[
"Flamingo",
"Parrot",
"Dog",
"Horse",
"Brittle stars",
"Centipede"
]
-- indices:
[
3,
4,
5
]
]
Remove missing values from a chunked array.
See pyarrow.compute.drop_null() for full description.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, None], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
null
],
[
4,
5,
100
]
]
>>> n_legs.drop_null()
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2
],
[
4,
5,
100
]
]
Return whether the contents of two chunked arrays are equal.
pyarrow.ChunkedArrayChunked array to compare against.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> animals = pa.chunked_array((
... ["Flamingo", "Parrot", "Dog"],
... ["Horse", "Brittle stars", "Centipede"]
... ))
>>> n_legs.equals(n_legs)
True
>>> n_legs.equals(animals)
False
Replace each null element in values with fill_value.
See pyarrow.compute.fill_null() for full usage.
anyThe replacement value for null entries.
Array or ChunkedArrayA new array with nulls replaced by the given value.
Examples
>>> import pyarrow as pa
>>> fill_value = pa.scalar(5, type=pa.int8())
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> n_legs.fill_null(fill_value)
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4,
4,
5,
100
]
]
Select values from the chunked array.
See pyarrow.compute.filter() for full usage.
Array or array-likeThe boolean mask to filter the chunked array with.
str, default dropHow nulls in the mask should be handled.
Array or ChunkedArrayAn array of the same type, with only the elements selected by the boolean mask.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> mask = pa.array([True, False, None, True, False, True])
>>> n_legs.filter(mask)
<pyarrow.lib.ChunkedArray object at ...>
[
[
2
],
[
4,
100
]
]
>>> n_legs.filter(mask, null_selection_behavior="emit_null")
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
null
],
[
4,
100
]
]
Flatten this ChunkedArray. If it has a struct type, the column is flattened into one array per struct field.
MemoryPool, default NoneFor memory allocations, if required, otherwise use default pool
list of ChunkedArrayExamples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> c_arr = pa.chunked_array(n_legs.value_counts())
>>> c_arr
<pyarrow.lib.ChunkedArray object at ...>
[
-- is_valid: all not null
-- child 0 type: int64
[
2,
4,
5,
100
]
-- child 1 type: int64
[
2,
2,
1,
1
]
]
>>> c_arr.flatten()
[<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
4,
5,
100
]
], <pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
1,
1
]
]]
>>> c_arr.type
StructType(struct<values: int64, counts: int64>)
>>> n_legs.type
DataType(int64)
DEPRECATED, use pyarrow.ChunkedArray.to_string
The sum of bytes in each buffer referenced by the chunked array.
An array may only reference a portion of a buffer. This method will overestimate in this case and return the byte size of the entire buffer.
If a buffer is referenced multiple times then it will only be counted once.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> n_legs.get_total_buffer_size()
49
Find the first index of a value.
See pyarrow.compute.index() for full usage.
Scalar or objectThe value to look for in the array.
int, optionalThe start index where to look for value.
int, optionalThe end index where to look for value.
MemoryPool, optionalA memory pool for potential memory allocations.
Int64ScalarThe index of the value in the array (-1 if not found).
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> n_legs.index(4)
<pyarrow.Int64Scalar: 2>
>>> n_legs.index(4, start=3)
<pyarrow.Int64Scalar: 3>
Whether all chunks in the ChunkedArray are CPU-accessible.
Return boolean array indicating the NaN values.
Examples
>>> import pyarrow as pa
>>> import numpy as np
>>> arr = pa.chunked_array([[2, np.nan, 4], [4, None, 100]])
>>> arr.is_nan()
<pyarrow.lib.ChunkedArray object at ...>
[
[
false,
true,
false,
false,
null,
false
]
]
Return boolean array indicating the null values.
Array or ChunkedArrayExamples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> n_legs.is_null()
<pyarrow.lib.ChunkedArray object at ...>
[
[
false,
false,
false,
false,
true,
false
]
]
Return boolean array indicating the non-null values.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> n_legs.is_valid()
<pyarrow.lib.ChunkedArray object at ...>
[
[
true,
true,
true
],
[
true,
false,
true
]
]
Convert to an iterator of ChunkArrays.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> for i in n_legs.iterchunks():
... print(i.null_count)
...
0
1
Return length of a ChunkedArray.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs.length()
6
Total number of bytes consumed by the elements of the chunked array.
In other words, the sum of bytes from all buffer ranges referenced.
Unlike get_total_buffer_size this method will account for array offsets.
If buffers are shared between arrays then the shared portion will only be counted multiple times.
The dictionary of dictionary arrays will always be counted in their entirety even if the array only references a portion of the dictionary.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> n_legs.nbytes
49
Number of null entries
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> n_legs.null_count
1
Number of underlying chunks.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, None], [4, 5, 100]])
>>> n_legs.num_chunks
2
Compute zero-copy slice of this ChunkedArray
ChunkedArrayExamples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> n_legs.slice(2,2)
<pyarrow.lib.ChunkedArray object at ...>
[
[
4
],
[
4
]
]
Sort the ChunkedArray
str, default ascendingWhich order to sort values in. Accepted values are ascending, descending.
str, default at_endWhether nulls and NaNs are placed at the start or at the end. Accepted values are at_end, at_start.
dict, optionalAdditional sorting options.
As allowed by SortOptions
ChunkedArraySelect values from the chunked array.
See pyarrow.compute.take() for full usage.
Array or array-likeThe indices in the array whose values will be returned.
Array or ChunkedArrayAn array with the same datatype, containing the taken values.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> n_legs.take([1,4,5])
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
5,
100
]
]
Return a NumPy copy of this array (experimental).
numpy.ndarrayExamples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs.to_numpy()
array([ 2, 2, 4, 4, 5, 100])
Convert to a pandas-compatible NumPy array or DataFrame, as appropriate
MemoryPool, default NoneArrow MemoryPool to use for allocations. Uses the default memory pool if not passed.
list, default emptyList of fields that should be returned as pandas.Categorical. Only applies to table-like data structures.
FalseEncode string (UTF8) and binary types to pandas.Categorical.
FalseRaise an ArrowException if this function call would require copying the underlying data.
FalseCast integers with nulls to objects
TrueCast dates to objects. If False, convert to datetime64 dtype with the equivalent time unit (if supported). Note: in pandas version < 2.0, only datetime64[ns] conversion is supported.
FalseCast non-nanosecond timestamps (np.datetime64) to objects. This is useful in pandas version 1.x if you have timestamps that dont fit in the normal date range of nanosecond timestamps (1678 CE-2262 CE). Non-nanosecond timestamps are supported in pandas version 2.0. If False, all timestamps are converted to datetime64 dtype.
TrueWhether to parallelize the conversion using multiple threads.
TrueDo not create multiple copies Python objects when created, to save on memory use. Conversion will be slower.
FalseIf True, do not use the pandas metadata to reconstruct the DataFrame index, if present
TrueFor 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.
FalseIf True, generate one internal block for each column when creating a pandas.DataFrame from a RecordBatch or Table. While this can temporarily reduce memory note that various pandas operations can trigger consolidation which may balloon memory use.
FalseEXPERIMENTAL: If True, attempt to deallocate the originating Arrow memory while converting the Arrow object to pandas. If you use the object after calling to_pandas with this option it will crash your program.
Note that you may not see always memory usage improvements. For example, if multiple columns share an underlying allocation, memory cant be freed until all columns are converted.
str, optional, default NoneValid values are None, lossy, or strict. The default behavior (None), is to convert Arrow Map arrays to Python association lists (list-of-tuples) in the same order as the Arrow Map, as in [(key1, value1), (key2, value2), ].
If lossy or strict, convert Arrow Map arrays to native Python dicts. This can change the ordering of (key, value) pairs, and will deduplicate multiple keys, resulting in a possible loss of data.
If lossy, this key deduplication results in a warning printed when detected. If strict, this instead results in an exception being raised when detected.
NoneA function mapping a pyarrow DataType to a pandas ExtensionDtype.
This can be used to override the default pandas type for conversion
of built-in pyarrow types or in absence of pandas_metadata in the
Table schema. The function receives a pyarrow DataType and is
expected to return a pandas ExtensionDtype or None if the
default conversion should be used for that type. If you have
a dictionary mapping, you can pass dict.get as function.
FalseOnly applicable to pandas version >= 2.0. A legacy option to coerce date32, date64, duration, and timestamp time units to nanoseconds when converting to pandas. This is the default behavior in pandas version 1.x. Set this option to True if youd like to use this coercion when using pandas version >= 2.0 for backwards compatibility (not recommended otherwise).
pandas.Series or pandas.DataFrame depending on type of objectExamples
>>> import pyarrow as pa
>>> import pandas as pd
Convert a Table to pandas DataFrame:
>>> table = pa.table([
... pa.array([2, 4, 5, 100]),
... pa.array(["Flamingo", "Horse", "Brittle stars", "Centipede"])
... ], names=['n_legs', 'animals'])
>>> table.to_pandas()
n_legs animals
0 2 Flamingo
1 4 Horse
2 5 Brittle stars
3 100 Centipede
>>> isinstance(table.to_pandas(), pd.DataFrame)
True
Convert a RecordBatch to pandas DataFrame:
>>> import pyarrow as pa
>>> n_legs = pa.array([2, 4, 5, 100])
>>> animals = pa.array(["Flamingo", "Horse", "Brittle stars", "Centipede"])
>>> batch = pa.record_batch([n_legs, animals],
... names=["n_legs", "animals"])
>>> batch
pyarrow.RecordBatch
n_legs: int64
animals: string
----
n_legs: [2,4,5,100]
animals: ["Flamingo","Horse","Brittle stars","Centipede"]
>>> batch.to_pandas()
n_legs animals
0 2 Flamingo
1 4 Horse
2 5 Brittle stars
3 100 Centipede
>>> isinstance(batch.to_pandas(), pd.DataFrame)
True
Convert a Chunked Array to pandas Series:
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs.to_pandas()
0 2
1 2
2 4
3 4
4 5
5 100
dtype: int64
>>> isinstance(n_legs.to_pandas(), pd.Series)
True
Convert to a list of native Python objects.
str, optional, default NoneValid values are None, lossy, or strict. The default behavior (None), is to convert Arrow Map arrays to Python association lists (list-of-tuples) in the same order as the Arrow Map, as in [(key1, value1), (key2, value2), ].
If lossy or strict, convert Arrow Map arrays to native Python dicts.
If lossy, whenever duplicate keys are detected, a warning will be printed. The last seen value of a duplicate key will be in the Python dictionary. If strict, this instead results in an exception being raised when detected.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, None, 100]])
>>> n_legs.to_pylist()
[2, 2, 4, 4, None, 100]
Render a pretty-printed string representation of the ChunkedArray
intHow much to indent right the content of the array,
by default 0.
intHow many items to preview within each chunk at the begin and end of the chunk when the chunk is bigger than the window. The other elements will be ellipsed.
intHow many chunks to preview at the begin and end of the array when the array is bigger than the window. The other elements will be ellipsed. This setting also applies to list columns.
If the array should be rendered as a single line of text or if each element should be on its own line.
int, default 100Maximum number of characters of a single element before it is truncated.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs.to_string(skip_new_lines=True)
'[[2,2,4],[4,5,100]]'
Return data type of a ChunkedArray.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs.type
DataType(int64)
Unify dictionaries across all chunks.
This method returns an equivalent chunked array, but where all chunks share the same dictionary values. Dictionary indices are transposed accordingly.
If there are no dictionaries in the chunked array, it is returned unchanged.
MemoryPool, default NoneFor memory allocations, if required, otherwise use default pool
ChunkedArrayExamples
>>> import pyarrow as pa
>>> arr_1 = pa.array(["Flamingo", "Parrot", "Dog"]).dictionary_encode()
>>> arr_2 = pa.array(["Horse", "Brittle stars", "Centipede"]).dictionary_encode()
>>> c_arr = pa.chunked_array([arr_1, arr_2])
>>> c_arr
<pyarrow.lib.ChunkedArray object at ...>
[
...
-- dictionary:
[
"Flamingo",
"Parrot",
"Dog"
]
-- indices:
[
0,
1,
2
],
...
-- dictionary:
[
"Horse",
"Brittle stars",
"Centipede"
]
-- indices:
[
0,
1,
2
]
]
>>> c_arr.unify_dictionaries()
<pyarrow.lib.ChunkedArray object at ...>
[
...
-- dictionary:
[
"Flamingo",
"Parrot",
"Dog",
"Horse",
"Brittle stars",
"Centipede"
]
-- indices:
[
0,
1,
2
],
...
-- dictionary:
[
"Flamingo",
"Parrot",
"Dog",
"Horse",
"Brittle stars",
"Centipede"
]
-- indices:
[
3,
4,
5
]
]
Compute distinct elements in array
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> n_legs.unique()
<pyarrow.lib.Int64Array object at ...>
[
2,
4,
5,
100
]
Perform validation checks. An exception is raised if validation fails.
By default only cheap validation checks are run. Pass full=True for thorough validation checks (potentially O(n)).
Compute counts of unique elements in array.
Examples
>>> import pyarrow as pa
>>> n_legs = pa.chunked_array([[2, 2, 4], [4, 5, 100]])
>>> n_legs
<pyarrow.lib.ChunkedArray object at ...>
[
[
2,
2,
4
],
[
4,
5,
100
]
]
>>> n_legs.value_counts()
<pyarrow.lib.StructArray object at ...>
-- is_valid: all not null
-- child 0 type: int64
[
2,
4,
5,
100
]
-- child 1 type: int64
[
2,
2,
1,
1
]
| Web Proxy Viewer | New URL | Original Page |