Skip to content

Commit dd5b0d4

Browse files
author
Zach Banks
committed
MOD: With pretty_px, replace null prices with NaN
1 parent 93da15b commit dd5b0d4

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

databento/common/dbnstore.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
Metadata,
4949
]
5050

51+
INT64_NULL = 9223372036854775807
52+
NAN = float("NaN")
53+
5154

5255
logger = logging.getLogger(__name__)
5356

@@ -420,11 +423,11 @@ def _apply_pretty_px(self, df: pd.DataFrame) -> pd.DataFrame:
420423
or column.startswith("bid_px") # MBP
421424
or column.startswith("ask_px") # MBP
422425
):
423-
df[column] = df[column] * 1e-9
426+
df[column] = df[column].replace(INT64_NULL, NAN) * 1e-9
424427

425428
if self.schema == Schema.DEFINITION:
426429
for column in DEFINITION_PRICE_COLUMNS:
427-
df[column] = df[column] * 1e-9
430+
df[column] = df[column].replace(INT64_NULL, NAN) * 1e-9
428431

429432
return df
430433

@@ -882,7 +885,8 @@ def to_csv(
882885
`int` to `pd.Timestamp` tz-aware (UTC).
883886
pretty_px : bool, default True
884887
If all price columns should be converted from `int` to `float` at
885-
the correct scale (using the fixed precision scalar 1e-9).
888+
the correct scale (using the fixed precision scalar 1e-9). Null
889+
prices are replaced with an empty string.
886890
map_symbols : bool, default True
887891
If symbology mappings from the metadata should be used to create
888892
a 'symbol' column, mapping the instrument ID to its native symbol for
@@ -925,7 +929,8 @@ def to_df(
925929
`int` to `pd.Timestamp` tz-aware (UTC).
926930
pretty_px : bool, default True
927931
If all price columns should be converted from `int` to `float` at
928-
the correct scale (using the fixed precision scalar 1e-9).
932+
the correct scale (using the fixed precision scalar 1e-9). Null
933+
prices are replaced with NaN.
929934
map_symbols : bool, default True
930935
If symbology mappings from the metadata should be used to create
931936
a 'symbol' column, mapping the instrument ID to its native symbol for

tests/test_historical_bento.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,21 @@ def test_to_df_with_pretty_px_with_various_schemas_converts_prices_as_expected(
426426
assert isinstance(df[column].iloc(0)[1], float)
427427
# TODO(cs): Check float values once display factor fixed
428428

429+
def test_to_df_with_pretty_px_handles_null(
430+
test_data: Callable[[Schema], bytes],
431+
) -> None:
432+
# Arrange
433+
stub_data = test_data(Schema.DEFINITION)
434+
data = DBNStore.from_bytes(data=stub_data)
435+
436+
# Act
437+
df_plain = data.to_df(pretty_px=False)
438+
df_pretty = data.to_df(pretty_px=True)
439+
440+
# Assert
441+
assert all(df_plain["strike_price"] == 9223372036854775807)
442+
assert all(np.isnan(df_pretty["strike_price"]))
443+
429444

430445
@pytest.mark.parametrize(
431446
"expected_schema",

0 commit comments

Comments
 (0)