-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: pytables _set_tz/_get_tz #30018
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
Changes from 2 commits
b306cb8
efa81b5
6679fa2
a4ca858
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||
# 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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i wonder if we need the ravel any longer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i havent checked if we ever actually pass 2D ndarray, but the ravel is definitely needed in the case where we have a DTI (or we would have to replace it with DTI-specific code) (see the comment a few lines up) |
||
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]") | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the return easy to annotate here? DTI or Ndarray?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, will do