---
sidebar:
  hidden: true
title: roboto.domain.topics.topic_data_service
---
## Module Contents

### TopicDataService

```python
class roboto.domain.topics.topic_data_service.TopicDataService(
    roboto_client: roboto.http.RobotoClient,
    cache_dir: Union[str, pathlib.Path, None] = None,
)
```

`from roboto.domain.topics import TopicDataService`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/topics/topic_data_service.py#L63-L346)

Reader of topic data for the `get_data` and `get_data_as_df` methods of topics, message paths and events.

The SDK's own code uses it; to read topic data, use the `get_data` or `get_data_as_df` methods of [`Topic`](/docs/reference/python-sdk/roboto/domain/topics/topic#roboto.domain.topics.topic.Topic), [`MessagePath`](/docs/reference/python-sdk/roboto/domain/topics/message_path#roboto.domain.topics.message_path.MessagePath), or [`Event`](/docs/reference/python-sdk/roboto/domain/events/event#roboto.domain.events.event.Event) instead.

It fetches the representations that hold each of a topic's message paths, keeps the message paths and representations a read asks for, and reads them with the reader for their storage format, MCAP or Parquet.

**Parameters**

- **roboto_client** (`roboto.http.RobotoClient`): Client to make requests to Roboto with.
- **cache_dir** (`Union[str, pathlib.Path, None]`): Directory to download Parquet-stored topic data to and reuse it from. `None` uses `DEFAULT_CACHE_DIR`.

**Attributes**

- **TopicDataService.DEFAULT_CACHE_DIR** (`ClassVar[pathlib.Path]`): Directory Parquet-stored topic data is downloaded to when no other is given: `~/.cache/roboto/topic-data`.

#### TopicDataService.get_data()

```python
def get_data(
    topic_id: str,
    message_paths_include: Optional[collections.abc.Iterable[str]] = None,
    message_paths_exclude: Optional[collections.abc.Iterable[str]] = None,
    start_time: Optional[roboto.time.Time] = None,
    end_time: Optional[roboto.time.Time] = None,
    cache_dir_override: Union[str, pathlib.Path, None] = None,
    representation_selector: roboto.domain.topics.record.RepresentationSelector = RepresentationSelector.raw(),
) -> collections.abc.Generator[tuple[roboto.domain.topics.topic_reader.Timestamp, dict[str, Any]], None, None]
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/topics/topic_data_service.py#L93-L158)

Yield a topic's messages, optionally limited to some message paths and a time range.

A message path matches a path in `message_paths_include` or `message_paths_exclude` when that path is its [`message_path`](/docs/reference/python-sdk/roboto/domain/topics/record#roboto.domain.topics.record.MessagePathRecord.message_path), its [`source_path`](/docs/reference/python-sdk/roboto/domain/topics/record#roboto.domain.topics.record.MessagePathRecord.source_path), or the path of one of its ancestors. The filters never leave out a message path whose data type is a timestamp.

**Parameters**

- **topic_id** (`str`): ID of the topic to read.
- **message_paths_include** (`Optional[collections.abc.Iterable[str]]`): Message paths to include. `None` includes every message path.
- **message_paths_exclude** (`Optional[collections.abc.Iterable[str]]`): Message paths to leave out. `None` leaves out none.
- **start_time** (`Optional[roboto.time.Time]`): Inclusive start of the time range, in nanoseconds since the Unix epoch or any value [`to_epoch_nanoseconds()`](/docs/reference/python-sdk/roboto/time#roboto.time.to_epoch_nanoseconds) accepts.
- **end_time** (`Optional[roboto.time.Time]`): Exclusive end of the time range, in the same form.
- **cache_dir_override** (`Union[str, pathlib.Path, None]`): Directory to download Parquet-stored data to for this read. `None` uses the directory the service was created with.
- **representation_selector** (`roboto.domain.topics.record.RepresentationSelector`): Which of the topic's stored representations to read. Defaults to the original data, with no transformations.

**Yields**

- `(timestamp, message)` tuples -- the timestamp in nanoseconds since the Unix epoch, and a dictionary that follows the topic's schema.

**Raises**

- `NotImplementedError`: The representations selected for the read are not all stored as MCAP or all as Parquet. It surfaces on the first iteration.

**Returns**

- `collections.abc.Generator[tuple[roboto.domain.topics.topic_reader.Timestamp, dict[str, Any]], None, None]`

#### TopicDataService.get_data_as_df()

```python
def get_data_as_df(
    topic_id: str,
    message_paths_include: Optional[collections.abc.Iterable[str]] = None,
    message_paths_exclude: Optional[collections.abc.Iterable[str]] = None,
    start_time: Optional[roboto.time.Time] = None,
    end_time: Optional[roboto.time.Time] = None,
    cache_dir_override: Union[str, pathlib.Path, None] = None,
    representation_selector: roboto.domain.topics.record.RepresentationSelector = RepresentationSelector.raw(),
) -> pandas.DataFrame
```

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/topics/topic_data_service.py#L160-L234)

Fetch a topic's messages as a pandas DataFrame, optionally limited to some message paths and a time range.

The message paths are filtered as `get_data` describes.

**Parameters**

- **topic_id** (`str`): ID of the topic to read.
- **message_paths_include** (`Optional[collections.abc.Iterable[str]]`): Message paths to include. `None` includes every message path.
- **message_paths_exclude** (`Optional[collections.abc.Iterable[str]]`): Message paths to leave out. `None` leaves out none.
- **start_time** (`Optional[roboto.time.Time]`): Inclusive start of the time range, in nanoseconds since the Unix epoch or any value [`to_epoch_nanoseconds()`](/docs/reference/python-sdk/roboto/time#roboto.time.to_epoch_nanoseconds) accepts.
- **end_time** (`Optional[roboto.time.Time]`): Exclusive end of the time range, in the same form.
- **cache_dir_override** (`Union[str, pathlib.Path, None]`): Directory to download Parquet-stored data to for this read. `None` uses the directory the service was created with.
- **representation_selector** (`roboto.domain.topics.record.RepresentationSelector`): Which of the topic's stored representations to read. Defaults to the original data, with no transformations.

**Returns**

- `pandas.DataFrame`: A data frame with one row per message, indexed by each message's timestamp: a UTC `pandas.DatetimeIndex` named `_index`.

**Raises**

- `ImportError`: The `roboto[analytics]` extra is not installed.
- `NotImplementedError`: The representations selected for the read are not all stored as MCAP or all as Parquet.

### iterable_to_set()

```python
def roboto.domain.topics.topic_data_service.iterable_to_set(
    arg: collections.abc.Iterable[str],
) -> set[str]
```

`from roboto.domain.topics.topic_data_service import iterable_to_set`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/topics/topic_data_service.py#L36-L60)

Return `arg` as a set of strings, treating a single string as one item rather than as its characters.

[`get_data()`](/docs/reference/python-sdk/roboto/domain/topics/topic#roboto.domain.topics.topic.Topic.get_data) and the other methods that read topic data filter by message path through it, so most code never calls it.

**Parameters**

- **arg** (`collections.abc.Iterable[str]`): Strings to convert, or a single string.

**Returns**

- `set[str]`: `arg` itself when it is a set, not a copy; otherwise a new set of its items.

**Raises**

- `TypeError`: `arg` is not iterable.

### logger

```python
roboto.domain.topics.topic_data_service.logger
```

`from roboto.domain.topics.topic_data_service import logger`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/domain/topics/topic_data_service.py#L33-L33)
