Expressions API
class pw.DateTimeNamespace(expression)
Typical use:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 2023-05-15T14:13:00
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S"))
table_with_days = table_with_datetime.select(day=table_with_datetime.t1.dt.day())
add_duration_in_timezone(duration, timezone)
Adds Duration to DateTime taking into account time zone.- Parameters
- duration (
Union
[ColumnExpression
,Timedelta
]) – Duration to be added to DateTime. - timezone (
Union
[ColumnExpression
,str
]) – The time zone to perform addition in.
- duration (
- Returns
DateTimeNaive or DateTimeUtc depending on the type of an object the method was called on
Example:
import pathway as pw
import datetime
t1 = pw.debug.table_from_markdown(
'''
| date
1 | 2023-03-26T01:23:00
2 | 2023-03-27T01:23:00
3 | 2023-10-29T01:23:00
4 | 2023-10-30T01:23:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
t2 = t1.select(date=pw.this.date.dt.strptime(fmt=fmt))
t3 = t2.with_columns(
new_date=pw.this.date.dt.add_duration_in_timezone(
datetime.timedelta(hours=2), timezone="Europe/Warsaw"
),
)
pw.debug.compute_and_print(t3, include_id=False)
day()
Extracts day from a DateTime.- Returns
Day as int. 1 <= day <= 31 (depending on a month)
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 1974-03-12T00:00:00
2 | 2023-03-25T12:00:00
3 | 2023-05-15T14:13:00
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S"))
table_with_days = table_with_datetime.select(day=table_with_datetime.t1.dt.day())
pw.debug.compute_and_print(table_with_days, include_id=False)
days()
The total number of days in a Duration.- Returns
Days as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-03-15T00:00:00 | 2023-05-15T10:13:23
1 | 2023-04-15T00:00:00 | 2023-05-15T10:00:00
2 | 2023-05-01T10:00:00 | 2023-05-15T10:00:00
3 | 2023-05-15T10:00:00 | 2023-05-15T09:00:00
4 | 2023-05-15T10:00:00 | 2023-05-15T11:00:00
5 | 2023-05-16T12:13:00 | 2023-05-15T10:00:00
6 | 2024-05-15T14:13:23 | 2023-05-15T10:00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_days = table_with_diff.select(days=pw.this["diff"].dt.days())
pw.debug.compute_and_print(table_with_days, include_id=False)
floor(duration)
Truncates DateTime to precision specified by duration argument.- Parameters
duration (Union
[ColumnExpression
,Timedelta
]) – truncation precision - Returns
DateTimeNaive or DateTimeUtc depending on the type of an object the method was called on
Examples:
import pathway as pw
import datetime
t1 = pw.debug.table_from_markdown(
'''
| date
1 | 2023-05-15T12:23:12
2 | 2023-05-15T12:33:21
3 | 2023-05-15T13:20:35
4 | 2023-05-15T13:51:41
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
t2 = t1.select(date=pw.this.date.dt.strptime(fmt=fmt))
res = t2.with_columns(
truncated_to_hours=pw.this.date.dt.floor(datetime.timedelta(hours=1)),
truncated_to_10_min=pw.this.date.dt.floor(datetime.timedelta(minutes=10)),
truncated_to_15_s=pw.this.date.dt.floor(datetime.timedelta(seconds=15)),
)
pw.debug.compute_and_print(res, include_id=False)
from_timestamp(unit)
Converts timestamp represented as an int to DateTime.- Parameters
- timestamp – value to be converted to DateTime
- unit (
str
) – unit of a timestamp. It has to be one of ‘s’, ‘ms’, ‘us’, ‘ns’
- Returns
DateTime
Example:
import pathway as pw
fmt = "%Y-%m-%dT%H:%M:%S"
t1 = pw.debug.table_from_markdown(
'''
| timestamp
1 | 10
2 | 1685969950
'''
)
t2 = t1.select(date=pw.this.timestamp.dt.from_timestamp(unit="s"))
pw.debug.compute_and_print(t2, include_id=False)
hour()
Extracts hour from a DateTime.- Returns
Hour as int. 0 <= hour < 24
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 2023-05-15T00:00:00
2 | 2023-05-15T12:00:00
3 | 2023-05-15T14:13:00
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S"))
table_with_hours = table_with_datetime.select(hour=table_with_datetime.t1.dt.hour())
pw.debug.compute_and_print(table_with_hours, include_id=False)
hours()
The total number of hours in a Duration.- Returns
Hours as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-05-15T00:00:00 | 2023-05-15T10:13:23
1 | 2023-05-15T00:00:00 | 2023-05-15T10:00:00
2 | 2023-05-15T10:00:00 | 2023-05-15T10:00:00
3 | 2023-05-15T10:00:23 | 2023-05-15T10:00:00
4 | 2023-05-15T12:13:00 | 2023-05-15T10:00:00
5 | 2023-05-15T14:13:23 | 2023-05-15T10:00:00
6 | 2023-05-16T10:13:23 | 2023-05-15T10:00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_hours = table_with_diff.select(hours=pw.this["diff"].dt.hours())
pw.debug.compute_and_print(table_with_hours, include_id=False)
microsecond()
Extracts microseconds from a DateTime.- Returns
Microsecond as int. 0 <= microsecond < 1_000_000
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.000012000
3 | 2023-05-15T10:13:00.123456789
4 | 2023-05-15T10:13:23.123456789
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S.%f"))
table_with_microseconds = table_with_datetime.select(
microsecond=table_with_datetime.t1.dt.microsecond()
)
pw.debug.compute_and_print(table_with_microseconds, include_id=False)
microseconds()
The total number of microseconds in a Duration.- Returns
Microseconds as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:23.123456789
1 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.000012000 | 2023-05-15T10:13:00.000000000
3 | 2023-05-15T10:13:00.123456789 | 2023-05-15T10:13:00.000000000
4 | 2023-05-15T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
5 | 2023-05-16T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
'''
)
fmt = "%Y-%m-%dT%H:%M:%S.%f"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_microseconds = table_with_diff.select(
microseconds=pw.this["diff"].dt.microseconds()
)
pw.debug.compute_and_print(table_with_microseconds, include_id=False)
millisecond()
Extracts milliseconds from a DateTime.- Returns
Millisecond as int. 0 <= millisecond < 1_000
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.012000000
3 | 2023-05-15T10:13:00.123456789
4 | 2023-05-15T10:13:23.123456789
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S.%f"))
table_with_milliseconds = table_with_datetime.select(
millisecond=table_with_datetime.t1.dt.millisecond()
)
pw.debug.compute_and_print(table_with_milliseconds, include_id=False)
milliseconds()
The total number of milliseconds in a Duration.- Returns
Milliseconds as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:23.123456789
1 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.012000000 | 2023-05-15T10:13:00.000000000
3 | 2023-05-15T10:13:00.123456789 | 2023-05-15T10:13:00.000000000
4 | 2023-05-15T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
5 | 2023-05-16T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
'''
)
fmt = "%Y-%m-%dT%H:%M:%S.%f"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_milliseconds = table_with_diff.select(
milliseconds=pw.this["diff"].dt.milliseconds()
)
pw.debug.compute_and_print(table_with_milliseconds, include_id=False)
minute()
Extracts minute from a DateTime.- Returns
Minute as int. 0 <= minute < 60
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 2023-05-15T10:00:00
2 | 2023-05-15T10:00:23
3 | 2023-05-15T10:13:00
4 | 2023-05-15T10:13:23
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S"))
table_with_minutes = table_with_datetime.select(
minute=table_with_datetime.t1.dt.minute()
)
pw.debug.compute_and_print(table_with_minutes, include_id=False)
minutes()
The total number of minutes in a Duration.- Returns
Minutes as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-05-15T10:00:00 | 2023-05-15T10:13:23
1 | 2023-05-15T10:00:00 | 2023-05-15T10:00:00
2 | 2023-05-15T10:00:23 | 2023-05-15T10:00:00
3 | 2023-05-15T10:13:00 | 2023-05-15T10:00:00
4 | 2023-05-15T10:13:23 | 2023-05-15T10:00:00
5 | 2023-05-16T10:13:23 | 2023-05-15T10:00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_minutes = table_with_diff.select(minutes=pw.this["diff"].dt.minutes())
pw.debug.compute_and_print(table_with_minutes, include_id=False)
month()
Extracts month from a DateTime.- Returns
Month as int. 1 <= month <= 12
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 1974-03-12T00:00:00
2 | 2023-03-25T12:00:00
3 | 2023-05-15T14:13:00
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S"))
table_with_months = table_with_datetime.select(month=table_with_datetime.t1.dt.month())
pw.debug.compute_and_print(table_with_months, include_id=False)
nanosecond()
Extracts nanoseconds from a DateTime.- Returns
Nanosecond as int. 0 <= nanosecond < 1_000_000_000
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.000000012
3 | 2023-05-15T10:13:00.123456789
4 | 2023-05-15T10:13:23.123456789
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S.%f"))
table_with_nanoseconds = table_with_datetime.select(
nanosecond=table_with_datetime.t1.dt.nanosecond()
)
pw.debug.compute_and_print(table_with_nanoseconds, include_id=False)
nanoseconds()
The total number of nanoseconds in a Duration.- Returns
Nanoseconds as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:23.123456789
1 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.000000012 | 2023-05-15T10:13:00.000000000
3 | 2023-05-15T10:13:00.123456789 | 2023-05-15T10:13:00.000000000
4 | 2023-05-15T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
5 | 2023-05-16T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
'''
)
fmt = "%Y-%m-%dT%H:%M:%S.%f"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_nanoseconds = table_with_diff.select(
nanoseconds=pw.this["diff"].dt.nanoseconds()
)
pw.debug.compute_and_print(table_with_nanoseconds, include_id=False)
round(duration)
Rounds DateTime to precision specified by duration argument.- Parameters
duration (Union
[ColumnExpression
,Timedelta
]) – rounding precision - Returns
DateTimeNaive or DateTimeUtc depending on the type of an object the method was called on
Examples:
import pathway as pw
import datetime
t1 = pw.debug.table_from_markdown(
'''
| date
1 | 2023-05-15T12:23:12
2 | 2023-05-15T12:33:21
3 | 2023-05-15T13:20:35
4 | 2023-05-15T13:51:41
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
t2 = t1.select(date=pw.this.date.dt.strptime(fmt=fmt))
res = t2.with_columns(
rounded_to_hours=pw.this.date.dt.round(datetime.timedelta(hours=1)),
rounded_to_10_min=pw.this.date.dt.round(datetime.timedelta(minutes=10)),
rounded_to_15_s=pw.this.date.dt.round(datetime.timedelta(seconds=15)),
)
pw.debug.compute_and_print(res, include_id=False)
second()
Extracts seconds from a DateTime.- Returns
Second as int. 0 <= second < 60
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.123456789
3 | 2023-05-15T10:13:23.000000000
4 | 2023-05-15T10:13:23.123456789
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S.%f"))
table_with_seconds = table_with_datetime.select(
second=table_with_datetime.t1.dt.second()
)
pw.debug.compute_and_print(table_with_seconds, include_id=False)
seconds()
The total number of seconds in a Duration.- Returns
Seconds as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:23.123456789
1 | 2023-05-15T10:13:00.000000000 | 2023-05-15T10:13:00.000000000
2 | 2023-05-15T10:13:00.123456789 | 2023-05-15T10:13:00.000000000
3 | 2023-05-15T10:13:23.000000000 | 2023-05-15T10:13:00.000000000
4 | 2023-05-15T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
5 | 2023-05-16T10:13:23.123456789 | 2023-05-15T10:13:00.000000000
'''
)
fmt = "%Y-%m-%dT%H:%M:%S.%f"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_seconds = table_with_diff.select(seconds=pw.this["diff"].dt.seconds())
pw.debug.compute_and_print(table_with_seconds, include_id=False)
strftime(fmt)
Converts a DateTime to a string.- Parameters
fmt (Union
[ColumnExpression
,str
]) – Format string. We use the specifiers of chrono library. In most cases they are identical to standard python specifiers in strftime . - Returns
str
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 1970-02-03T10:13:00
2 | 2023-03-25T10:13:00
3 | 2023-03-26T12:13:00
4 | 2023-05-15T14:13:23
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
table_with_datetime = table.select(t1=pw.this.t1.dt.strptime(fmt=fmt))
table_formatted = table_with_datetime.select(
date=pw.this.t1.dt.strftime("%d.%m.%Y"),
full_date=pw.this.t1.dt.strftime("%B %d, %Y"),
time_24=pw.this.t1.dt.strftime("%H:%M:%S"),
time_12=pw.this.t1.dt.strftime("%I:%M:%S %p"),
)
pw.debug.compute_and_print(table_formatted, include_id=False)
strptime(fmt, contains_timezone=None)
Converts a string to a DateTime. If the string contains a timezone and a %z specifier is used, timezone-aware DateTime is created. Then the timezone is converted to a server timezone (see examples). If the string contains no timezone, a naive (not aware of timezone) DateTime is created.- Parameters
fmt (Union
[ColumnExpression
,str
]) – Format string. We use the specifiers of chrono library. In most cases they are identical to standard python specifiers in strptime . contains_timezone: If fmt is not a single string (the same for all objects) but a ColumnExpression, you need to set this parameter so that the function can determine if the return type is DateTimeNaive (contains_timezone = False) or DateTimeUtc (contains_timezone = True). - Returns
DateTime
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 1970-02-03T10:13:00.000000000
2 | 2023-03-25T10:13:00.000000012
3 | 2023-03-26T12:13:00.123456789
4 | 2023-05-15T14:13:23.123456789
'''
)
fmt = "%Y-%m-%dT%H:%M:%S.%f"
table_with_datetime = table.select(t1=table.t1.dt.strptime(fmt=fmt))
pw.debug.compute_and_print(table_with_datetime, include_id=False)
table = pw.debug.table_from_markdown(
'''
| t1
1 | 03.02.1970T10:13:00.000000000
2 | 25.03.2023T10:13:00.000000012
3 | 26.03.2023T12:13:00.123456789
4 | 15.05.2023T14:13:23.123456789
'''
)
fmt = "%d.%m.%YT%H:%M:%S.%f"
table_with_datetime = table.select(t1=table.t1.dt.strptime(fmt=fmt))
pw.debug.compute_and_print(table_with_datetime, include_id=False)
table = pw.debug.table_from_markdown(
'''
| t1
1 | 1970-02-03T10:13:00-02:00
2 | 2023-03-25T10:13:00+00:00
3 | 2023-03-26T12:13:00-01:00
4 | 2023-05-15T14:13:23+00:30
'''
)
fmt = "%Y-%m-%dT%H:%M:%S%z"
table_with_datetime = table.select(t1=table.t1.dt.strptime(fmt=fmt))
pw.debug.compute_and_print(table_with_datetime, include_id=False)
subtract_date_time_in_timezone(date_time, timezone)
Subtracts two DateTimeNaives taking into account time zone.- Parameters
- date_time (
Union
[ColumnExpression
,Timestamp
]) – DateTimeNaive to be subtracted from self. - timezone (
Union
[ColumnExpression
,str
]) – The time zone to perform subtraction in.
- date_time (
- Returns
Duration
Example:
import pathway as pw
t1 = pw.debug.table_from_markdown(
'''
| date1 | date2
1 | 2023-03-26T03:20:00 | 2023-03-26T01:20:00
2 | 2023-03-27T03:20:00 | 2023-03-27T01:20:00
3 | 2023-10-29T03:20:00 | 2023-10-29T01:20:00
4 | 2023-10-30T03:20:00 | 2023-10-30T01:20:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
t2 = t1.select(
date1=pw.this.date1.dt.strptime(fmt=fmt), date2=pw.this.date2.dt.strptime(fmt=fmt)
)
t3 = t2.with_columns(
diff=pw.this.date1.dt.subtract_date_time_in_timezone(
pw.this.date2, timezone="Europe/Warsaw"
),
)
pw.debug.compute_and_print(t3, include_id=False)
subtract_duration_in_timezone(duration, timezone)
Subtracts Duration from DateTime taking into account time zone.- Parameters
- duration (
Union
[ColumnExpression
,Timedelta
]) – Duration to be subtracted from DateTime. - timezone (
Union
[ColumnExpression
,str
]) – The time zone to perform subtraction in.
- duration (
- Returns
DateTimeNaive or DateTimeUtc depending on the type of an object the method was called on
Example:
import pathway as pw
import datetime
t1 = pw.debug.table_from_markdown(
'''
| date
1 | 2023-03-26T03:23:00
2 | 2023-03-27T03:23:00
3 | 2023-10-29T03:23:00
4 | 2023-10-30T03:23:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
t2 = t1.select(date=pw.this.date.dt.strptime(fmt=fmt))
t3 = t2.with_columns(
new_date=pw.this.date.dt.subtract_duration_in_timezone(
datetime.timedelta(hours=2), timezone="Europe/Warsaw"
),
)
pw.debug.compute_and_print(t3, include_id=False)
timestamp()
Returns a number of nanoseconds from 1970-01-01 for naive DateTime and from 1970-01-01 UTC for timezone-aware datetime.- Returns
Timestamp as int.
Examples:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
0 | 1969-01-01T00:00:00.000000000
1 | 1970-01-01T00:00:00.000000000
2 | 2023-01-01T00:00:00.000000000
3 | 2023-03-25T00:00:00.000000000
4 | 2023-03-25T13:45:26.000000000
5 | 2023-03-25T13:45:26.987654321
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S.%f"))
table_with_timestamp = table_with_datetime.select(
timestamp=table_with_datetime.t1.dt.timestamp()
)
pw.debug.compute_and_print(table_with_timestamp, include_id=False)
table = pw.debug.table_from_markdown(
'''
| t1
1 | 1969-01-01T00:00:00.000000000+00:00
2 | 1970-01-01T00:00:00.000000000+00:00
3 | 1970-01-01T00:00:00.000000000+02:00
4 | 1970-01-01T00:00:00.000000000-03:00
5 | 2023-01-01T00:00:00.000000000+01:00
6 | 2023-03-25T00:00:00.000000000+01:00
7 | 2023-03-25T13:45:26.000000000+01:00
8 | 2023-03-25T13:45:26.987654321+01:00
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S.%f%z"))
table_with_timestamp = table_with_datetime.select(
timestamp=table_with_datetime.t1.dt.timestamp()
)
pw.debug.compute_and_print(table_with_timestamp, include_id=False)
to_naive_in_timezone(timezone)
Converts DateTimeUtc to time zone specified as timezone argument.- Parameters
timezone (Union
[ColumnExpression
,str
]) – The time zone to convert to. - Returns
DateTimeNaive
Examples:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| date_utc
1 | 2023-03-26T00:59:00+00:00
2 | 2023-03-26T01:00:00+00:00
3 | 2023-03-27T00:59:00+00:00
4 | 2023-03-27T01:00:00+00:00
5 | 2023-10-28T23:59:00+00:00
6 | 2023-10-29T00:00:00+00:00
7 | 2023-10-29T00:30:00+00:00
8 | 2023-10-29T01:00:00+00:00
9 | 2023-10-29T01:30:00+00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S%z"
table_utc = table.select(date_utc=pw.this.date_utc.dt.strptime(fmt=fmt))
table_local = table_utc.with_columns(
date=pw.this.date_utc.dt.to_naive_in_timezone(timezone="Europe/Warsaw"),
)
pw.debug.compute_and_print(table_local, include_id=False)
table = pw.debug.table_from_markdown(
'''
| date_utc
1 | 2023-03-12T09:59:00+00:00
2 | 2023-03-12T10:00:00+00:00
3 | 2023-03-13T09:59:00+00:00
4 | 2023-03-13T10:00:00+00:00
5 | 2023-11-05T07:59:00+00:00
6 | 2023-11-05T08:00:00+00:00
7 | 2023-11-05T08:30:00+00:00
8 | 2023-11-05T09:00:00+00:00
9 | 2023-11-05T09:30:00+00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S%z"
table_utc = table.select(date_utc=pw.this.date_utc.dt.strptime(fmt=fmt))
table_local = table_utc.with_columns(
date=pw.this.date_utc.dt.to_naive_in_timezone(timezone="America/Los_Angeles"),
)
pw.debug.compute_and_print(table_local, include_id=False)
to_utc(from_timezone)
Converts DateTimeNaive to UTC from time zone provided as from_timezone argument. If the given DateTime doesn’t exist in the provided time zone it is mapped to the first existing DateTime after it. If a given DateTime corresponds to more than one moments in the provided time zone, it is mapped to a later moment.- Parameters
from_timezone (Union
[ColumnExpression
,str
]) – The time zone to convert from. - Returns
DateTimeUtc
Examples:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| date
1 | 2023-03-26T01:59:00
2 | 2023-03-26T02:30:00
3 | 2023-03-26T03:00:00
4 | 2023-03-27T01:59:00
5 | 2023-03-27T02:30:00
6 | 2023-03-27T03:00:00
7 | 2023-10-29T01:59:00
8 | 2023-10-29T02:00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
table_local = table.select(date=pw.this.date.dt.strptime(fmt=fmt))
table_utc = table_local.with_columns(
date_utc=pw.this.date.dt.to_utc(from_timezone="Europe/Warsaw"),
)
pw.debug.compute_and_print(table_utc, include_id=False)
table = pw.debug.table_from_markdown(
'''
| date
1 | 2023-03-12T01:59:00
2 | 2023-03-12T02:30:00
3 | 2023-03-12T03:00:00
4 | 2023-03-13T01:59:00
5 | 2023-03-13T02:30:00
6 | 2023-03-13T03:00:00
7 | 2023-11-05T00:59:00
8 | 2023-11-05T01:00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
table_local = table.select(date=pw.this.date.dt.strptime(fmt=fmt))
table_utc = table_local.with_columns(
date_utc=pw.this.date.dt.to_utc(from_timezone="America/Los_Angeles"),
)
pw.debug.compute_and_print(table_utc, include_id=False)
weeks()
The total number of weeks in a Duration.- Returns
Weeks as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1 | t2
0 | 2023-03-15T00:00:00 | 2023-05-15T10:13:23
1 | 2023-04-15T00:00:00 | 2023-05-15T10:00:00
2 | 2023-05-01T10:00:00 | 2023-05-15T10:00:00
3 | 2023-05-15T10:00:00 | 2023-05-15T09:00:00
4 | 2023-05-15T10:00:00 | 2023-05-15T11:00:00
5 | 2023-05-16T12:13:00 | 2023-05-15T10:00:00
6 | 2024-05-15T14:13:23 | 2023-05-15T10:00:00
'''
)
fmt = "%Y-%m-%dT%H:%M:%S"
table_with_datetimes = table.select(
t1=pw.this.t1.dt.strptime(fmt=fmt), t2=pw.this.t2.dt.strptime(fmt=fmt)
)
table_with_diff = table_with_datetimes.select(diff=pw.this.t1 - pw.this.t2)
table_with_weeks = table_with_diff.select(weeks=pw.this["diff"].dt.weeks())
pw.debug.compute_and_print(table_with_weeks, include_id=False)
year()
Extracts year from a DateTime.- Returns
Year as int.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| t1
1 | 1974-03-12T00:00:00
2 | 2023-03-25T12:00:00
3 | 2023-05-15T14:13:00
'''
)
table_with_datetime = table.select(t1=table.t1.dt.strptime("%Y-%m-%dT%H:%M:%S"))
table_with_years = table_with_datetime.select(year=table_with_datetime.t1.dt.year())
pw.debug.compute_and_print(table_with_years, include_id=False)
class pw.NumericalNamespace(expression)
Typical use:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| v
1 | -1
'''
)
table_abs = table.select(v_abs=table.v.num.abs())
abs()
Returns the absolute value from a numerical value.- Returns
Absolute value as float
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| v
1 | 1
2 | -1
3 | 2.5
4 | -2.5
'''
)
table_abs = table.select(v_abs=table.v.num.abs())
pw.debug.compute_and_print(table_abs, include_id=False)
fill_na(default_value)
Fill the missing values (None or NaN) in a column of a table with a specified default value.- Parameters
default_value (float) – The value to fill in for the missing values. - Returns
A new column with the missing values filled with the specified default value.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| v
1 | 1
2 | 2.0
3 | None
4 | 3.5
'''
)
table_fill_na = table.select(v_filled=table.v.num.fill_na(0))
pw.debug.compute_and_print(table_fill_na, include_id=False)
round(decimals=0)
Round the values in a column of a table to the specified number of decimals.- Parameters
- decimals (
Union
[ColumnExpression
,int
]) – The number of decimal places to round to. It can be either an - 0. (integer* or *a reference to another column. Defaults to) –
- decimals (
- Returns
A new column with the values rounded to the specified number of decimals.
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| v
1 | -2.18
2 | -1.11
3 | 1
4 | 2.1
5 | 3.14
6 | 4.17
'''
)
table_round = table.select(v_round=table.v.num.round(1))
pw.debug.compute_and_print(table_round, include_id=False)
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| v | precision
1 | 3 | 0
2 | 3.1 | 1
3 | 3.14 | 1
4 | 3.141 | 2
5 | 3.1415 | 2
'''
)
table_round = table.select(v_round=table.v.num.round(pw.this.precision))
pw.debug.compute_and_print(table_round, include_id=False)
class pw.StringNamespace(expression)
Typical use:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | ALICE
'''
)
table += table.select(name_lower=table.name.str.lower())
pw.debug.compute_and_print(table, include_id=False)
count(sub, start=None, end=None)
Returns the number of non-overlapping occurrences of substring sub in the range \[start, end). Optional arguments start and end are interpreted as in slice notation.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Hello
3 | World
4 | Zoo
'''
)
table += table.select(count=table.name.str.count("o"))
pw.debug.compute_and_print(table, include_id=False)
endswith(suffix)
Returns True if the string ends with suffix.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(ends_with_e=table.name.str.endswith("e"))
pw.debug.compute_and_print(table, include_id=False)
find(sub, start=None, end=None)
Return the lowest index in the string where substring sub is found within the slice s\[start:end\]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Hello
3 | World
4 | Zoo
'''
)
table += table.select(pos=table.name.str.find("o"))
pw.debug.compute_and_print(table, include_id=False)
len()
Returns the length of a string.- Returns
Length of the string
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(length=table.name.str.len())
pw.debug.compute_and_print(table, include_id=False)
lower()
Returns a lowercase copy of a string.- Returns
Lowercase string
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(name_lower=table.name.str.lower())
pw.debug.compute_and_print(table, include_id=False)
parse_bool(true_values=['on', 'true', 'yes', '1'], false_values=['off', 'false', 'no', '0'], optional=False)
Parses the string to bool, by checking if given string is either in true_values or false_values. The given string and all values in true_vales and false_values are made lowercase, so parsing is case insensitive.When true_values and false_values arguments are not provided, strings “True”, “On”, “1” and “Yes” are interpreted as True value, and “False”, “Off”, “0”, and “No” are interpreted as False.
If true_values or false_values is provided, then these values are mapped to respectively True and False, while all other either raise an exception or return None, depending on argument optional.
If optional argument is set to True, then the return type is Optional[bool] and if some string cannot be parsed, None is returned.
Example:
import pathway as pw
import pandas as pd
df = pd.DataFrame({"a": ["0", "TRUE", "on"]}, dtype=str)
table = pw.debug.table_from_pandas(df)
table.schema.as_dict()
pw.debug.compute_and_print(table, include_id=False)
table = table.select(a=table.a.str.parse_bool())
table.schema.as_dict()
pw.debug.compute_and_print(table, include_id=False)
parse_float(optional=False)
Parses the string to float. If optional argument is set to True, then the return type is Optional\[float\] and if some string cannot be parsed, None is returned.Example:
import pathway as pw
import pandas as pd
df = pd.DataFrame({"a": ["-5", "0.1", "200.999"]}, dtype=str)
table = pw.debug.table_from_pandas(df)
table.schema.as_dict()
table = table.select(a=table.a.str.parse_float())
table.schema.as_dict()
pw.debug.compute_and_print(table, include_id=False)
parse_int(optional=False)
Parses the string to int. If optional argument is set to True, then the return type is Optional\[int\] and if some string cannot be parsed, None is returned.Example:
import pathway as pw
import pandas as pd
df = pd.DataFrame({"a": ["-5", "0", "200"]}, dtype=str)
table = pw.debug.table_from_pandas(df)
table.schema.as_dict()
table = table.select(a=table.a.str.parse_int())
table.schema.as_dict()
pw.debug.compute_and_print(table, include_id=False)
removeprefix(prefix, /)
If the string starts with prefix, returns a copy of the string without the prefix. Otherwise returns the original string.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(without_da=table.name.str.removeprefix("da"))
pw.debug.compute_and_print(table, include_id=False)
table = pw.debug.table_from_markdown(
'''
| note | prefix
1 | AAA | A
2 | BB | B
'''
)
table = table.select(
pw.this.note,
new_note=pw.this.note.str.removeprefix(pw.this.prefix)
)
pw.debug.compute_and_print(table, include_id=False)
removesuffix(suffix, /)
If the string ends with suffix, returns a copy of the string without the suffix. Otherwise returns the original string.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(without_LE=table.name.str.removesuffix("LE"))
pw.debug.compute_and_print(table, include_id=False)
table = pw.debug.table_from_markdown(
'''
| fruit | suffix
1 | bamboo | o
2 | banana | na
'''
)
table = table.select(
pw.this.fruit,
fruit_cropped=pw.this.fruit.str.removesuffix(pw.this.suffix)
)
pw.debug.compute_and_print(table, include_id=False)
replace(old_value, new_value, count=-1, /)
Returns the a string where the occurrences of the old_value substrings arereplaced by the new_value substring.
- Parameters
count (Union
[ColumnExpression
,int
]) – Maximum number of occurrences to replace. When set to -1, replaces all occurrences. Defaults to -1. - Returns
The new string where old_value is replaced by new_value
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
5 | Edward
'''
)
table += table.select(name_replace=table.name.str.replace("d","Z"))
pw.debug.compute_and_print(table, include_id=False)
table = pw.debug.table_from_markdown(
'''
| value | old | new | count
1 | Scaciscics | c | t | 3
2 | yelliwwiid | i | o | 2
'''
)
table = table.select(
pw.this.value,
value_replace=pw.this.value.str.replace(
pw.this.old, pw.this.new, pw.this.count
)
)
pw.debug.compute_and_print(table, include_id=False)
reversed()
Returns a reverse copy of a string.- Returns
Reverse string
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(name_reverse=table.name.str.reversed())
pw.debug.compute_and_print(table, include_id=False)
rfind(sub, start=None, end=None)
Return the highest index in the string where substring sub is found within the slice s\[start:end\]. Optional arguments start and end are interpreted as in slice notation. Return -1 if sub is not found.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Hello
3 | World
4 | Zoo
'''
)
table += table.select(pos=table.name.str.rfind("o"))
pw.debug.compute_and_print(table, include_id=False)
slice(start, end, /)
Return a slice of the string.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(slice=table.name.str.slice(1,4))
pw.debug.compute_and_print(table, include_id=False)
startswith(prefix)
Returns True if the string starts with prefix.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(starts_with_A=table.name.str.startswith("A"))
pw.debug.compute_and_print(table, include_id=False)
strip(chars=None)
Returns a copy of the string with specified leading and trailing characters removed. If no arguments are passed, remove the leading and trailing whitespaces.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(name_strip=table.name.str.strip("Aod"))
pw.debug.compute_and_print(table, include_id=False)
swapcase()
Returns a copy of the string where the case is inverted.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(name_swap=table.name.str.swapcase())
pw.debug.compute_and_print(table, include_id=False)
title()
Returns a copy of the string where where words start with an uppercase character and the remaining characters are lowercase.Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| col
1 | title
'''
)
table = table.select(col_title=table.col.str.title())
pw.debug.compute_and_print(table, include_id=False)
upper()
Returns a uppercase copy of a string.- Returns
Uppercase string
Example:
import pathway as pw
table = pw.debug.table_from_markdown(
'''
| name
1 | Alice
2 | Bob
3 | CAROLE
4 | david
'''
)
table += table.select(name_upper=table.name.str.upper())
pw.debug.compute_and_print(table, include_id=False)