---
sidebar:
  hidden: true
title: roboto.formats.parquet.parquet_parser
---
## Module Contents

### ParquetParser

```python
class roboto.formats.parquet.parquet_parser.ParquetParser(
    source: pathlib.Path,
    min_required_row_group_size: int = 100000,
    small_row_group_count_threshold: int = 32,
)
```

`from roboto.formats.parquet import ParquetParser`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L35-L377)

Reader of a local Parquet file's schema, row groups and timestamp column.

[`create_from_df()`](/docs/reference/python-sdk/roboto/domain/topics/topic#roboto.domain.topics.topic.Topic.create_from_df) finds a new topic's time range through it, and its message paths through [`generate_message_path_requests()`](/docs/reference/python-sdk/roboto/formats/parquet/arrow_to_roboto#roboto.formats.parquet.arrow_to_roboto.generate_message_path_requests), so most code never calls it. It can also tell whether the file's row groups let Roboto read a time range of it efficiently, and write a copy that does.

**Parameters**

- **source** (`pathlib.Path`): Path of the local Parquet file to read.
- **min_required_row_group_size** (`int`): Number of rows below which the file's average row group counts as small, for `requires_rewrite`.
- **small_row_group_count_threshold** (`int`): Number of row groups above which a file of small row groups needs rewriting, for `requires_rewrite`.

**Raises**

- `ImportError`: The `roboto[ingestion]` extra is not installed.

**Properties**

- **ParquetParser.column_count** (`int`): Number of leaf columns the file stores, in which each field of a struct counts as its own column.

- **ParquetParser.fields** (`Generator[pyarrow.Field, None, None]`): Top-level fields of the file's schema, in schema order.

- **ParquetParser.row_count** (`int`): Number of rows in the file.

- **ParquetParser.row_group_count** (`int`): Number of row groups in the file.

- **ParquetParser.row_group_size** (`int`): Average number of rows per row group, rounded to the nearest whole number.

  The final row group is left out of the average, since it holds whatever rows remain and is usually smaller. For a file with one row group, the value is that group's row count; for a file with none, `0`.

#### ParquetParser.extract_timestamp_info()

```python
def extract_timestamp_info(
    timestamp_column_name: Optional[str] = None,
    timestamp_unit: Optional[Union[str, roboto.time.TimeUnit]] = None,
) -> roboto.formats.parquet.timestamp.TimestampInfo
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L154-L232)

Read the file's timestamp column, its unit, and the earliest and latest timestamps in it.

Reads the whole timestamp column to find its minimum and maximum. A timezone-aware timestamp column yields `datetime.datetime` values; a numeric column yields numbers in `timestamp_unit`. When pandas is not installed and either value has a fraction of a microsecond, both are integers in nanoseconds instead. A file with no rows yields `None` for both.

**Parameters**

- **timestamp_column_name** (`Optional[str]`): Name of the top-level column that holds each row's timestamp. `None` uses the first column whose type is a timezone-aware timestamp.
- **timestamp_unit** (`Optional[Union[str, roboto.time.TimeUnit]]`): Unit of the timestamp values, as a [`TimeUnit`](/docs/reference/python-sdk/roboto/time#roboto.time.TimeUnit) or its value, such as `"ms"`. `None` takes the unit from the column's timestamp type. A unit given here wins over the type's, with a logged warning when the two differ.

**Returns**

- `roboto.formats.parquet.timestamp.TimestampInfo`: The timestamp column's field, its unit, and the minimum and maximum of its values.

**Raises**

- [`TimestampFieldNotFoundException`](/docs/reference/python-sdk/roboto/exceptions/ingestion#roboto.exceptions.ingestion.TimestampFieldNotFoundException): `timestamp_column_name` names no top-level column, or it is `None` and no top-level column is a timezone-aware timestamp.
- [`IngestionException`](/docs/reference/python-sdk/roboto/exceptions/ingestion#roboto.exceptions.ingestion.IngestionException): The named column is neither numeric nor a timestamp, is a timestamp without a time zone, or is numeric and no `timestamp_unit` was given.
- `ValueError`: `timestamp_unit` is not a [`TimeUnit`](/docs/reference/python-sdk/roboto/time#roboto.time.TimeUnit) value.

#### ParquetParser.find_timestamp_field_by_type()

```python
def find_timestamp_field_by_type() -> pyarrow.Field
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L127-L152)

