graviti.dataframe.frame
#
The implementation of the Graviti DataFrame.
Module Contents#
Classes#
Two-dimensional, size-mutable, potentially heterogeneous tabular data. |
- class graviti.dataframe.frame.DataFrame[source]#
Bases:
graviti.dataframe.container.Container
Two-dimensional, size-mutable, potentially heterogeneous tabular data.
- Parameters
data – The data that needs to be stored in DataFrame.
schema – The schema of the DataFrame. If None, will be inferred from data.
columns – Column labels to use for resulting frame when data does not have them, defaulting to RangeIndex(0, 1, 2, …, n). If data contains column labels, will perform column selection instead.
Examples
Constructing DataFrame from list.
>>> df = DataFrame( ... [ ... {"filename": "a.jpg", "box2ds": {"x": 1, "y": 1}}, ... {"filename": "b.jpg", "box2ds": {"x": 2, "y": 2}}, ... {"filename": "c.jpg", "box2ds": {"x": 3, "y": 3}}, ... ] ... ) >>> df filename box2ds x y 0 a.jpg 1 1 1 b.jpg 2 2 2 c.jpg 3 3
- classmethod from_pyarrow(cls, array, schema=None)[source]#
Create DataFrame with pyarrow struct array.
- Parameters
array (pyarrow.StructArray) – The input pyarrow struct array.
schema (Optional[graviti.portex.PortexType]) – The schema of the DataFrame.
cls (Type[_T]) –
- Raises
TypeError – When the given schema is mismatched with the pyarrow array type.
- Returns
The loaded
DataFrame
instance.- Return type
_T
- property iloc(self)[source]#
Purely integer-location based indexing for selection by position.
Allowed inputs are:
An integer, e.g.
5
.A list or array of integers, e.g.
[4, 3, 0]
.A slice object with ints, e.g.
1:7
.A boolean array of the same length as the axis being sliced.
- Returns
The instance of the ILocIndexer.
- Return type
Examples
>>> df = DataFrame({"col1": [1, 2], "col2": [3, 4]}) >>> df.iloc[0] col1 1 col2 3 Name: 0, dtype: int64 >>> df.iloc[[0]] col1 col2 0 1 3
- property loc(self)[source]#
Access a group of rows and columns by indexes or a boolean array.
Allowed inputs are:
A single index, e.g.
5
.A list or array of indexes, e.g.
[4, 3, 0]
.A slice object with indexes, e.g.
1:7
.A boolean array of the same length as the axis being sliced.
- Returns
The instance of the LocIndexer.
- Return type
Examples
>>> df = DataFrame({"col1": [1, 2], "col2": [3, 4]}) >>> df.loc[0] col1 1 col2 3 Name: 0, dtype: int64 >>> df.loc[[0]] col1 col2 0 1 3
- property shape(self)[source]#
Return a tuple representing the dimensionality of the DataFrame.
- Returns
Shape of the DataFrame.
- Return type
Tuple[int, int]
Examples
>>> df = DataFrame( ... [ ... {"filename": "a.jpg", "box2ds": {"x": 1, "y": 1}}, ... {"filename": "b.jpg", "box2ds": {"x": 2, "y": 2}}, ... {"filename": "c.jpg", "box2ds": {"x": 3, "y": 3}}, ... ] ... ) >>> df filename box2ds x y 0 a.jpg 1 1 1 b.jpg 2 2 2 c.jpg 3 3 >>> df.shape (3, 2)
- property size(self)[source]#
Return an int representing the number of elements in this object.
- Returns
Size of the DataFrame.
- Return type
int
Examples
>>> df = DataFrame({"col1": [1, 2], "col2": [3, 4]}) >>> df.size 4
- head(self, n=5)[source]#
Return the first n rows.
- Parameters
n (int) – Number of rows to select.
- Returns
The first n rows.
- Return type
Examples
>>> df = DataFrame( ... { ... "animal": [ ... "alligator", ... "bee", ... "falcon", ... "lion", ... "monkey", ... "parrot", ... "shark", ... "whale", ... "zebra", ... ] ... } ... ) >>> df animal 0 alligator 1 bee 2 falcon 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra
Viewing the first n lines (three in this case)
>>> df.head(3) animal 0 alligator 1 bee 2 falcon
For negative values of n
>>> df.head(-3) animal 0 alligator 1 bee 2 falcon 3 lion 4 monkey 5 parrot
- tail(self, n=5)[source]#
Return the last n rows.
- Parameters
n (int) – Number of rows to select.
- Returns
The last n rows.
- Return type
Examples
>>> df = DataFrame( ... { ... "animal": [ ... "alligator", ... "bee", ... "falcon", ... "lion", ... "monkey", ... "parrot", ... "shark", ... "whale", ... "zebra", ... ] ... } ... ) >>> df animal 0 alligator 1 bee 2 falcon 3 lion 4 monkey 5 parrot 6 shark 7 whale 8 zebra
Viewing the last 5 lines
>>> df.tail() animal 4 monkey 5 parrot 6 shark 7 whale 8 zebra
Viewing the last n lines (three in this case)
>>> df.tail(3) animal 6 shark 7 whale 8 zebra
- copy(self)[source]#
Get a copy of the dataframe.
- Returns
A copy of the dataframe.
- Parameters
self (_T) –
- Return type
_T
- sample(self, n=None, axis=None)[source]#
Return a random sample of items from an axis of object.
- Parameters
n (Optional[int]) – Number of items from axis to return.
axis (Optional[int]) – {0 or index, 1 or columns, None} Axis to sample. Accepts axis number or name. Default is stat axis for given data type (0 for Series and DataFrames).
- Returns
A new object of same type as caller containing n items randomly sampled from the caller object.
- Return type
- extend(self, values)[source]#
Extend Sequence object or DataFrame to itself row by row.
- Parameters
values (Union[Iterable[Dict[str, Any]], DataFrame]) – A sequence object or DataFrame.
- Raises
TypeError – When the given Dataframe mismatched with the self schema.
- Return type
None
Examples
>>> df = DataFrame([ ... {"filename": "a.jpg", "box2ds": {"x": 1, "y": 1}}, ... {"filename": "b.jpg", "box2ds": {"x": 2, "y": 2}}, ... ])
Extended by another list.
>>> df.extend([{"filename": "c.jpg", "box2ds": {"x": 3, "y": 3}}]) >>> df filename box2ds x y 0 a.jpg 1 1 1 b.jpg 2 2 2 c.jpg 3 3
Extended by another DataFrame.
>>> df2 = DataFrame([{"filename": "d.jpg", "box2ds": {"x": 4 "y": 4}}]) >>> df.extend(df2) >>> df filename box2ds x y 0 a.jpg 1 1 1 b.jpg 2 2 2 d.jpg 4 4