|
4 | 4 | """
|
5 | 5 |
|
6 | 6 | import copy
|
7 |
| -from datetime import date |
| 7 | +from datetime import date, tzinfo |
8 | 8 | import itertools
|
9 | 9 | import os
|
10 | 10 | import re
|
@@ -2918,7 +2918,8 @@ def read_array(
|
2918 | 2918 | if dtype == "datetime64":
|
2919 | 2919 |
|
2920 | 2920 | # 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) |
2922 | 2923 |
|
2923 | 2924 | elif dtype == "timedelta64":
|
2924 | 2925 | ret = np.asarray(ret, dtype="m8[ns]")
|
@@ -4164,7 +4165,7 @@ def read_column(
|
4164 | 4165 | encoding=self.encoding,
|
4165 | 4166 | errors=self.errors,
|
4166 | 4167 | )
|
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) |
4168 | 4169 |
|
4169 | 4170 | raise KeyError(f"column [{column}] not found in the table")
|
4170 | 4171 |
|
@@ -4693,37 +4694,39 @@ def _get_info(info, name):
|
4693 | 4694 | # tz to/from coercion
|
4694 | 4695 |
|
4695 | 4696 |
|
4696 |
| -def _get_tz(tz): |
| 4697 | +def _get_tz(tz: tzinfo) -> Union[str, tzinfo]: |
4697 | 4698 | """ for a tz-aware type, return an encoded zone """
|
4698 | 4699 | zone = timezones.get_timezone(tz)
|
4699 |
| - if zone is None: |
4700 |
| - zone = tz.utcoffset().total_seconds() |
4701 | 4700 | return zone
|
4702 | 4701 |
|
4703 | 4702 |
|
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]: |
4705 | 4708 | """
|
4706 | 4709 | coerce the values to a DatetimeIndex if tz is set
|
4707 | 4710 | preserve the input shape if possible
|
4708 | 4711 |
|
4709 | 4712 | Parameters
|
4710 | 4713 | ----------
|
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 |
4715 | 4716 | coerce : if we do not have a passed timezone, coerce to M8[ns] ndarray
|
4716 | 4717 | """
|
| 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 | + |
4717 | 4724 | if tz is not None:
|
4718 | 4725 | name = getattr(values, "name", None)
|
4719 | 4726 | values = values.ravel()
|
4720 | 4727 | tz = timezones.get_timezone(_ensure_decoded(tz))
|
4721 | 4728 | 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) |
4727 | 4730 | elif coerce:
|
4728 | 4731 | values = np.asarray(values, dtype="M8[ns]")
|
4729 | 4732 |
|
|
0 commit comments