Skip to content

Commit 500a533

Browse files
jbrockmendelproost
authored andcommitted
CLN: pytables _set_tz/_get_tz (pandas-dev#30018)
1 parent ca28ae0 commit 500a533

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

pandas/io/pytables.py

+19-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import copy
7-
from datetime import date
7+
from datetime import date, tzinfo
88
import itertools
99
import os
1010
import re
@@ -2918,7 +2918,8 @@ def read_array(
29182918
if dtype == "datetime64":
29192919

29202920
# reconstruct a timezone if indicated
2921-
ret = _set_tz(ret, getattr(attrs, "tz", None), coerce=True)
2921+
tz = getattr(attrs, "tz", None)
2922+
ret = _set_tz(ret, tz, coerce=True)
29222923

29232924
elif dtype == "timedelta64":
29242925
ret = np.asarray(ret, dtype="m8[ns]")
@@ -4164,7 +4165,7 @@ def read_column(
41644165
encoding=self.encoding,
41654166
errors=self.errors,
41664167
)
4167-
return Series(_set_tz(a.take_data(), a.tz, True), name=column)
4168+
return Series(_set_tz(a.take_data(), a.tz), name=column)
41684169

41694170
raise KeyError(f"column [{column}] not found in the table")
41704171

@@ -4693,37 +4694,39 @@ def _get_info(info, name):
46934694
# tz to/from coercion
46944695

46954696

4696-
def _get_tz(tz):
4697+
def _get_tz(tz: tzinfo) -> Union[str, tzinfo]:
46974698
""" for a tz-aware type, return an encoded zone """
46984699
zone = timezones.get_timezone(tz)
4699-
if zone is None:
4700-
zone = tz.utcoffset().total_seconds()
47014700
return zone
47024701

47034702

4704-
def _set_tz(values, tz, preserve_UTC: bool = False, coerce: bool = False):
4703+
def _set_tz(
4704+
values: Union[np.ndarray, Index],
4705+
tz: Optional[Union[str, tzinfo]],
4706+
coerce: bool = False,
4707+
) -> Union[np.ndarray, DatetimeIndex]:
47054708
"""
47064709
coerce the values to a DatetimeIndex if tz is set
47074710
preserve the input shape if possible
47084711
47094712
Parameters
47104713
----------
4711-
values : ndarray
4712-
tz : string/pickled tz object
4713-
preserve_UTC : bool,
4714-
preserve the UTC of the result
4714+
values : ndarray or Index
4715+
tz : str or tzinfo
47154716
coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray
47164717
"""
4718+
if isinstance(values, DatetimeIndex):
4719+
# If values is tzaware, the tz gets dropped in the values.ravel()
4720+
# call below (which returns an ndarray). So we are only non-lossy
4721+
# if `tz` matches `values.tz`.
4722+
assert values.tz is None or values.tz == tz
4723+
47174724
if tz is not None:
47184725
name = getattr(values, "name", None)
47194726
values = values.ravel()
47204727
tz = timezones.get_timezone(_ensure_decoded(tz))
47214728
values = DatetimeIndex(values, name=name)
4722-
if values.tz is None:
4723-
values = values.tz_localize("UTC").tz_convert(tz)
4724-
if preserve_UTC:
4725-
if tz == "UTC":
4726-
values = list(values)
4729+
values = values.tz_localize("UTC").tz_convert(tz)
47274730
elif coerce:
47284731
values = np.asarray(values, dtype="M8[ns]")
47294732

0 commit comments

Comments
 (0)