Skip to content

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

Merged
merged 4 commits into from
Dec 4, 2019
Merged
Changes from 2 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
35 changes: 19 additions & 16 deletions pandas/io/pytables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

import copy
from datetime import date
from datetime import date, tzinfo
import itertools
import os
import re
Expand Down Expand Up @@ -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]")
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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,
):
Copy link
Member

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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will do

"""
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is we a typo?

Copy link
Member Author

Choose a reason for hiding this comment

The 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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if we need the ravel any longer

Copy link
Member Author

Choose a reason for hiding this comment

The 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]")

Expand Down