Skip to content

Commit bb9ef02

Browse files
authored
TYP: dt64 unit in pytables tests (#56118)
1 parent d9f70b3 commit bb9ef02

File tree

7 files changed

+62
-65
lines changed

7 files changed

+62
-65
lines changed

pandas/tests/io/pytables/test_put.py

+8-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import datetime
21
import re
32

43
import numpy as np
@@ -15,6 +14,7 @@
1514
Series,
1615
_testing as tm,
1716
concat,
17+
date_range,
1818
)
1919
from pandas.tests.io.pytables.common import (
2020
_maybe_remove,
@@ -279,15 +279,9 @@ def test_store_multiindex(setup_path):
279279
with ensure_clean_store(setup_path) as store:
280280

281281
def make_index(names=None):
282-
return MultiIndex.from_tuples(
283-
[
284-
(datetime.datetime(2013, 12, d), s, t)
285-
for d in range(1, 3)
286-
for s in range(2)
287-
for t in range(3)
288-
],
289-
names=names,
290-
)
282+
dti = date_range("2013-12-01", "2013-12-02")
283+
mi = MultiIndex.from_product([dti, range(2), range(3)], names=names)
284+
return mi
291285

292286
# no names
293287
_maybe_remove(store, "df")
@@ -306,11 +300,11 @@ def make_index(names=None):
306300
tm.assert_frame_equal(store.select("df"), df)
307301

308302
# series
309-
_maybe_remove(store, "s")
310-
s = Series(np.zeros(12), index=make_index(["date", None, None]))
311-
store.append("s", s)
303+
_maybe_remove(store, "ser")
304+
ser = Series(np.zeros(12), index=make_index(["date", None, None]))
305+
store.append("ser", ser)
312306
xp = Series(np.zeros(12), index=make_index(["date", "level_1", "level_2"]))
313-
tm.assert_series_equal(store.select("s"), xp)
307+
tm.assert_series_equal(store.select("ser"), xp)
314308

315309
# dup with column
316310
_maybe_remove(store, "df")

pandas/tests/io/pytables/test_read.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def test_pytables_native_read(datapath):
154154
datapath("io", "data", "legacy_hdf/pytables_native.h5"), mode="r"
155155
) as store:
156156
d2 = store["detector/readout"]
157-
assert isinstance(d2, DataFrame)
157+
assert isinstance(d2, DataFrame)
158158

159159

160160
@pytest.mark.skipif(is_platform_windows(), reason="native2 read fails oddly on windows")
@@ -164,7 +164,7 @@ def test_pytables_native2_read(datapath):
164164
) as store:
165165
str(store)
166166
d1 = store["detector"]
167-
assert isinstance(d1, DataFrame)
167+
assert isinstance(d1, DataFrame)
168168

169169

170170
def test_legacy_table_fixed_format_read_py2(datapath):
@@ -174,28 +174,29 @@ def test_legacy_table_fixed_format_read_py2(datapath):
174174
datapath("io", "data", "legacy_hdf", "legacy_table_fixed_py2.h5"), mode="r"
175175
) as store:
176176
result = store.select("df")
177-
expected = DataFrame(
178-
[[1, 2, 3, "D"]],
179-
columns=["A", "B", "C", "D"],
180-
index=Index(["ABC"], name="INDEX_NAME"),
181-
)
182-
tm.assert_frame_equal(expected, result)
177+
expected = DataFrame(
178+
[[1, 2, 3, "D"]],
179+
columns=["A", "B", "C", "D"],
180+
index=Index(["ABC"], name="INDEX_NAME"),
181+
)
182+
tm.assert_frame_equal(expected, result)
183183

184184

185185
def test_legacy_table_fixed_format_read_datetime_py2(datapath):
186186
# GH 31750
187187
# legacy table with fixed format and datetime64 column written in Python 2
188+
expected = DataFrame(
189+
[[Timestamp("2020-02-06T18:00")]],
190+
columns=["A"],
191+
index=Index(["date"]),
192+
dtype="M8[ns]",
193+
)
188194
with ensure_clean_store(
189195
datapath("io", "data", "legacy_hdf", "legacy_table_fixed_datetime_py2.h5"),
190196
mode="r",
191197
) as store:
192198
result = store.select("df")
193-
expected = DataFrame(
194-
[[Timestamp("2020-02-06T18:00")]],
195-
columns=["A"],
196-
index=Index(["date"]),
197-
)
198-
tm.assert_frame_equal(expected, result)
199+
tm.assert_frame_equal(expected, result)
199200

