Skip to content

ENH: add flag to get_table to control tz-awareness #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions databroker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ def get_events(self, headers, fields=None, stream_name=ALL, fill=False,
def get_table(self, headers, fields=None, stream_name='primary',
fill=False,
convert_times=True, timezone=None, handler_registry=None,
handler_overrides=None, localize_times=True):
handler_overrides=None, localize_times=True,
tz_aware_times=False):
"""
Make a table (pandas.DataFrame) from given run(s).

Expand Down Expand Up @@ -566,10 +567,14 @@ def get_table(self, headers, fields=None, stream_name='primary',

however, this makes the dataframe repr look nicer

This implies convert_times.
Ignored if `convert_times` is False

Defaults to True to preserve back-compatibility.

tz_aware_times : bool, optional
If the 'time' column should be naive or TZ aware, Default to False
(which returns naive values). Ignored if `convert_times` is False

Returns
-------
table : pandas.DataFrame
Expand All @@ -581,7 +586,8 @@ def get_table(self, headers, fields=None, stream_name='primary',
convert_times=convert_times,
timezone=timezone, handler_registry=handler_registry,
handler_overrides=handler_overrides,
localize_times=localize_times)
localize_times=localize_times,
tz_aware_times=tz_aware_times)
return res

def get_images(self, headers, name, handler_registry=None,
Expand Down
35 changes: 19 additions & 16 deletions databroker/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ def get_events(mds, fs, headers, fields=None, stream_name=ALL, fill=False,

def get_table(mds, fs, headers, fields=None, stream_name='primary', fill=False,
convert_times=True, timezone=None, handler_registry=None,
handler_overrides=None, localize_times=True):
handler_overrides=None, localize_times=True,
tz_aware_times=False):
"""
Make a table (pandas.DataFrame) from given run(s).

Expand All @@ -249,8 +250,9 @@ def get_table(mds, fs, headers, fields=None, stream_name='primary', fill=False,
Whether externally-stored data should be filled in. Defaults to False.
convert_times : bool, optional
Whether to convert times from float (seconds since 1970) to
numpy datetime64, using pandas. True by default, returns naive
datetime64 objects in UTC
numpy datetime64, using pandas. True by default.

If False, return naive floats in UTC
timezone : str, optional
e.g., 'US/Eastern'
handler_registry : dict, optional
Expand All @@ -269,9 +271,12 @@ def get_table(mds, fs, headers, fields=None, stream_name='primary', fill=False,

however, this makes the dataframe repr look nicer

This implies convert_times.
Ignored if `convert_times` is False

Defaults to True to preserve back-compatibility.
tz_aware_times : bool, optional
If the 'time' column should be naive or TZ aware, Default to False (
which returns naive values). Ignored if `convert_times` is False
Returns
-------
table : pandas.DataFrame
Expand Down Expand Up @@ -327,18 +332,16 @@ def get_table(mds, fs, headers, fields=None, stream_name='primary', fill=False,
descriptor, data, seq_nums, times, uids, timestamps = payload
df = pd.DataFrame(index=seq_nums)
# if converting to datetime64 (in utc or 'local' tz)
if convert_times or localize_times:
times = pd.to_datetime(times, unit='s')
# make sure this is a series
times = pd.Series(times, index=seq_nums)

# if localizing to 'local' time
if localize_times:
times = (times
.dt.tz_localize('UTC') # first make tz aware
.dt.tz_convert(timezone) # convert to 'local'
.dt.tz_localize(None) # make naive again
)
if convert_times:
times = pd.Series(pd.to_datetime(times, unit='s'),
index=seq_nums)
times = times.dt.tz_localize('UTC') # make tz aware
if localize_times:
times = times.dt.tz_convert(timezone) # convert to 'local'
if not tz_aware_times:
times = times.dt.tz_localize(None) # make naive again
else:
times = pd.Series(times, index=seq_nums)

df['time'] = times
for field, values in six.iteritems(data):
Expand Down