Skip to content

Commit afd6526

Browse files
committed
datetime64 dtype parsing using numpy api
1 parent b842245 commit afd6526

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

pandas/io/pytables.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,7 +2655,7 @@ def convert(self, values: np.ndarray, nan_rep, encoding: str, errors: str):
26552655
# reverse converts
26562656
if dtype.startswith("datetime64"):
26572657
# recreate with tz if indicated
2658-
converted = _set_tz(converted, tz)
2658+
converted = _set_tz(converted, tz, dtype)
26592659

26602660
elif dtype == "timedelta64":
26612661
converted = np.asarray(converted, dtype="m8[ns]")
@@ -3036,8 +3036,7 @@ def read_array(self, key: str, start: int | None = None, stop: int | None = None
30363036
if dtype and dtype.startswith("datetime64"):
30373037
# reconstruct a timezone if indicated
30383038
tz = getattr(attrs, "tz", None)
3039-
# set time zone with parsed datetime64 unit
3040-
ret = _set_tz(ret, tz, unit=dtype.split("[")[1].strip("]"))
3039+
ret = _set_tz(ret, tz, dtype)
30413040

30423041
elif dtype == "timedelta64":
30433042
ret = np.asarray(ret, dtype="m8[ns]")
@@ -4966,7 +4965,7 @@ def _get_tz(tz: tzinfo) -> str | tzinfo:
49664965

49674966

49684967
def _set_tz(
4969-
values: npt.NDArray[np.int64], tz: str | tzinfo | None, unit: str = "ns"
4968+
values: npt.NDArray[np.int64], tz: str | tzinfo | None, datetime64_dtype: str
49704969
) -> DatetimeArray:
49714970
"""
49724971
Coerce the values to a DatetimeArray with appropriate tz.
@@ -4975,11 +4974,12 @@ def _set_tz(
49754974
----------
49764975
values : ndarray[int64]
49774976
tz : str, tzinfo, or None
4978-
unit : str. The default unit is ns. Needs to be specified otherwise.
4977+
datetime64_dtype : str, e.g. "datetime64[ns]", "datetime64[25s]"
49794978
"""
49804979
assert values.dtype == "i8", values.dtype
49814980
# Argument "tz" to "tz_to_dtype" has incompatible type "str | tzinfo | None";
49824981
# expected "tzinfo"
4982+
unit, _ = np.datetime_data(datetime64_dtype) # parsing dtype: unit, count
49834983
dtype = tz_to_dtype(tz=tz, unit=unit) # type: ignore[arg-type]
49844984
dta = DatetimeArray._from_sequence(values, dtype=dtype)
49854985
return dta

0 commit comments

Comments
 (0)