From b306cb8de2dc1e7dd330063f02c252b7376d8370 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Tue, 3 Dec 2019 14:54:23 -0800 Subject: [PATCH 1/2] CLN: pytables _set_tz/_get_tz --- pandas/io/pytables.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index a48d9abc3c13b..2ce521648234e 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -4,7 +4,7 @@ """ import copy -from datetime import date +from datetime import date, tzinfo import itertools import os import re @@ -2858,7 +2858,8 @@ def read_array( if dtype == "datetime64": # reconstruct a timezone if indicated - ret = _set_tz(ret, getattr(attrs, "tz", None), coerce=True) + tz = getattr(attrs, "tz", None) + ret = _set_tz(ret, tz, coerce=True) elif dtype == "timedelta64": ret = np.asarray(ret, dtype="m8[ns]") @@ -4105,7 +4106,7 @@ def read_column( encoding=self.encoding, errors=self.errors, ) - return Series(_set_tz(a.take_data(), a.tz, True), name=column) + return Series(_set_tz(a.take_data(), a.tz), name=column) raise KeyError(f"column [{column}] not found in the table") @@ -4651,37 +4652,39 @@ def _get_info(info, name): # tz to/from coercion -def _get_tz(tz): +def _get_tz(tz: tzinfo) -> Union[str, tzinfo]: """ for a tz-aware type, return an encoded zone """ zone = timezones.get_timezone(tz) - if zone is None: - zone = tz.utcoffset().total_seconds() return zone -def _set_tz(values, tz, preserve_UTC: bool = False, coerce: bool = False): +def _set_tz( + values: Union[np.ndarray, Index], + tz: Optional[Union[str, tzinfo]], + coerce: bool = False, +): """ coerce the values to a DatetimeIndex if tz is set preserve the input shape if possible Parameters ---------- - values : ndarray - tz : string/pickled tz object - preserve_UTC : bool, - preserve the UTC of the result + values : ndarray or Index + tz : str or tzinfo coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray """ + if isinstance(values, DatetimeIndex): + # If we values is tzaware, the tz gets dropped in the values.ravel() + # call below (which returns an ndarray). So we are only non-lossy + # if `tz` matches `values.tz`. + assert values.tz is None or values.tz == tz + if tz is not None: name = getattr(values, "name", None) values = values.ravel() tz = timezones.get_timezone(_ensure_decoded(tz)) values = DatetimeIndex(values, name=name) - if values.tz is None: - values = values.tz_localize("UTC").tz_convert(tz) - if preserve_UTC: - if tz == "UTC": - values = list(values) + values = values.tz_localize("UTC").tz_convert(tz) elif coerce: values = np.asarray(values, dtype="M8[ns]") From a4ca8589e89ed32d90acbb7c6c2d288df725fb04 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Wed, 4 Dec 2019 09:14:27 -0800 Subject: [PATCH 2/2] requested edits --- pandas/io/pytables.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/pytables.py b/pandas/io/pytables.py index 242f5fbdf5059..f948cebcb28d8 100644 --- a/pandas/io/pytables.py +++ b/pandas/io/pytables.py @@ -4698,7 +4698,7 @@ def _set_tz( values: Union[np.ndarray, Index], tz: Optional[Union[str, tzinfo]], coerce: bool = False, -): +) -> Union[np.ndarray, DatetimeIndex]: """ coerce the values to a DatetimeIndex if tz is set preserve the input shape if possible @@ -4710,7 +4710,7 @@ def _set_tz( coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray """ if isinstance(values, DatetimeIndex): - # If we values is tzaware, the tz gets dropped in the values.ravel() + # If values is tzaware, the tz gets dropped in the values.ravel() # call below (which returns an ndarray). So we are only non-lossy # if `tz` matches `values.tz`. assert values.tz is None or values.tz == tz