Learn how to work with datetimes in TileDB arrays, including writing and reading datetime data.
How to run this tutorial
You can run this tutorial in two ways:
Locally on your machine.
On TileDB Cloud.
However, since TileDB Cloud has a free tier, we strongly recommend that you sign up and run everything there, as that requires no installations or deployment.
This tutorial explains how to use datetimes. For more information, visit the Key Concepts: Datetimes section.
First, import the necessary libraries, set the array URI (that is, its path, which in this tutorial will be on local storage), and delete any previously created arrays with the same name.
# Single dimension with a domain of 10 years, day resolution, one tile per 365 daysdim = tiledb.Dim( name="dim", domain=(np.datetime64("2014-01-01"), np.datetime64("2024-01-01")), tile=np.timedelta64(365, "D"), dtype=np.datetime64("", "D").dtype,)# Add the dimension to the array domaindom = tiledb.Domain(dim)# Create an attributea = tiledb.Attr(name="a", dtype=np.float64)# Create the array schema, setting `sparse=False` to indicate a dense arraysch = tiledb.ArraySchema(domain=dom, sparse=False, attrs=[a])# Create the array on disk (it will initially be empty)tiledb.Array.create(array_uri, sch)
# Domain is 10 years, day resolution, one tile per 365 daysdim <-tiledb_dim("d1",c(as.Date("2014-01-01"), as.Date("2024-01-01")),365,"FLOAT64")dom <-tiledb_domain(dims =c(dim))sch <-tiledb_array_schema( dom,attrs =c(tiledb_attr("a1", type ="FLOAT64")),sparse =TRUE)arr <-tiledb_array_create(array_uri, sch)
# Randomly generate 2 years of values for attribute 'a'ndays =365*2a_vals = np.random.rand(ndays)# Write the data at the beginning of the domainstart = np.datetime64("2014-01-01")end = start + np.timedelta64(ndays -1, "D")# Write the data to the arraywith tiledb.open(array_uri, "w") as A: A[start:end] = {"a": a_vals}
# Randomly generate 2 years of values for attribute 'a1'ndays <-365*2a1_vals <-runif(ndays, min =0, max =1)# Write the data at the beginning of the domaind1_vals <-as.Date("2014-01-01") +0:(ndays -1)arr <-tiledb_array(array_uri, return_as ="data.frame")arr[] <-data.frame(d1 = d1_vals, a1 = a1_vals)
# Slice a few days from the middle using two datetimeswith tiledb.open(array_uri, "r", attr="a") as A: vals = A[np.datetime64("2014-11-01") : np.datetime64("2015-01-31")]print(vals)