Loading...

verticapy.vDataColumn.slice#

vDataColumn.slice(length: int, unit: str = 'second', start: bool = True) vDataFrame#

Slices and transforms the vDataColumn using a time series rule.

Parameters#

length: int

Slice size.

unit: str, optional

Slice size unit. For example, ‘minute’, ‘hour’…

start: bool, optional

If set to True, the record is sliced using the floor of the slicing instead of the ceiling.

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-03 00:30:01",
            "1993-11-03 00:31:00",
            "1993-11-03 01:05:01",
            "1993-11-03 01:41:02",
            "1993-11-03 01:50:00",
        ],
        "val": [0., 1., 2., 4., 5., 4.],
    }
)

Abc
time
Varchar(19)
100%
123
val
Numeric(4)
100%
11993-11-03 00:00:000.0
21993-11-03 00:30:011.0
31993-11-03 00:31:002.0
41993-11-03 01:05:014.0
51993-11-03 01:41:025.0
61993-11-03 01:50:004.0

We can make sure that the column has the correct data type:

vdf["time"].astype("datetime")

Next, we can conveniently slice the data into intervals of 30 minutes using:

vdf["time"].slice(30, "minute")
📅
time
Timestamp(29)
100%
123
val
Numeric(4)
100%
11993-11-03 00:00:000.0
21993-11-03 00:30:001.0
31993-11-03 00:30:002.0
41993-11-03 01:00:004.0
51993-11-03 01:30:005.0
61993-11-03 01:30:004.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"] = "TIME_SLICE(val, 30, 'MINUTE')"

See also

vDataColumn.date_part() : Extracts a specific TS field from the vDataColumn.