200201

201202
def test_legacy_table_read_py2(datapath):
@@ -264,7 +265,7 @@ def test_read_hdf_iterator(tmp_path, setup_path):
264265
with closing(iterator.store):
265266
assert isinstance(iterator, TableIterator)
266267
indirect = next(iterator.__iter__())
267-
tm.assert_frame_equal(direct, indirect)
268+
tm.assert_frame_equal(direct, indirect)
268269

269270

270271
def test_read_nokey(tmp_path, setup_path):
@@ -387,7 +388,7 @@ def test_read_py2_hdf_file_in_py3(datapath):
387388
mode="r",
388389
) as store:
389390
result = store["p"]
390-
tm.assert_frame_equal(result, expected)
391+
tm.assert_frame_equal(result, expected)
391392

392393

393394
def test_read_infer_string(tmp_path, setup_path):

pandas/tests/io/pytables/test_retain_attributes.py

+12-21
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import pytest
22

3-
from pandas._libs.tslibs import Timestamp
4-
53
from pandas import (
64
DataFrame,
5+
DatetimeIndex,
76
Series,
87
_testing as tm,
98
date_range,
@@ -18,11 +17,10 @@
1817
pytestmark = pytest.mark.single_cpu
1918

2019

21-
def test_retain_index_attributes(setup_path):
20+
def test_retain_index_attributes(setup_path, unit):
2221
# GH 3499, losing frequency info on index recreation
23-
df = DataFrame(
24-
{"A": Series(range(3), index=date_range("2000-1-1", periods=3, freq="h"))}
25-
)
22+
dti = date_range("2000-1-1", periods=3, freq="h", unit=unit)
23+
df = DataFrame({"A": Series(range(3), index=dti)})
2624

2725
with ensure_clean_store(setup_path) as store:
2826
_maybe_remove(store, "data")
@@ -37,37 +35,30 @@ def test_retain_index_attributes(setup_path):
3735
getattr(result, idx), attr, None
3836
)
3937

38+
dti2 = date_range("2002-1-1", periods=3, freq="D", unit=unit)
4039
# try to append a table with a different frequency
4140
with tm.assert_produces_warning(errors.AttributeConflictWarning):
42-
df2 = DataFrame(
43-
{
44-
"A": Series(
45-
range(3), index=date_range("2002-1-1", periods=3, freq="D")
46-
)
47-
}
48-
)
41+
df2 = DataFrame({"A": Series(range(3), index=dti2)})
4942
store.append("data", df2)
5043

5144
assert store.get_storer("data").info["index"]["freq"] is None
5245

5346
# this is ok
5447
_maybe_remove(store, "df2")
48+
dti3 = DatetimeIndex(
49+
["2001-01-01", "2001-01-02", "2002-01-01"], dtype=f"M8[{unit}]"
50+
)
5551
df2 = DataFrame(
5652
{
5753
"A": Series(
5854
range(3),
59-
index=[
60-
Timestamp("20010101"),
61-
Timestamp("20010102"),
62-
Timestamp("20020101"),
63-
],
55+
index=dti3,
6456
)
6557
}
6658
)
6759
store.append("df2", df2)
68-
df3 = DataFrame(
69-
{"A": Series(range(3), index=date_range("2002-1-1", periods=3, freq="D"))}
70-
)
60+
dti4 = date_range("2002-1-1", periods=3, freq="D", unit=unit)
61+
df3 = DataFrame({"A": Series(range(3), index=dti4)})
7162
store.append("df2", df3)
7263

7364

pandas/tests/io/pytables/test_round_trip.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pandas as pd
1111
from pandas import (
1212
DataFrame,
13+
DatetimeIndex,
1314
Index,
1415
Series,
1516
_testing as tm,
@@ -320,7 +321,11 @@ def test_index_types(setup_path):
320321
ser = Series(values, [1, 5])
321322
_check_roundtrip(ser, func, path=setup_path)
322323

323-
ser = Series(values, [datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 2)])
324+
dti = DatetimeIndex(["2012-01-01", "2012-01-02"], dtype="M8[ns]")
325+
ser = Series(values, index=dti)
326+
_check_roundtrip(ser, func, path=setup_path)
327+
328+
ser.index = ser.index.as_unit("s")
324329
_check_roundtrip(ser, func, path=setup_path)
325330

