evo-objects 0.9.0__tar.gz → 0.9.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {evo_objects-0.9.0 → evo_objects-0.9.1}/PKG-INFO +12 -1
- {evo_objects-0.9.0 → evo_objects-0.9.1}/pyproject.toml +1 -1
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/client/object_client.py +4 -2
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/parquet/loader.py +7 -2
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/utils/__init__.py +10 -4
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/utils/data.py +24 -31
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/utils/tables.py +108 -7
- {evo_objects-0.9.0 → evo_objects-0.9.1}/.gitignore +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/LICENSE.md +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/__init__.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/_model_config.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/client/__init__.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/client/api_client.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/client/parse.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/data.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/endpoints/__init__.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/endpoints/api/__init__.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/endpoints/api/data_api.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/endpoints/api/metadata_api.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/endpoints/api/objects_api.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/endpoints/api/stages_api.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/endpoints/models.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/exceptions.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/io.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/parquet/__init__.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/py.typed +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/__init__.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/_data.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/_downhole.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/_grid.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/_model.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/_prefetch.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/_utils.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/attributes.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/base.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/block_model_ref.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/downhole_collection.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/downhole_intervals.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/exceptions.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/pointset.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/regular_grid.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/regular_masked_grid.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/spatial.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/tensor_grid.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/types.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/typed/variogram.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/utils/downhole.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/utils/table_formats.py +0 -0
- {evo_objects-0.9.0 → evo_objects-0.9.1}/src/evo/objects/utils/types.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: evo-objects
|
|
3
|
-
Version: 0.9.
|
|
3
|
+
Version: 0.9.1
|
|
4
4
|
Summary: Python SDK for using the Seequent Evo Geoscience Object API
|
|
5
5
|
Project-URL: Source, https://github.com/SeequentEvo/evo-python-sdk
|
|
6
6
|
Project-URL: Tracker, https://github.com/SeequentEvo/evo-python-sdk/issues
|
|
@@ -51,6 +51,17 @@ data, empowering our users to build responsive modern workflows.
|
|
|
51
51
|
pip install evo-objects
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
+
The base installation supports object operations and low-level data transfers without PyArrow.
|
|
55
|
+
`ObjectAPIClient.prepare_data_upload()` and `prepare_data_download()` return contexts for transferring existing Parquet files. You must create or read the files yourself and provide matching table descriptions when creating objects.
|
|
56
|
+
|
|
57
|
+
The optional `ObjectDataClient` provides higher-level operations on PyArrow tables without requiring pandas. Its
|
|
58
|
+
DataFrame methods and other pandas-based helpers additionally require pandas. Install the `utils` extra for these
|
|
59
|
+
dependencies:
|
|
60
|
+
|
|
61
|
+
```shell
|
|
62
|
+
pip install "evo-objects[utils]"
|
|
63
|
+
```
|
|
64
|
+
|
|
54
65
|
## Usage
|
|
55
66
|
|
|
56
67
|
See [the evo-sdk-common documentation](https://github.com/SeequentEvo/evo-python-sdk/blob/main/packages/evo-sdk-common/README.md)
|
|
@@ -40,6 +40,7 @@ try:
|
|
|
40
40
|
ParquetLoader,
|
|
41
41
|
TableInfo,
|
|
42
42
|
)
|
|
43
|
+
from ..utils.tables import table_to_dataframe
|
|
43
44
|
except ImportError as e:
|
|
44
45
|
print(e)
|
|
45
46
|
_LOADER_AVAILABLE = False
|
|
@@ -486,10 +487,11 @@ class DownloadedObject(IContext):
|
|
|
486
487
|
nan_description structure.
|
|
487
488
|
:param column_names: An optional list of column names for the table, instead of those from the Parquet file.
|
|
488
489
|
|
|
489
|
-
:returns: A Pandas DataFrame containing the downloaded data.
|
|
490
|
+
:returns: A Pandas DataFrame containing the downloaded data. Column dtypes are derived from the
|
|
491
|
+
Geoscience Object column types; integer and boolean columns use pandas nullable dtypes.
|
|
490
492
|
"""
|
|
491
493
|
table = await self.download_table(table_info, fb=fb, nan_values=nan_values, column_names=column_names)
|
|
492
|
-
return table
|
|
494
|
+
return table_to_dataframe(table)
|
|
493
495
|
|
|
494
496
|
async def download_category_dataframe(
|
|
495
497
|
self,
|
|
@@ -28,6 +28,7 @@ from evo.common.utils import NoFeedback
|
|
|
28
28
|
|
|
29
29
|
from ..exceptions import SchemaValidationError, TableFormatError
|
|
30
30
|
from ..utils import ArrowTableFormat, KnownTableFormat, TableInfo
|
|
31
|
+
from ..utils.tables import table_to_dataframe
|
|
31
32
|
|
|
32
33
|
try:
|
|
33
34
|
import pandas as pd
|
|
@@ -140,9 +141,13 @@ class ParquetLoader:
|
|
|
140
141
|
# Optional support for pandas dataframes
|
|
141
142
|
|
|
142
143
|
def load_as_dataframe(self) -> pd.DataFrame:
|
|
143
|
-
"""Load the Parquet data as a Pandas DataFrame.
|
|
144
|
+
"""Load the Parquet data as a Pandas DataFrame.
|
|
145
|
+
|
|
146
|
+
Column dtypes are derived from the Geoscience Object column types, never from pandas metadata in the file.
|
|
147
|
+
Integer and boolean columns use pandas nullable dtypes (e.g. `Int64`, `boolean`).
|
|
148
|
+
"""
|
|
144
149
|
table = self.load_as_table()
|
|
145
|
-
return table
|
|
150
|
+
return table_to_dataframe(table)
|
|
146
151
|
|
|
147
152
|
if _NP_AVAILABLE:
|
|
148
153
|
# Optional support for numpy arrays
|
|
@@ -23,9 +23,8 @@ except ImportError:
|
|
|
23
23
|
DataFrame = None # type: ignore
|
|
24
24
|
|
|
25
25
|
from .data import ObjectDataClient
|
|
26
|
-
from .downhole import expand_hole_index, hole_chunks_from_ids
|
|
27
26
|
from .table_formats import all_known_formats, get_known_format
|
|
28
|
-
from .tables import ArrowTableFormat, BaseTableFormat, KnownTableFormat
|
|
27
|
+
from .tables import ArrowTableFormat, BaseTableFormat, ColumnFormat, KnownTableFormat
|
|
29
28
|
from .types import ArrayTableInfo, AttributeInfo, CategoryInfo, LookupTableInfo, TableInfo
|
|
30
29
|
|
|
31
30
|
# We _used_ to export Table and DataFrame from this package as custom protocols, but we are using the actual
|
|
@@ -39,12 +38,19 @@ __all__ = [
|
|
|
39
38
|
"AttributeInfo",
|
|
40
39
|
"BaseTableFormat",
|
|
41
40
|
"CategoryInfo",
|
|
41
|
+
"ColumnFormat",
|
|
42
42
|
"KnownTableFormat",
|
|
43
43
|
"LookupTableInfo",
|
|
44
44
|
"ObjectDataClient",
|
|
45
45
|
"TableInfo",
|
|
46
46
|
"all_known_formats",
|
|
47
|
-
"expand_hole_index",
|
|
48
47
|
"get_known_format",
|
|
49
|
-
"hole_chunks_from_ids",
|
|
50
48
|
]
|
|
49
|
+
|
|
50
|
+
if DataFrame is not None:
|
|
51
|
+
from .downhole import expand_hole_index, hole_chunks_from_ids
|
|
52
|
+
|
|
53
|
+
__all__ += [
|
|
54
|
+
"expand_hole_index",
|
|
55
|
+
"hole_chunks_from_ids",
|
|
56
|
+
]
|
|
@@ -26,7 +26,7 @@ from evo.common.utils import NoFeedback, PartialFeedback, split_feedback
|
|
|
26
26
|
from ..exceptions import TableFormatError
|
|
27
27
|
from ..io import _CACHE_SCOPE, ObjectDataDownload, ObjectDataUpload
|
|
28
28
|
from .table_formats import INTEGER_ARRAY_1_INT32, INTEGER_ARRAY_MD_INT32, LOOKUP_TABLE_INT32
|
|
29
|
-
from .tables import KnownTableFormat
|
|
29
|
+
from .tables import ArrowTableFormat, KnownTableFormat, table_to_dataframe
|
|
30
30
|
from .types import ArrayTableInfo, CategoryInfo, LookupTableInfo
|
|
31
31
|
|
|
32
32
|
try:
|
|
@@ -69,27 +69,19 @@ def _iter_refs(target: Any, _key: str | None = None) -> Iterator[str]:
|
|
|
69
69
|
yield str(value)
|
|
70
70
|
|
|
71
71
|
|
|
72
|
-
def
|
|
73
|
-
"""
|
|
72
|
+
def _table_from_dataframe(dataframe: "pd.DataFrame") -> pa.Table:
|
|
73
|
+
"""Convert a pandas dataframe to a pyarrow table for upload.
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
- large_string -> string
|
|
78
|
-
- dictionary<*, large_string> -> dictionary<*, string>
|
|
75
|
+
The dataframe index is not part of any Geoscience Object table format, so it is never stored. Pandas metadata is
|
|
76
|
+
removed, so readers rely only on the Geoscience Object schema.
|
|
79
77
|
|
|
80
|
-
:param dataframe: The pandas dataframe to
|
|
81
|
-
:return: A pyarrow
|
|
78
|
+
:param dataframe: The pandas dataframe to convert.
|
|
79
|
+
:return: A pyarrow table with the normalized schema from `ArrowTableFormat.from_pandas`.
|
|
82
80
|
"""
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
fields.append(field.with_type(pa.string()))
|
|
88
|
-
elif pa.types.is_dictionary(field.type) and pa.types.is_large_string(field.type.value_type):
|
|
89
|
-
fields.append(field.with_type(pa.dictionary(field.type.index_type, pa.string())))
|
|
90
|
-
else:
|
|
91
|
-
fields.append(field)
|
|
92
|
-
return pa.schema(fields)
|
|
81
|
+
table_format = ArrowTableFormat.from_pandas(dataframe)
|
|
82
|
+
return pa.Table.from_pandas(
|
|
83
|
+
dataframe, schema=table_format.pa_schema, preserve_index=False
|
|
84
|
+
).replace_schema_metadata()
|
|
93
85
|
|
|
94
86
|
|
|
95
87
|
class ObjectDataClient:
|
|
@@ -226,7 +218,8 @@ class ObjectDataClient:
|
|
|
226
218
|
both the value and lookup tables.
|
|
227
219
|
|
|
228
220
|
The table columns must be string columns, but can be dictionary encoded. If they are dictionary encoded, this
|
|
229
|
-
method takes advantage of that, when building the value and lookup tables.
|
|
221
|
+
method takes advantage of that, when building the value and lookup tables. Large strings are converted to
|
|
222
|
+
strings.
|
|
230
223
|
|
|
231
224
|
:param table: The table to be uploaded.
|
|
232
225
|
:param fb: A feedback object for tracking upload progress.
|
|
@@ -241,11 +234,13 @@ class ObjectDataClient:
|
|
|
241
234
|
columns_chunk_range = []
|
|
242
235
|
for column in table.itercolumns():
|
|
243
236
|
column = column.dictionary_encode()
|
|
244
|
-
|
|
237
|
+
value_type = column.type.value_type
|
|
238
|
+
if not (pa.types.is_string(value_type) or pa.types.is_large_string(value_type)):
|
|
245
239
|
raise TableFormatError("Category columns must be of type string")
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
240
|
+
# Currently, we only support int32 indices and string values
|
|
241
|
+
category_type = pa.dictionary(pa.int32(), pa.string())
|
|
242
|
+
if column.type != category_type:
|
|
243
|
+
column = pc.cast(column, category_type)
|
|
249
244
|
start_chunk = len(all_chunks)
|
|
250
245
|
if isinstance(column, pa.ChunkedArray):
|
|
251
246
|
all_chunks.extend(column.chunks)
|
|
@@ -426,8 +421,7 @@ class ObjectDataClient:
|
|
|
426
421
|
no table formats are specified, raised when the table does not match any known format.
|
|
427
422
|
:raises StorageFileNotFoundError: If the destination does not exist or is not a directory.
|
|
428
423
|
"""
|
|
429
|
-
|
|
430
|
-
table = pa.Table.from_pandas(dataframe, schema)
|
|
424
|
+
table = _table_from_dataframe(dataframe)
|
|
431
425
|
return self.save_table(table, table_format=table_format)
|
|
432
426
|
|
|
433
427
|
async def upload_dataframe(
|
|
@@ -450,8 +444,7 @@ class ObjectDataClient:
|
|
|
450
444
|
:raises TableFormatError: If the provided table does not match any of the specified formats. If
|
|
451
445
|
no table formats are specified, raised when the table does not match any known format.
|
|
452
446
|
"""
|
|
453
|
-
|
|
454
|
-
table = pa.Table.from_pandas(dataframe, schema)
|
|
447
|
+
table = _table_from_dataframe(dataframe)
|
|
455
448
|
table_info = await self.upload_table(table, table_format=table_format, fb=fb)
|
|
456
449
|
return table_info
|
|
457
450
|
|
|
@@ -472,8 +465,8 @@ class ObjectDataClient:
|
|
|
472
465
|
:raises TableFormatError: If the table isn't a valid category table, or if the number of categories exceeds
|
|
473
466
|
what int32 type can represent.
|
|
474
467
|
"""
|
|
475
|
-
|
|
476
|
-
table = pa.Table.from_pandas(dataframe,
|
|
468
|
+
# Keep dictionary encoding until the categories are split into value and lookup tables.
|
|
469
|
+
table = pa.Table.from_pandas(dataframe, preserve_index=False).replace_schema_metadata()
|
|
477
470
|
category_info = await self.upload_category_table(table, fb=fb)
|
|
478
471
|
return category_info
|
|
479
472
|
|
|
@@ -499,6 +492,6 @@ class ObjectDataClient:
|
|
|
499
492
|
"""
|
|
500
493
|
table = await self.download_table(object_id, version_id, table_info, fb)
|
|
501
494
|
try:
|
|
502
|
-
return table
|
|
495
|
+
return table_to_dataframe(table)
|
|
503
496
|
except ModuleNotFoundError:
|
|
504
497
|
raise RuntimeError("Unable to download dataframe because the `pandas` package is not installed")
|
|
@@ -13,6 +13,7 @@ from __future__ import annotations
|
|
|
13
13
|
|
|
14
14
|
import hashlib
|
|
15
15
|
import os
|
|
16
|
+
from collections.abc import Callable
|
|
16
17
|
from pathlib import Path
|
|
17
18
|
from tempfile import NamedTemporaryFile
|
|
18
19
|
from types import EllipsisType
|
|
@@ -26,20 +27,50 @@ from evo.common.exceptions import StorageFileNotFoundError
|
|
|
26
27
|
|
|
27
28
|
from ..exceptions import SchemaValidationError, TableFormatError
|
|
28
29
|
|
|
30
|
+
try:
|
|
31
|
+
import pandas as pd
|
|
32
|
+
except ImportError:
|
|
33
|
+
_PD_AVAILABLE = False
|
|
34
|
+
else:
|
|
35
|
+
_PD_AVAILABLE = True
|
|
36
|
+
|
|
29
37
|
logger = evo.logging.getLogger("object.tables")
|
|
30
38
|
|
|
31
39
|
__all__ = [
|
|
32
40
|
"ArrowTableFormat",
|
|
33
41
|
"BaseTableFormat",
|
|
42
|
+
"ColumnFormat",
|
|
34
43
|
"KnownTableFormat",
|
|
44
|
+
"table_to_dataframe",
|
|
35
45
|
]
|
|
36
46
|
|
|
37
47
|
# Block size to limit memory use while calculating the sha256 digest of a file.
|
|
38
48
|
_DIGEST_BLOCK_SIZE = 4 * 1024 * 1024
|
|
39
49
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
50
|
+
# Pandas dtype for every supported Geoscience Object column type. Integer and boolean columns use pandas nullable
|
|
51
|
+
# dtypes, so nulls do not change the dtype. `None` keeps the pyarrow default conversion: pyarrow `types_mapper` only
|
|
52
|
+
# accepts pandas extension dtypes, so numpy-backed `float64` and the pandas default string dtype cannot be named here.
|
|
53
|
+
_PANDAS_DTYPES: dict[str, str | None] = {
|
|
54
|
+
"float64": None,
|
|
55
|
+
"uint8": "UInt8",
|
|
56
|
+
"uint32": "UInt32",
|
|
57
|
+
"uint64": "UInt64",
|
|
58
|
+
"int32": "Int32",
|
|
59
|
+
"int64": "Int64",
|
|
60
|
+
"bool": "boolean",
|
|
61
|
+
"string": None,
|
|
62
|
+
"timestamp": "datetime64[us, UTC]",
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class ColumnFormat:
|
|
67
|
+
"""A Geoscience Object column type and its corresponding Arrow data type."""
|
|
68
|
+
|
|
69
|
+
def __init__(self, format_spec: pa.DataType | str) -> None:
|
|
70
|
+
"""
|
|
71
|
+
:param format_spec: A Geoscience Object column type identifier or a supported Arrow data type.
|
|
72
|
+
:raises TypeError: If the column type is not supported.
|
|
73
|
+
"""
|
|
43
74
|
if isinstance(format_spec, str):
|
|
44
75
|
self._type = self._get_data_type(format_spec)
|
|
45
76
|
self._format_id = format_spec
|
|
@@ -97,12 +128,29 @@ class _ColumnFormat:
|
|
|
97
128
|
|
|
98
129
|
@property
|
|
99
130
|
def id(self) -> str:
|
|
131
|
+
"""The Geoscience Object column type identifier."""
|
|
100
132
|
return self._format_id
|
|
101
133
|
|
|
102
134
|
@property
|
|
103
135
|
def type(self) -> pa.DataType:
|
|
136
|
+
"""The corresponding Arrow data type."""
|
|
104
137
|
return self._type
|
|
105
138
|
|
|
139
|
+
@property
|
|
140
|
+
def pandas_dtype(self) -> pd.api.extensions.ExtensionDtype | None:
|
|
141
|
+
"""The pandas dtype used when loading this column into a dataframe.
|
|
142
|
+
|
|
143
|
+
Integer and boolean columns use pandas nullable dtypes, so nulls do not change the dtype. Returns `None` for
|
|
144
|
+
`float64` and `string` columns, which use the pyarrow default conversion.
|
|
145
|
+
|
|
146
|
+
:raises ImportError: If pandas is not installed.
|
|
147
|
+
"""
|
|
148
|
+
if not _PD_AVAILABLE:
|
|
149
|
+
raise ModuleNotFoundError("No module named 'pandas'", name="pandas")
|
|
150
|
+
if (dtype_name := _PANDAS_DTYPES[self._format_id]) is None:
|
|
151
|
+
return None
|
|
152
|
+
return pd.api.types.pandas_dtype(dtype_name)
|
|
153
|
+
|
|
106
154
|
|
|
107
155
|
class BaseTableFormat:
|
|
108
156
|
"""Base type for comparing table formats"""
|
|
@@ -114,7 +162,7 @@ class BaseTableFormat:
|
|
|
114
162
|
("...") indicates a multidimensional format.
|
|
115
163
|
"""
|
|
116
164
|
self._name = name
|
|
117
|
-
self._columns: list[
|
|
165
|
+
self._columns: list[ColumnFormat] = []
|
|
118
166
|
self._multi_dimensional = False
|
|
119
167
|
for column_type in columns:
|
|
120
168
|
if column_type is Ellipsis:
|
|
@@ -122,7 +170,7 @@ class BaseTableFormat:
|
|
|
122
170
|
elif self._multi_dimensional:
|
|
123
171
|
raise AssertionError("Found new type specification after Ellipsis")
|
|
124
172
|
else:
|
|
125
|
-
self._columns.append(
|
|
173
|
+
self._columns.append(ColumnFormat(format_spec=column_type))
|
|
126
174
|
|
|
127
175
|
if self._multi_dimensional and len(self._columns) != 1:
|
|
128
176
|
raise AssertionError("Multidimensional formats must only have one column type")
|
|
@@ -147,6 +195,11 @@ class BaseTableFormat:
|
|
|
147
195
|
"""The number of columns in this table format"""
|
|
148
196
|
return len(self._columns)
|
|
149
197
|
|
|
198
|
+
@property
|
|
199
|
+
def columns(self) -> tuple[ColumnFormat, ...]:
|
|
200
|
+
"""Column formats in order. A multidimensional format has one entry for its repeated column type."""
|
|
201
|
+
return tuple(self._columns)
|
|
202
|
+
|
|
150
203
|
@property
|
|
151
204
|
def data_type(self) -> str:
|
|
152
205
|
"""The column data type(s) in this table format"""
|
|
@@ -156,15 +209,63 @@ class BaseTableFormat:
|
|
|
156
209
|
class ArrowTableFormat(BaseTableFormat):
|
|
157
210
|
"""Specialised table format type that can be generated from a pyarrow table"""
|
|
158
211
|
|
|
212
|
+
_pa_schema: pa.Schema | None = None
|
|
213
|
+
|
|
214
|
+
@property
|
|
215
|
+
def pa_schema(self) -> pa.Schema:
|
|
216
|
+
"""The Arrow schema supplied to `from_schema` or inferred by `from_pandas`, without any metadata.
|
|
217
|
+
|
|
218
|
+
:raises ValueError: If the format was constructed directly without a schema.
|
|
219
|
+
"""
|
|
220
|
+
if self._pa_schema is None:
|
|
221
|
+
raise ValueError("Create the format with from_schema() or from_pandas() to access its Arrow schema")
|
|
222
|
+
return self._pa_schema
|
|
223
|
+
|
|
159
224
|
@classmethod
|
|
160
225
|
def from_schema(cls, pa_schema: pa.Schema) -> ArrowTableFormat:
|
|
161
226
|
"""Generate an ArrowTableFormat instance that represents the structure of the provided table schema.
|
|
162
227
|
|
|
163
|
-
:param pa_schema: Table schema to generate a format representation for.
|
|
228
|
+
:param pa_schema: Table schema to generate a format representation for. Schema and field metadata are not kept.
|
|
164
229
|
|
|
165
230
|
:return: A table format that can be used to compare against known formats.
|
|
166
231
|
"""
|
|
167
|
-
|
|
232
|
+
table_format = cls(name=f"{cls.__name__}<{'/'.join(pa_schema.names)}>", columns=pa_schema.types)
|
|
233
|
+
table_format._pa_schema = pa.schema([field.remove_metadata() for field in pa_schema])
|
|
234
|
+
return table_format
|
|
235
|
+
|
|
236
|
+
@classmethod
|
|
237
|
+
def from_pandas(cls, dataframe: pd.DataFrame) -> ArrowTableFormat:
|
|
238
|
+
"""Infer a table format from a pandas dataframe using the supported Geoscience Object column types.
|
|
239
|
+
|
|
240
|
+
Large strings are normalized to strings. The inferred schema excludes the dataframe index and pandas metadata.
|
|
241
|
+
|
|
242
|
+
:param dataframe: The dataframe to infer the format from.
|
|
243
|
+
:return: A format whose `pa_schema` can be passed to `pyarrow.Table.from_pandas`.
|
|
244
|
+
:raises TypeError: If a column type is not supported. Use `ObjectDataClient.upload_category_dataframe`
|
|
245
|
+
for categorical columns, which are split into value and lookup tables separately.
|
|
246
|
+
"""
|
|
247
|
+
schema = pa.Schema.from_pandas(dataframe, preserve_index=False)
|
|
248
|
+
fields = [field.with_type(pa.string()) if pa.types.is_large_string(field.type) else field for field in schema]
|
|
249
|
+
return cls.from_schema(pa.schema(fields))
|
|
250
|
+
|
|
251
|
+
def pandas_types_mapper(self) -> Callable[[pa.DataType], pd.api.extensions.ExtensionDtype | None]:
|
|
252
|
+
"""Get a pyarrow `types_mapper` that converts this format's columns to their `ColumnFormat.pandas_dtype`.
|
|
253
|
+
|
|
254
|
+
Pass the result to `table.replace_schema_metadata().to_pandas(types_mapper=...)` to ignore legacy metadata.
|
|
255
|
+
|
|
256
|
+
:raises ImportError: If pandas is not installed.
|
|
257
|
+
"""
|
|
258
|
+
return {column.type: column.pandas_dtype for column in self._columns}.get
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
def table_to_dataframe(table: pa.Table) -> pd.DataFrame:
|
|
262
|
+
"""Convert a table to a pandas dataframe using the Geoscience Object column types, ignoring pandas metadata.
|
|
263
|
+
|
|
264
|
+
:param table: The table to convert. It is expected to have been validated against a known table format.
|
|
265
|
+
"""
|
|
266
|
+
types_mapper = ArrowTableFormat.from_schema(table.schema).pandas_types_mapper()
|
|
267
|
+
# PyArrow parses pandas metadata even with ignore_metadata=True.
|
|
268
|
+
return table.replace_schema_metadata().to_pandas(types_mapper=types_mapper)
|
|
168
269
|
|
|
169
270
|
|
|
170
271
|
class KnownTableFormat(BaseTableFormat):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|