Skip to content

Commit 3041b6b

Browse files
jbrockmendelproost
authored andcommitted
CLN: remove legacy datetime support in io.pytables (pandas-dev#29808)
1 parent a0e0466 commit 3041b6b

File tree

1 file changed

+10
-59
lines changed

1 file changed

+10
-59
lines changed

pandas/io/pytables.py

+10-59
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
"""
55

66
import copy
7-
from datetime import date, datetime
7+
from datetime import date
88
import itertools
99
import os
1010
import re
11-
import time
1211
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Type, Union
1312
import warnings
1413

@@ -43,7 +42,6 @@
4342
TimedeltaIndex,
4443
concat,
4544
isna,
46-
to_datetime,
4745
)
4846
from pandas.core.arrays.categorical import Categorical
4947
from pandas.core.arrays.sparse import BlockIndex, IntIndex
@@ -2137,6 +2135,7 @@ def set_kind(self):
21372135
elif dtype.startswith("int") or dtype.startswith("uint"):
21382136
self.kind = "integer"
21392137
elif dtype.startswith("date"):
2138+
# in tests this is always "datetime64"
21402139
self.kind = "datetime"
21412140
elif dtype.startswith("timedelta"):
21422141
self.kind = "timedelta"
@@ -2182,8 +2181,8 @@ def set_atom(
21822181
if inferred_type == "date":
21832182
raise TypeError("[date] is not implemented as a table column")
21842183
elif inferred_type == "datetime":
2185-
# after 8260
2186-
# this only would be hit for a mutli-timezone dtype
2184+
# after GH#8260
2185+
# this only would be hit for a multi-timezone dtype
21872186
# which is an error
21882187

21892188
raise TypeError(
@@ -2406,10 +2405,6 @@ def convert(self, values, nan_rep, encoding, errors, start=None, stop=None):
24062405
self.data = np.asarray(
24072406
[date.fromtimestamp(v) for v in self.data], dtype=object
24082407
)
2409-
elif dtype == "datetime":
2410-
self.data = np.asarray(
2411-
[datetime.fromtimestamp(v) for v in self.data], dtype=object
2412-
)
24132408

24142409
elif meta == "category":
24152410

@@ -2920,7 +2915,7 @@ def read_index_node(
29202915
# created by python3
29212916
kwargs["tz"] = node._v_attrs["tz"]
29222917

2923-
if kind in ("date", "datetime"):
2918+
if kind == "date":
29242919
index = factory(
29252920
_unconvert_index(
29262921
data, kind, encoding=self.encoding, errors=self.errors
@@ -4619,39 +4614,12 @@ def _convert_index(name: str, index, encoding=None, errors="strict", format_type
46194614
raise TypeError("MultiIndex not supported here!")
46204615

46214616
inferred_type = lib.infer_dtype(index, skipna=False)
4617+
# we wont get inferred_type of "datetime64" or "timedelta64" as these
4618+
# would go through the DatetimeIndex/TimedeltaIndex paths above
46224619

46234620
values = np.asarray(index)
46244621

4625-
if inferred_type == "datetime64":
4626-
converted = values.view("i8")
4627-
return IndexCol(
4628-
name,
4629-
converted,
4630-
"datetime64",
4631-
_tables().Int64Col(),
4632-
freq=getattr(index, "freq", None),
4633-
tz=getattr(index, "tz", None),
4634-
index_name=index_name,
4635-
)
4636-
elif inferred_type == "timedelta64":
4637-
converted = values.view("i8")
4638-
return IndexCol(
4639-
name,
4640-
converted,
4641-
"timedelta64",
4642-
_tables().Int64Col(),
4643-
freq=getattr(index, "freq", None),
4644-
index_name=index_name,
4645-
)
4646-
elif inferred_type == "datetime":
4647-
converted = np.asarray(
4648-
[(time.mktime(v.timetuple()) + v.microsecond / 1e6) for v in values],
4649-
dtype=np.float64,
4650-
)
4651-
return IndexCol(
4652-
name, converted, "datetime", _tables().Time64Col(), index_name=index_name
4653-
)
4654-
elif inferred_type == "date":
4622+
if inferred_type == "date":
46554623
converted = np.asarray([v.toordinal() for v in values], dtype=np.int32)
46564624
return IndexCol(
46574625
name, converted, "date", _tables().Time32Col(), index_name=index_name,
@@ -4670,19 +4638,6 @@ def _convert_index(name: str, index, encoding=None, errors="strict", format_type
46704638
itemsize=itemsize,
46714639
index_name=index_name,
46724640
)
4673-
elif inferred_type == "unicode":
4674-
if format_type == "fixed":
4675-
atom = _tables().ObjectAtom()
4676-
return IndexCol(
4677-
name,
4678-
np.asarray(values, dtype="O"),
4679-
"object",
4680-
atom,
4681-
index_name=index_name,
4682-
)
4683-
raise TypeError(
4684-
f"[unicode] is not supported as a in index type for [{format_type}] formats"
4685-
)
46864641

46874642
elif inferred_type == "integer":
46884643
# take a guess for now, hope the values fit
@@ -4703,7 +4658,7 @@ def _convert_index(name: str, index, encoding=None, errors="strict", format_type
47034658
atom,
47044659
index_name=index_name,
47054660
)
4706-
else: # pragma: no cover
4661+
else:
47074662
atom = _tables().ObjectAtom()
47084663
return IndexCol(
47094664
name, np.asarray(values, dtype="O"), "object", atom, index_name=index_name,
@@ -4716,8 +4671,6 @@ def _unconvert_index(data, kind, encoding=None, errors="strict"):
47164671
index = DatetimeIndex(data)
47174672
elif kind == "timedelta64":
47184673
index = TimedeltaIndex(data)
4719-
elif kind == "datetime":
4720-
index = np.asarray([datetime.fromtimestamp(v) for v in data], dtype=object)
47214674
elif kind == "date":
47224675
try:
47234676
index = np.asarray([date.fromordinal(v) for v in data], dtype=object)
@@ -4819,16 +4772,14 @@ def _maybe_convert(values: np.ndarray, val_kind, encoding, errors):
48194772
def _get_converter(kind: str, encoding, errors):
48204773
if kind == "datetime64":
48214774
return lambda x: np.asarray(x, dtype="M8[ns]")
4822-
elif kind == "datetime":
4823-
return lambda x: to_datetime(x, cache=True).to_pydatetime()
48244775
elif kind == "string":
48254776
return lambda x: _unconvert_string_array(x, encoding=encoding, errors=errors)
48264777
else: # pragma: no cover
48274778
raise ValueError(f"invalid kind {kind}")
48284779

48294780

48304781
def _need_convert(kind) -> bool:
4831-
if kind in ("datetime", "datetime64", "string"):
4782+
if kind in ("datetime64", "string"):
48324783
return True
48334784
return False
48344785

0 commit comments

Comments
 (0)