vastorbit.VastColumn.date_part¶
- VastColumn.date_part(field: str) VastFrame¶
Extracts a specific TS field from the VastColumn (only if the VastColumn type is date like). The VastColumn is transformed.
- Parameters:
field (str) – The field to extract. It must be one of the following: century | day | decade | doq | dow | doy | epoch | hour | isodow | isoweek | isoyear | millennium | milliseconds | minute | month | quarter | second | timezone | timezone_hour | timezone_minute | week | year
- Returns:
self._parent
- Return type:
Examples
Let’s begin by importing vastorbit.
import vastorbit as vo
Hint
By assigning an alias to
vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions fromvastorbitare used as intended without interfering with functions from other libraries.Let us create a dummy dataset that has timestamp values:
vdf = vo.VastFrame( { "time": [ "1993-11-03 00:00:00", "1993-11-04 00:00:01", "1993-11-05 00:00:02", "1993-11-06 00:00:04", "1993-11-07 00:00:05", ], "val": [0., 1., 2., 4.,5.], } )
AbctimeVarchar(19)123valDecimal(2, 1)1 1993-11-06 00:00:04 4.0 2 1993-11-07 00:00:05 5.0 3 1993-11-03 00:00:00 0.0 4 1993-11-05 00:00:02 2.0 5 1993-11-04 00:00:01 1.0 Rows: 1-5 | Columns: 2We can make sure that the column has the correct data type:
vdf["time"].astype("timestamp")
Next, we can apply the
date_partfunction to get the required temporal details:vdf["time"].date_part(field = "day")
123timeBigint123valDecimal(2, 1)1 5 2.0 2 7 5.0 3 3 0.0 4 4 1.0 5 6 4.0 Rows: 1-5 | Columns: 2Note
While the same task can be accomplished using pure SQL (see below), adopting a Pythonic approach can offer greater convenience and help avoid potential syntax errors.
vdf["val"] = "EXTRACT(DAY FROM val)"
See also