Skip to content

Commit fb33710

Browse files
arw2019JulianWgs
authored andcommitted
BUG: preserve timezone info when writing empty tz-aware frame to HDF5 (pandas-dev#37069)
1 parent 24e997f commit fb33710

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,7 @@ I/O
433433
- Bug in :meth:`to_json` with ``lines=True`` and ``orient='records'`` the last line of the record is not appended with 'new line character' (:issue:`36888`)
434434
- Bug in :meth:`read_parquet` with fixed offset timezones. String representation of timezones was not recognized (:issue:`35997`, :issue:`36004`)
435435
- Bug in output rendering of complex numbers showing too many trailing zeros (:issue:`36799`)
436+
- Bug in :class:`HDFStore` threw a ``TypeError`` when exporting an empty :class:`DataFrame` with ``datetime64[ns, tz]`` dtypes with a fixed HDF5 store (:issue:`20594`)
436437

437438
Plotting
438439
^^^^^^^^

pandas/io/pytables.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -3027,8 +3027,6 @@ def write_array(self, key: str, value: ArrayLike, items: Optional[Index] = None)
30273027
vlarr = self._handle.create_vlarray(self.group, key, _tables().ObjectAtom())
30283028
vlarr.append(value)
30293029

3030-
elif empty_array:
3031-
self.write_array_empty(key, value)
30323030
elif is_datetime64_dtype(value.dtype):
30333031
self._handle.create_array(self.group, key, value.view("i8"))
30343032
getattr(self.group, key)._v_attrs.value_type = "datetime64"
@@ -3043,6 +3041,8 @@ def write_array(self, key: str, value: ArrayLike, items: Optional[Index] = None)
30433041
elif is_timedelta64_dtype(value.dtype):
30443042
self._handle.create_array(self.group, key, value.view("i8"))
30453043
getattr(self.group, key)._v_attrs.value_type = "timedelta64"
3044+
elif empty_array:
3045+
self.write_array_empty(key, value)
30463046
else:
30473047
self._handle.create_array(self.group, key, value)
30483048

pandas/tests/io/pytables/test_timezones.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def test_tseries_select_index_column(setup_path):
268268
assert rng.tz == result.dt.tz
269269

270270

271-
def test_timezones_fixed(setup_path):
271+
def test_timezones_fixed_format_frame_non_empty(setup_path):
272272
with ensure_clean_store(setup_path) as store:
273273

274274
# index
@@ -296,6 +296,16 @@ def test_timezones_fixed(setup_path):
296296
tm.assert_frame_equal(result, df)
297297

298298

299+
@pytest.mark.parametrize("dtype", ["datetime64[ns, UTC]", "datetime64[ns, US/Eastern]"])
300+
def test_timezones_fixed_format_frame_empty(setup_path, dtype):
301+
with ensure_clean_store(setup_path) as store:
302+
s = Series(dtype=dtype)
303+
df = DataFrame({"A": s})
304+
store["df"] = df
305+
result = store["df"]
306+
tm.assert_frame_equal(result, df)
307+
308+
299309
def test_fixed_offset_tz(setup_path):
300310
rng = date_range("1/1/2000 00:00:00-07:00", "1/30/2000 00:00:00-07:00")
301311
frame = DataFrame(np.random.randn(len(rng), 4), index=rng)

0 commit comments

Comments
 (0)