verticapy.vDataColumn.date_part#
- vDataColumn.date_part(field: str) vDataFrame #
Extracts a specific TS field from the vDataColumn (only if the vDataColumn type is date like). The vDataColumn 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 | microseconds | millennium | milliseconds | minute | month | quarter | second | time
zone | timezone_hour | timezone_minute | week | year
Returns#
- vDataFrame
self._parent
Examples#
Let’s begin by importing VerticaPy.
import verticapy as vp
Hint
By assigning an alias to
verticapy
, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy 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 fromverticapy
are used as intended without interfering with functions from other libraries.Let us create a dummy dataset that has timestamp values:
vdf = vp.vDataFrame( { "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)100%123valNumeric(4)100%1 1993-11-03 00:00:00 0.0 2 1993-11-04 00:00:01 1.0 3 1993-11-05 00:00:02 2.0 4 1993-11-06 00:00:04 4.0 5 1993-11-07 00:00:05 5.0 We can make sure that the column has the correct data type:
vdf["time"].astype("datetime")
Next, we can apply the
date_part
function to get the required temporal details:vdf["time"].date_part(field = "day")
123timeNumeric(20)100%123valNumeric(4)100%1 3.0 0.0 2 4.0 1.0 3 5.0 2.0 4 6.0 4.0 5 7.0 5.0 Note
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"] = "DATE_PART('DAY', val)"
See also