2626import pandas as pd
2727import pyarrow as pa
2828import pyarrow .parquet as pq
29+ import pytz
2930import zstandard
3031from databento_dbn import FIXED_PRICE_SCALE
3132from databento_dbn import Compression
4748from databento .common .error import BentoError
4849from databento .common .symbology import InstrumentMap
4950from databento .common .types import DBNRecord
51+ from databento .common .types import Default
5052from databento .common .validation import validate_enum
5153from databento .common .validation import validate_file_write_path
5254from databento .common .validation import validate_maybe_enum
@@ -830,6 +832,7 @@ def to_df(
830832 pretty_ts : bool = ...,
831833 map_symbols : bool = ...,
832834 schema : Schema | str | None = ...,
835+ tz : pytz .BaseTzInfo | str = ...,
833836 count : None = ...,
834837 ) -> pd .DataFrame :
835838 ...
@@ -841,6 +844,7 @@ def to_df(
841844 pretty_ts : bool = ...,
842845 map_symbols : bool = ...,
843846 schema : Schema | str | None = ...,
847+ tz : pytz .BaseTzInfo | str = ...,
844848 count : int = ...,
845849 ) -> DataFrameIterator :
846850 ...
@@ -851,6 +855,7 @@ def to_df(
851855 pretty_ts : bool = True ,
852856 map_symbols : bool = True ,
853857 schema : Schema | str | None = None ,
858+ tz : pytz .BaseTzInfo | str | Default [pytz .BaseTzInfo ] = Default [pytz .BaseTzInfo ](pytz .UTC ),
854859 count : int | None = None ,
855860 ) -> pd .DataFrame | DataFrameIterator :
856861 """
@@ -865,14 +870,16 @@ def to_df(
865870 If "decimal", prices will be instances of `decimal.Decimal`.
866871 pretty_ts : bool, default True
867872 If all timestamp columns should be converted from UNIX nanosecond
868- `int` to tz-aware UTC `pd.Timestamp`.
873+ `int` to tz-aware `pd.Timestamp`. The timezone can be specified using the `tz` parameter .
869874 map_symbols : bool, default True
870875 If symbology mappings from the metadata should be used to create
871876 a 'symbol' column, mapping the instrument ID to its requested symbol for
872877 every record.
873878 schema : Schema or str, optional
874879 The DBN schema for the dataframe.
875880 This is only required when reading a DBN stream with mixed record types.
881+ tz : pytz.BaseTzInfo or str, default UTC
882+ If `pretty_ts` is `True`, all timestamps will be converted to the specified timezone.
876883 count : int, optional
877884 If set, instead of returning a single `DataFrame` a `DataFrameIterator`
878885 instance will be returned. When iterated, this object will yield
@@ -892,6 +899,14 @@ def to_df(
892899
893900 """
894901 schema = validate_maybe_enum (schema , Schema , "schema" )
902+
903+ if isinstance (tz , Default ):
904+ tz = tz .value # consume default
905+ elif not pretty_ts :
906+ raise ValueError ("A timezone was specified when `pretty_ts` is `False`. Did you mean to set `pretty_ts=True`?" )
907+
908+ if not isinstance (tz , pytz .BaseTzInfo ):
909+ tz = pytz .timezone (tz )
895910 if schema is None :
896911 if self .schema is None :
897912 raise ValueError ("a schema must be specified for mixed DBN data" )
@@ -910,6 +925,7 @@ def to_df(
910925 count = count ,
911926 struct_type = self ._schema_struct_map [schema ],
912927 instrument_map = self ._instrument_map ,
928+ tz = tz ,
913929 price_type = price_type ,
914930 pretty_ts = pretty_ts ,
915931 map_symbols = map_symbols ,
@@ -1334,6 +1350,7 @@ def __init__(
13341350 count : int | None ,
13351351 struct_type : type [DBNRecord ],
13361352 instrument_map : InstrumentMap ,
1353+ tz : pytz .BaseTzInfo ,
13371354 price_type : Literal ["fixed" , "float" , "decimal" ] = "float" ,
13381355 pretty_ts : bool = True ,
13391356 map_symbols : bool = True ,
@@ -1345,6 +1362,7 @@ def __init__(
13451362 self ._pretty_ts = pretty_ts
13461363 self ._map_symbols = map_symbols
13471364 self ._instrument_map = instrument_map
1365+ self ._tz = tz
13481366
13491367 def __iter__ (self ) -> DataFrameIterator :
13501368 return self
@@ -1411,7 +1429,7 @@ def _format_px(
14111429
14121430 def _format_pretty_ts (self , df : pd .DataFrame ) -> None :
14131431 for field in self ._struct_type ._timestamp_fields :
1414- df [field ] = pd .to_datetime (df [field ], utc = True , errors = "coerce" )
1432+ df [field ] = pd .to_datetime (df [field ], utc = True , errors = "coerce" ). dt . tz_convert ( self . _tz )
14151433
14161434 def _format_set_index (self , df : pd .DataFrame ) -> None :
14171435 index_column = self ._struct_type ._ordered_fields [0 ]
0 commit comments