From 58e70990b2de2acad9779c5f1c586059f90a09f5 Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Thu, 21 Nov 2024 21:11:55 +0800 Subject: [PATCH 1/2] add test for dataframe-to_hdf-datetme64 --- .../tests/io/pytables/test_retain_attributes.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pandas/tests/io/pytables/test_retain_attributes.py b/pandas/tests/io/pytables/test_retain_attributes.py index 6284b826c3cf0..64e69f7af2eca 100644 --- a/pandas/tests/io/pytables/test_retain_attributes.py +++ b/pandas/tests/io/pytables/test_retain_attributes.py @@ -8,6 +8,7 @@ date_range, errors, read_hdf, + to_datetime, ) from pandas.tests.io.pytables.common import ( _maybe_remove, @@ -90,3 +91,19 @@ def test_retain_index_attributes2(tmp_path, setup_path): df2.to_hdf(path, key="data", append=True) assert read_hdf(path, "data").index.name is None + + +def test_retain_datetime_attribute(tmp_path, setup_path): + path = tmp_path / setup_path + ser = Series( + [ + to_datetime("2024-08-26 15:13:14"), + to_datetime("2024-08-26 15:14:14"), + ], + dtype="datetime64[us, UTC]", + ) + dataframe = DataFrame(ser) + dataframe.to_hdf(path, key="Annotations", mode="w") + + recovered_dataframe = read_hdf(path, key="Annotations") + tm.assert_frame_equal(dataframe, recovered_dataframe) From 4726d07db1ff2d914ab32a6a03eb380993d669cd Mon Sep 17 00:00:00 2001 From: KevsterAmp Date: Thu, 21 Nov 2024 21:15:01 +0800 Subject: [PATCH 2/2] improve initialization of series --- pandas/tests/io/pytables/test_retain_attributes.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/tests/io/pytables/test_retain_attributes.py b/pandas/tests/io/pytables/test_retain_attributes.py index 64e69f7af2eca..ab8dcacc4b8d4 100644 --- a/pandas/tests/io/pytables/test_retain_attributes.py +++ b/pandas/tests/io/pytables/test_retain_attributes.py @@ -8,7 +8,6 @@ date_range, errors, read_hdf, - to_datetime, ) from pandas.tests.io.pytables.common import ( _maybe_remove, @@ -96,10 +95,7 @@ def test_retain_index_attributes2(tmp_path, setup_path): def test_retain_datetime_attribute(tmp_path, setup_path): path = tmp_path / setup_path ser = Series( - [ - to_datetime("2024-08-26 15:13:14"), - to_datetime("2024-08-26 15:14:14"), - ], + ["2024-08-26 15:13:14", "2024-08-26 15:14:14"], dtype="datetime64[us, UTC]", ) dataframe = DataFrame(ser)