326331

pandas/tests/io/pytables/test_store.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -542,17 +542,16 @@ def test_store_index_name(setup_path):
542542

543543

544544
@pytest.mark.parametrize("tz", [None, "US/Pacific"])
545-
@pytest.mark.parametrize("unit", ["s", "ms", "us", "ns"])
546545
@pytest.mark.parametrize("table_format", ["table", "fixed"])
547546
def test_store_index_name_numpy_str(tmp_path, table_format, setup_path, unit, tz):
548547
# GH #13492
549-
idx = Index(
550-
pd.to_datetime([dt.date(2000, 1, 1), dt.date(2000, 1, 2)]),
548+
idx = DatetimeIndex(
549+
[dt.date(2000, 1, 1), dt.date(2000, 1, 2)],
551550
name="cols\u05d2",
552551
).tz_localize(tz)
553552
idx1 = (
554-
Index(
555-
pd.to_datetime([dt.date(2010, 1, 1), dt.date(2010, 1, 2)]),
553+
DatetimeIndex(
554+
[dt.date(2010, 1, 1), dt.date(2010, 1, 2)],
556555
name="rows\u05d0",
557556
)
558557
.as_unit(unit)

pandas/tests/io/pytables/test_time_series.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from pandas import (
77
DataFrame,
8+
DatetimeIndex,
89
Series,
910
_testing as tm,
1011
)
@@ -13,10 +14,12 @@
1314
pytestmark = pytest.mark.single_cpu
1415

1516

16-
def test_store_datetime_fractional_secs(setup_path):
17+
@pytest.mark.parametrize("unit", ["us", "ns"])
18+
def test_store_datetime_fractional_secs(setup_path, unit):
19+
dt = datetime.datetime(2012, 1, 2, 3, 4, 5, 123456)
20+
dti = DatetimeIndex([dt], dtype=f"M8[{unit}]")
21+
series = Series([0], index=dti)
1722
with ensure_clean_store(setup_path) as store:
18-
dt = datetime.datetime(2012, 1, 2, 3, 4, 5, 123456)
19-
series = Series([0], [dt])
2023
store["a"] = series
2124
assert store["a"].index[0] == dt
2225

pandas/tests/io/pytables/test_timezones.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,20 @@ def test_append_with_timezones_as_index(setup_path, gettz):
148148
tm.assert_frame_equal(result, df)
149149

150150

151-
def test_roundtrip_tz_aware_index(setup_path):
151+
def test_roundtrip_tz_aware_index(setup_path, unit):
152152
# GH 17618
153-
time = Timestamp("2000-01-01 01:00:00", tz="US/Eastern")
154-
df = DataFrame(data=[0], index=[time])
153+
ts = Timestamp("2000-01-01 01:00:00", tz="US/Eastern")
154+
dti = DatetimeIndex([ts]).as_unit(unit)
155+
df = DataFrame(data=[0], index=dti)
155156

156157
with ensure_clean_store(setup_path) as store:
157158
store.put("frame", df, format="fixed")
158159
recons = store["frame"]
159160
tm.assert_frame_equal(recons, df)
160-
assert recons.index[0]._value == 946706400000000000
161+
162+
value = recons.index[0]._value
163+
denom = {"ns": 1, "us": 1000, "ms": 10**6, "s": 10**9}[unit]
164+
assert value == 946706400000000000 // denom
161165

162166

163167
def test_store_index_name_with_tz(setup_path):
@@ -365,7 +369,7 @@ def test_py2_created_with_datetimez(datapath):
365369
# Python 3.
366370
#
367371
# GH26443
368-
index = [Timestamp("2019-01-01T18:00").tz_localize("America/New_York")]
372+
index = DatetimeIndex(["2019-01-01T18:00"], dtype="M8[ns, America/New_York]")
369373
expected = DataFrame({"data": 123}, index=index)
370374
with ensure_clean_store(
371375
datapath("io", "data", "legacy_hdf", "gh26443.h5"), mode="r"

0 commit comments

Comments
 (0)