Skip to content

Commit bae24dc

Browse files
committed
FIX: Fix client map_symbols for stype_out
1 parent dedda92 commit bae24dc

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
- Added `map_symbols` option for `batch.submit_job(...)`, default `False`. When `True` this appends a requested symbol field to each record (available for CSV and JSON text encodings)
88

99
#### Bug fixes
10-
- Fixed an issue where no disconnection exception were raised when iterating the `Live` client.
10+
- Fixed an issue where no disconnection exception were raised when iterating the `Live` client
11+
- Fixed an issue where calling `DBNStore.to_df`, `DBNStore.to_json`, or `DBNStore.to_csv` with `map_symbols=True` would cause a `TypeError`
1112

1213
#### Breaking changes
1314
- Removed `default_value` parameter from `Historical.symbology.resolve`

databento/common/dbnstore.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ def to_csv(
828828
path: Path | str,
829829
pretty_px: bool = True,
830830
pretty_ts: bool = True,
831-
map_symbols: bool = True,
831+
map_symbols: bool | None = None,
832832
schema: Schema | str | None = None,
833833
) -> None:
834834
"""
@@ -883,7 +883,7 @@ def to_df(
883883
self,
884884
pretty_px: bool = ...,
885885
pretty_ts: bool = ...,
886-
map_symbols: bool = ...,
886+
map_symbols: bool | None = ...,
887887
schema: Schema | str | None = ...,
888888
count: None = ...,
889889
) -> pd.DataFrame:
@@ -894,7 +894,7 @@ def to_df(
894894
self,
895895
pretty_px: bool = ...,
896896
pretty_ts: bool = ...,
897-
map_symbols: bool = ...,
897+
map_symbols: bool | None = ...,
898898
schema: Schema | str | None = ...,
899899
count: int = ...,
900900
) -> DataFrameIterator:
@@ -904,7 +904,7 @@ def to_df(
904904
self,
905905
pretty_px: bool = True,
906906
pretty_ts: bool = True,
907-
map_symbols: bool = True,
907+
map_symbols: bool | None = None,
908908
schema: Schema | str | None = None,
909909
count: int | None = None,
910910
) -> pd.DataFrame | DataFrameIterator:
@@ -951,8 +951,16 @@ def to_df(
951951
raise ValueError("a schema must be specified for mixed DBN data")
952952
schema = self.schema
953953

954-
if not self._instrument_id_index:
955-
self._instrument_id_index = self._build_instrument_id_index()
954+
if map_symbols is None:
955+
map_symbols = self.stype_out == SType.INSTRUMENT_ID
956+
957+
if map_symbols:
958+
if self.stype_out != SType.INSTRUMENT_ID:
959+
raise ValueError(
960+
"`map_symbols` is not supported when `stype_out` is not 'instrument_id'",
961+
)
962+
if not self._instrument_id_index:
963+
self._instrument_id_index = self._build_instrument_id_index()
956964

957965
if count is None:
958966
records = iter([self.to_ndarray(schema)])
@@ -1002,7 +1010,7 @@ def to_json(
10021010
path: Path | str,
10031011
pretty_px: bool = True,
10041012
pretty_ts: bool = True,
1005-
map_symbols: bool = True,
1013+
map_symbols: bool | None = None,
10061014
schema: Schema | str | None = None,
10071015
) -> None:
10081016
"""

tests/test_historical_bento.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from io import BytesIO
66
from pathlib import Path
77
from typing import Any, Callable
8+
from unittest.mock import MagicMock
89

910
import databento
1011
import numpy as np
@@ -1140,3 +1141,50 @@ def test_dbnstore_to_df_with_count_empty(
11401141

11411142
# Assert
11421143
assert next(df_iter).empty
1144+
1145+
1146+
def test_dbnstore_to_df_cannot_map_symbols(
1147+
test_data: Callable[[Schema], bytes],
1148+
monkeypatch: pytest.MonkeyPatch,
1149+
) -> None:
1150+
"""
1151+
Test that calling to_df with map_symbols=True on a DBNStore with a
1152+
stype_out other than 'instrument_id' raises a ValueError.
1153+
"""
1154+
# Arrange
1155+
dbn_stub_data = (
1156+
zstandard.ZstdDecompressor().stream_reader(test_data(Schema.TRADES)).read()
1157+
)
1158+
1159+
# Act
1160+
dbnstore = DBNStore.from_bytes(data=dbn_stub_data)
1161+
monkeypatch.setattr(DBNStore, "stype_out", MagicMock(return_type=SType.RAW_SYMBOL))
1162+
1163+
# Assert
1164+
with pytest.raises(ValueError):
1165+
dbnstore.to_df(
1166+
map_symbols=True,
1167+
)
1168+
1169+
def test_dbnstore_to_df_cannot_map_symbols_default_to_false(
1170+
test_data: Callable[[Schema], bytes],
1171+
monkeypatch: pytest.MonkeyPatch,
1172+
) -> None:
1173+
"""
1174+
Test that calling to_df with on a DBNStore with a stype_out other than
1175+
'instrument_id' won't raise a ValueError if `map_symbols` is not explicitly
1176+
set.
1177+
"""
1178+
# Arrange
1179+
dbn_stub_data = (
1180+
zstandard.ZstdDecompressor().stream_reader(test_data(Schema.TRADES)).read()
1181+
)
1182+
1183+
# Act
1184+
dbnstore = DBNStore.from_bytes(data=dbn_stub_data)
1185+
monkeypatch.setattr(DBNStore, "stype_out", MagicMock(return_type=SType.RAW_SYMBOL))
1186+
1187+
df_iter = dbnstore.to_df()
1188+
1189+
# Assert
1190+
assert len(df_iter) == 4

0 commit comments

Comments
 (0)