Return the first top-level field whose type is a timezone-aware timestamp.

Logs a warning for each timestamp field without a time zone that it passes over, since Roboto cannot use one as the timestamp.

**Raises**

- [`TimestampFieldNotFoundException`](/docs/reference/python-sdk/roboto/exceptions/ingestion#roboto.exceptions.ingestion.TimestampFieldNotFoundException): No top-level field is a timezone-aware timestamp.

**Returns**

- `pyarrow.Field`

#### ParquetParser.get_data_for_column()

```python
def get_data_for_column(column_name: str) -> pyarrow.Table
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L234-L243)

Read one column of every row group into memory.

**Parameters**

- **column_name** (`str`): Name of the column to read.

**Returns**

- `pyarrow.Table`: A table whose only column is the one named.

#### ParquetParser.get_timestamp_field_by_name()

```python
def get_timestamp_field_by_name(column_name: str) -> pyarrow.Field
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L245-L276)

Return the top-level field named `column_name`, after checking that it can hold timestamps.

**Parameters**

- **column_name** (`str`): Name of the top-level column that holds each row's timestamp.

**Raises**

- [`TimestampFieldNotFoundException`](/docs/reference/python-sdk/roboto/exceptions/ingestion#roboto.exceptions.ingestion.TimestampFieldNotFoundException): No top-level column has this name.
- [`IngestionException`](/docs/reference/python-sdk/roboto/exceptions/ingestion#roboto.exceptions.ingestion.IngestionException): The column is neither numeric nor a timestamp, or is a timestamp without a time zone.

**Returns**

- `pyarrow.Field`

#### ParquetParser.is_parquet_file()

```python
@staticmethod
def is_parquet_file(path: pathlib.Path) -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L60-L74)

Return whether the file at `path` begins with the Parquet magic bytes `PAR1`.

A file that cannot be opened or read, such as one that does not exist, counts as Parquet when its name ends in `.parquet`, in any letter case.

**Parameters**

- **path** (`pathlib.Path`): Path of the local file to check.

**Returns**

- `bool`

#### ParquetParser.requires_rewrite()

```python
def requires_rewrite(timestamp: roboto.formats.parquet.timestamp.TimestampInfo) -> bool
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L278-L312)

Return whether the file needs rewriting with `rewrite` before Roboto can read time ranges of it quickly.

Roboto reads a time range of a Parquet file by skipping the row groups whose timestamp statistics fall outside it. A file with rows needs rewriting, with a logged warning, when either holds:

1. It has more row groups than `small_row_group_count_threshold` and they average fewer rows than `min_required_row_group_size`, which makes reading it slow.
2. A row group lacks statistics for the timestamp column, so Roboto cannot skip that row group.

**Parameters**

- **timestamp** (`roboto.formats.parquet.timestamp.TimestampInfo`): The file's timestamp column, from `extract_timestamp_info`.

**Returns**

- `bool`

#### ParquetParser.rewrite()

```python
def rewrite(
    outfile: pathlib.Path,
    timestamp: roboto.formats.parquet.timestamp.TimestampInfo,
    target_row_group_size_bytes: int = 100 * 1000 * 1000,
) -> None
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L314-L366)

Write a copy of the file with large row groups and statistics on the timestamp column.

The copy's row groups are sized to hold as many rows as fit in `target_row_group_size_bytes`, at the largest uncompressed size per row of any row group in this file, and never fewer than 100,000 rows. Only the timestamp column gets statistics, which Roboto uses to skip row groups outside a requested time range.

**Parameters**

- **outfile** (`pathlib.Path`): Path to write the copy to. An existing file there is overwritten.
- **timestamp** (`roboto.formats.parquet.timestamp.TimestampInfo`): The file's timestamp column, from `extract_timestamp_info`.
- **target_row_group_size_bytes** (`int`): Uncompressed size to aim for in each row group, in bytes. Larger row groups read faster, but the rewrite holds one row group in memory at a time. Defaults to 100 MB.

**Returns**

- `None`

### logger

```python
roboto.formats.parquet.parquet_parser.logger
```

`from roboto.formats.parquet.parquet_parser import logger`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/formats/parquet/parquet_parser.py#L32-L32)
