Loading...

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 from verticapy 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.],
    }
)

Abc
time
Varchar(19)
100%
123
val
Numeric(4)
100%
11993-11-03 00:00:000.0
21993-11-04 00:00:011.0
31993-11-05 00:00:022.0
41993-11-06 00:00:044.0
51993-11-07 00:00:055.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")
123
time
Numeric(20)
100%
123
val
Numeric(4)
100%
13.00.0
24.01.0
35.02.0
46.04.0
57.05.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

vDataColumn.slice() : Slice the vDataColumn by custom time-steps.