Databases Reference
In-Depth Information
>>> dset = f . create_dataset ( 'opaque' , ( 10 ,), dtype = dt )
>>> dset . dtype
dtype('|V200')
>>> dset . shape
(10,)
You should seriously consider using opaque types for storing raw binary data. It may
be tempting simply to store the data in a string, but remember that strings in HDF5 are
reserved either for ASCII or Unicode text.
Here's an example of how to “round-trip” a Python byte string through the HDF5 opaque
type, in this case to store binary data in an attribute:
>>> binary_blob = b "A \x00 B \x00 " # Try storing this directly! It won't work.
>>> obj . attrs [ "name" ] = np . void ( binary_blob ) # "Void" type maps to HDF5 opaque
>>> out = obj . attrs [ "name" ]
>>> binary_blob = out . tostring ()
Dates and Times
One frequently asked question is how to express time information in HDF5. At one
point there was a datetime type in HDF5, although to my knowledge nobody in the
Python world ever used it. Typically dates and times are expressed in HDF5 on an ad-
hoc basis.
One way to represent time is by a count of seconds (including fractional seconds) since
some time in the past, called the “epoch.” For example, “Unix time” or “POSIX time”
counts the number of seconds since midnight Jan. 1, 1970 UTC.
If you need only seconds of resolution, an integer works well:
>>> timestamp = np . dtype ( 'u8' )
You can also use a double-precision float to represent fractional time, as provided by
the built-in time.time() :
>>> import time , datetime
>>> time . time ()
1377548506.627
datetime objects can be used to provide a string in “ISO” format, which yields a nicer-
looking result:
>>> datetime . datetime . now () . isoformat ()
'2013-08-26T14:30:02.633000'
Such timestamps are also called “naive” timestamps, because they don't include infor‐
mation on the time zone or leap seconds. If your application is purely working in one
time zone, or only dealing in time differences (and can ignore leap seconds for this
purpose), this is likely OK. Otherwise, you will have to store appropriate data on the
time zone somewhere close by (like in another member of a compound type).
Search WWH ::




Custom Search