fenic.api.functions.dt
Date and time functions.
Functions:
-
current_date
–Get the current date.
-
current_timestamp
–Get the current date and time.
-
date_add
–Adds the number of days to the date/timestamp column.
-
date_format
–Formats a date/timestamp column to a given format.
-
date_sub
–Subtracts the number of days from the date/timestamp column.
-
date_trunc
–Truncate a date to a given unit.
-
datediff
–Calculates the number of days between two date/timestamp columns.
-
day
–Extract the day from a day column.
-
from_utc_timestamp
–Accepts a Column with [TimestampType] (UTC). For each row, converts the timestamp value to the provided
tz
timezone, then renders that timestamp as UTC without changing the timestamp value. In other words, this function shifts the timestamp by the timezone offsetout = t+offset(t+tz)
. -
hour
–Extract the hour from a day column.
-
millisecond
–Extract the hour from a millisecond column.
-
minute
–Extract the minute from a day column.
-
month
–Extract the month from a month column.
-
now
–Get the current date and time.
-
second
–Extract the hour from a second column.
-
timestamp_add
–Adds the quantity of the given unit to the timestamp column.
-
timestamp_diff
–Calculates the difference between two timestamp columns.
-
to_date
–Transform a string into a DateType.
-
to_timestamp
–Transform a string into a TimestampType.
-
to_utc_timestamp
–Accepts a Column with [TimestampType] (UTC), interprets each value as wall-clock time in the specified timezone
tz
, and converts it to a timestamp in UTC. -
year
–Extract the year from a date column.
current_date
current_date() -> Column
Get the current date.
Returns:
Example
df.select(dt.current_date().alias("cur_date")).to_pydict()
# Output: {'cur_date': [datetime.date(2025, 9, 26)]}
Source code in src/fenic/api/functions/dt.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
current_timestamp
current_timestamp() -> Column
Get the current date and time.
Returns:
-
Column
–A Column object with the current date and time.
-
Column
–The type of the column is TimestampType in UTC timezone.
Example
df.select(dt.current_timestamp().alias("cur_ts")).to_pydict()
# Output: {'cur_ts': [datetime.datetime(2025, 9, 26, 10, 0)]}
Source code in src/fenic/api/functions/dt.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
date_add
date_add(column: ColumnOrName, days: Union[int, ColumnOrName]) -> Column
Adds the number of days to the date/timestamp column.
Parameters:
-
column
(ColumnOrName
) –The column to add the days to.
-
days
(Union[int, ColumnOrName]
) –The number of days to add to the date/timestamp column. If the days is negative, the days will be subtracted.
Returns:
-
Column
–A Column object with the date/timestamp column with the days added.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType, or if days is not an IntegerType.
Example
# dates: "2025-01-01", "2025-02-01", "2025-03-01"]
df.select(dt.date_add(col("date"), 1).alias("date_add")).to_pydict()
# Output: {'date_add': ['2025-01-02', '2025-02-02', '2025-03-02']}
Source code in src/fenic/api/functions/dt.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
date_format
date_format(column: ColumnOrName, format: str) -> Column
Formats a date/timestamp column to a given format.
Parameters:
-
column
(ColumnOrName
) –The column to format.
-
format
(str
) –The format to format the column to.
Returns:
-
Column
–A Column object with the date/timestamp column formatted into a string.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Notes
- The accepted formats should follow this pattern: https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html
Example
# ts: "2025-01-01 10:00:00", "2025-02-01 11:00:00", "2025-03-01 15:00:00"]
df.select(dt.date_format(col("date"), "MM-dd-yyyy hh:mm:ss a").alias("date")).to_pydict()
# Output: {'date': ['01-01-2025 10:00:00 AM', '02-01-2025 11:00:00 AM', '03-01-2025 03:00:00 PM']}
Source code in src/fenic/api/functions/dt.py
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
|
date_sub
date_sub(column: ColumnOrName, days: Union[int, ColumnOrName]) -> Column
Subtracts the number of days from the date/timestamp column.
Parameters:
-
column
(ColumnOrName
) –The column to subtract the days from.
-
days
(Union[int, ColumnOrName]
) –The amount of days to subtract. If the days is negative, the days will be added.
Returns:
-
Column
–A Column object with the date/timestamp column with the days substracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType, or if days is not an IntegerType.
Example
# dates: "2025-01-01", "2025-02-01", "2025-03-01"]
df.select(dt.date_sub(col("date"), 1).alias("date_sub")).to_pydict()
# Output: {'date_sub': ['2024-12-31', '2025-01-31', '2025-02-28']}
Source code in src/fenic/api/functions/dt.py
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
|
date_trunc
date_trunc(column: ColumnOrName, unit: DateTimeUnit) -> Column
Truncate a date to a given unit.
Parameters:
-
column
(ColumnOrName
) –The column to truncate.
-
unit
(DateTimeUnit
) –The unit to truncate to.
Returns:
-
Column
–A Column object with the date truncated.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
-
ValueError
–If unit is not supported, must be one of the supported ones.
Notes
The supported units are: "year", "month", "day", "hour", "minute", "second", "millisecond".
Example
# dates: "2025-01-01", "2025-02-01", "2025-03-01"]
df.select(dt.date_trunc(col("date"), "year").alias("date_trunc")).to_pydict()
# Output: {'date_trunc': ['2025-01-01', '2025-01-01', '2025-01-01']}
Source code in src/fenic/api/functions/dt.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
datediff
datediff(end: ColumnOrName, start: ColumnOrName) -> Column
Calculates the number of days between two date/timestamp columns.
Parameters:
-
end
(ColumnOrName
) –To date column to work on.
-
start
(ColumnOrName
) –From date column to work on.
Returns:
-
Column
–A Column object with the difference in days between the two date/timestamp columns.
Example
# end: "2025-01-01", "2025-02-02", "2025-03-06"]
# start: "2025-01-02", "2025-02-01", "2025-03-02"]
df.select(dt.datediff(col("end"), col("start")).alias("date_diff")).to_pydict()
# Output: {'date_diff': [-1, 1, 4]}
Source code in src/fenic/api/functions/dt.py
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
|
day
day(column: ColumnOrName) -> Column
Extract the day from a day column.
Parameters:
-
column
(ColumnOrName
) –The column to extract the day from.
Returns:
-
Column
–A Column object with the day extracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Example
# dates: "2025-01-01", "2025-01-02", "2025-01-03"]
df.select(dt.day(col("date"))).to_pydict()
# Output: [{'day': 1}, {'day': 2}, {'day': 3}]
Source code in src/fenic/api/functions/dt.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
from_utc_timestamp
from_utc_timestamp(column: ColumnOrName, tz: str) -> Column
Accepts a Column with [TimestampType] (UTC). For each row, converts the timestamp value to the provided tz
timezone, then renders that timestamp as UTC without changing the timestamp value. In other words, this function shifts the timestamp by the timezone offset out = t+offset(t+tz)
.
Parameters:
-
column
(ColumnOrName
) –The column containing the timestamp.
-
tz
(str
) –A timezone that the input will be converted from.
Returns:
-
Column
–A Column object with timestamp expressed in UTC.
Raises:
-
TypeError
–If column type is not a TimestampType.
Notes
- In fenic, the TimestampType data type is always in UTC, which is not timezone-agnostic.
- Spark also represents all timestamps as not timezone-agnostic, except Spark uses a timestamp type with the local session timezone.
- Similarly to Spark from_utc_timestamp function, this function will take a UTC timestamp and convert it to the requested timezone, then represent it as a timestamp in the session (UTC) timezone.
- nulls are preserved.
- Use when applying
tz
to timestamps in UTC and the resulting wall‑clock timestamp is required (though the result is still stored with a UTC timezone). - Use to_utc_timestamp for the inverse (treats naive/local values in tz, then converts to UTC).
- see Spark documentation for more details: https://spark.apache.org/docs/4.0.1/api/python/reference/pyspark.sql/api/pyspark.sql.functions.from_utc_timestamp.html#
Example
df.select("timestamp", dt.to_timestamp(col("timestamp"), "yyyy-MM-dd HH:mm:ss").alias("utc_time")).show()
# Output:
#┌─────────────────────┬─────────────────────────┐
#│ timestamp ┆ utc_time │
#╞═════════════════════╪═════════════════════════╡
#│ 2025-01-15 10:30:00 ┆ 2025-01-15 10:30:00 UTC │
#│ 2025-01-16 14:00:00 ┆ 2025-01-16 14:00:00 UTC │
#│ 2025-01-17 18:45:00 ┆ 2025-01-17 18:45:00 UTC │
#└─────────────────────┴─────────────────────────┘
#
df.select(dt.from_utc_timestamp(col("utc_time"), "America/Los_Angeles").alias("la_time_in_utc")).show()
# Output:
#┌─────────────────────────┐
#│ la_time_in_utc │
#╞═════════════════════════╡
#│ 2025-01-15 02:30:00 UTC │
#│ 2025-01-16 06:00:00 UTC │
#│ 2025-01-17 10:45:00 UTC │
#└─────────────────────────┘
Source code in src/fenic/api/functions/dt.py
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
hour
hour(column: ColumnOrName) -> Column
Extract the hour from a day column.
Parameters:
-
column
(ColumnOrName
) –The column to extract the hour from.
Returns:
-
Column
–A Column object with the hour extracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Notes
This will return 0 for DateType columns.
Example
# ts: "2025-01-01 10:00:00", "2025-01-02 11:00:00", "2025-01-03 12:00:00"]
df.select(dt.hour(col("ts"))).to_pydict()
# Output: [{'hour': 10}, {'hour': 11}, {'hour': 12}]
Source code in src/fenic/api/functions/dt.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
|
millisecond
millisecond(column: ColumnOrName) -> Column
Extract the hour from a millisecond column.
Parameters:
-
column
(ColumnOrName
) –The column to extract the millisecond from.
Returns:
-
Column
–A Column object with the millisecond extracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Notes
This will return 0 for DateType columns.
Example
# ts: "2025-01-01 10:10:01.123", "2025-01-02 11:11:02.234", "2025-01-03 12:12:03.345"]
df.select(dt.millisecond(col("ts"))).to_pydict()
# Output: [{'millisecond': 123}, {'millisecond': 234}, {'millisecond': 345}]
Source code in src/fenic/api/functions/dt.py
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
|
minute
minute(column: ColumnOrName) -> Column
Extract the minute from a day column.
Parameters:
-
column
(ColumnOrName
) –The column to extract the minute from.
Returns:
-
Column
–A Column object with the minute extracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Notes
This will return 0 for DateType columns.
Example
# ts: "2025-01-01 10:10:00", "2025-01-02 11:11:00", "2025-01-03 12:12:00"]
df.select(dt.minute(col("ts"))).to_pydict()
# Output: [{'minute': 10}, {'minute': 11}, {'minute': 12}]
Source code in src/fenic/api/functions/dt.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
|
month
month(column: ColumnOrName) -> Column
Extract the month from a month column.
Parameters:
-
column
(ColumnOrName
) –The column to extract the month from.
Returns:
-
Column
–A Column object with the month extracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Example
# dates: "2025-01-01", "2025-01-02", "2024-12-03"]
df.select(dt.month(col("date"))).to_pydict()
# Output: [{'month': 1}, {'month': 1}, {'month': 12}]
Source code in src/fenic/api/functions/dt.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
|
now
now() -> Column
Get the current date and time.
Returns:
-
Column
–A Column object with the current date and time.
-
Column
–The type of the column is TimestampType.
Example
df.select(dt.now()).to_pydict()
# Output: [{'date': '<current date and time>'}]
Source code in src/fenic/api/functions/dt.py
258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|
second
second(column: ColumnOrName) -> Column
Extract the hour from a second column.
Parameters:
-
column
(ColumnOrName
) –The column to extract the second from.
Returns:
-
Column
–A Column object with the second extracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Notes
This will return 0 for DateType columns.
Example
# ts: "2025-01-01 10:10:01", "2025-01-02 11:11:02", "2025-01-03 12:12:03"]
df.select(dt.second(col("ts"))).to_pydict()
# Output: [{'second': 1}, {'second': 2}, {'second': 3}]
Source code in src/fenic/api/functions/dt.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
timestamp_add
timestamp_add(column: ColumnOrName, quantity: Union[int, ColumnOrName], unit: DateTimeUnit) -> Column
Adds the quantity of the given unit to the timestamp column.
Parameters:
-
column
(ColumnOrName
) –The column to add the quantity to.
-
quantity
(Union[int, ColumnOrName]
) –The quantity to add. If the quantity is negative, the quantity will be subtracted.
-
unit
(DateTimeUnit
) –The unit of the quantity.
Returns:
-
Column
–A Column object with the timestamp column with the quantity added.
Raises:
-
TypeError
–If column type is not a TimestampType, or if quantity is not an IntegerType.
-
ValueError
–If unit is not supported, must be one of the supported ones.
Notes
The supported units are: "year", "month", "day", "hour", "minute", "second", "millisecond".
Example
# ts: "2025-01-01 10:00:00", "2025-02-01 11:00:00", "2025-03-01 12:00:00"]
df.select(dt.timestamp_add(col("ts"), 1, "day").alias("ts_add")).to_pydict()
# Output: {'ts_add': ['2025-01-02 10:00:00', '2025-02-02 11:00:00', '2025-03-02 12:00:00']}
Source code in src/fenic/api/functions/dt.py
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
|
timestamp_diff
timestamp_diff(start: ColumnOrName, end: ColumnOrName, unit: DateTimeUnit) -> Column
Calculates the difference between two timestamp columns.
Parameters:
-
start
(ColumnOrName
) –The first column to calculate the difference from.
-
end
(ColumnOrName
) –The second column to calculate the difference from.
-
unit
(DateTimeUnit
) –The unit of the difference.
Returns:
-
Column
–A Column object with the difference in the given unit between the two timestamp columns.
Raises:
-
ValueError
–If unit is not supported, must be one of the supported ones.
Notes
The supported units are: "year", "month", "day", "hour", "minute", "second", "millisecond".
Example
# start: "2025-01-01 10:00:00", "2025-02-02 11:00:00", "2025-03-06 12:00:00"]
# end: "2025-01-02 10:00:00", "2025-02-01 11:00:00", "2025-03-01 12:00:00"]
df.select(dt.timestamp_diff(col("start"), col("end"), "day").alias("ts_diff")).to_pydict()
# Output: {'ts_diff': [-1, 1, 5]}
Source code in src/fenic/api/functions/dt.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
|
to_date
to_date(column: ColumnOrName, format: Optional[str] = None) -> Column
Transform a string into a DateType.
Parameters:
-
column
(ColumnOrName
) –The column to transform into a DateType.
-
format
(Optional[str]
, default:None
) –The format of the date string.
Returns:
-
Column
–A Column object with the DateType transformed.
Raises:
-
TypeError
–If column type is not a StringType.
Notes
- If format is not provided, the default format is "YYYY-MM-DD".
- The accepted formats should follow this pattern: https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html
Example
# date_str: "11-01-2025", "12-02-2025", "01-03-2025"]
df.select(to_date(col("date_str"), format="MM-dd-yyyy").alias("date")).to_pydict()
# Output: {'date': [datetime.datetime(2025, 11, 1, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC')), datetime.datetime(2025, 12, 2, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC')), datetime.datetime(2025, 1, 3, 0, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))]}
Source code in src/fenic/api/functions/dt.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
|
to_timestamp
to_timestamp(column: ColumnOrName, format: Optional[str] = None) -> Column
Transform a string into a TimestampType.
Parameters:
-
column
(ColumnOrName
) –The column to transform into a TimestampType.
-
format
(Optional[str]
, default:None
) –The format of the timestamp string.
Returns:
-
Column
–A Column object with the
TimestampType
type, with a UTC timezone. If the providedformat
contains a timezone specifier, the result timestamp value will be converted from theformat
timezone to UTC.
Raises:
-
TypeError
–If column type is not a StringType.
Notes
- If format is not provided, the default format is ISO 8601 with milliseconds.
- The accepted formats should follow this pattern: https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html
Example
# date_str: ["11-01-2025 10:00:00", "12-02-2025 11:00:00", "01-03-2025 12:00:00"]
df.select(dt.to_date(col("date_str"), format="MM-dd-yyyy HH:mm:ss").alias("timestamp")).to_pydict()
# Output: {'timestamp': [datetime.datetime(2025, 11, 1, 10, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC')), datetime.datetime(2025, 11, 1, 10, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC')), datetime.datetime(2025, 11, 1, 10, 0, tzinfo=zoneinfo.ZoneInfo(key='UTC'))]}
Source code in src/fenic/api/functions/dt.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
to_utc_timestamp
to_utc_timestamp(column: ColumnOrName, tz: str) -> Column
Accepts a Column with [TimestampType] (UTC), interprets each value as wall-clock time in the specified timezone tz
, and converts it to a timestamp in UTC.
Parameters:
-
column
(ColumnOrName
) –The column containing the timestamp. Will be treated as timezone-agnostic.
-
tz
(str
) –A timezone that the input should be converted to.
Returns:
-
Column
–A Column object with timestamp expressed in UTC.
Raises:
-
TypeError
–If column type is not a TimestampType.
Notes
- In fenic, the TimestampType data type is always in UTC, which is not timezone-agnostic.
- Spark also represents all timestamps as not timezone-agnostic, except Spark uses a timestamp type with the local session timezone.
- Similarly to Spark to_utc_timestamp function, this function treats the input timestamp as timezone-agnostic, sets it to the requested timezone (without changing the timestamp), then converts the timestamp to UTC.
- nulls are preserved.
- Use this when data contains local/wall‑clock timestamps from
tz
(ignoring the UTC timezone in the type), and timestamp values converted to UTC are required. - For the inverse operation (UTC → local wall‑clock, then re‑expressed in UTC), see from_utc_timestamp.
- see Spark documentation for more details: https://spark.apache.org/docs/4.0.1/api/python/reference/pyspark.sql/api/pyspark.sql.functions.to_utc_timestamp.html
Example
df.select("timestamp", dt.to_timestamp(col("timestamp"), "yyyy-MM-dd HH:mm:ss").alias("la_time")).show()
# Output:
#┌─────────────────────┬─────────────────────────┐
#│ timestamp ┆ la_time │
#╞═════════════════════╪═════════════════════════╡
#│ 2025-01-15 10:30:00 ┆ 2025-01-15 10:30:00 UTC │
#│ 2025-01-16 14:00:00 ┆ 2025-01-16 14:00:00 UTC │
#│ 2025-01-17 18:45:00 ┆ 2025-01-17 18:45:00 UTC │
#└─────────────────────┴─────────────────────────┘
#
df.select(dt.to_utc_timestamp(col("la_time"), "America/Los_Angeles").alias("la_time_to_utc")).show()
# Output:
#┌─────────────────────────┐
#│ la_time_to_utc │
#╞═════════════════════════╡
#│ 2025-01-15 18:30:00 UTC │
#│ 2025-01-16 22:00:00 UTC │
#│ 2025-01-18 02:45:00 UTC │
#└─────────────────────────┘
Source code in src/fenic/api/functions/dt.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
|
year
year(column: ColumnOrName) -> Column
Extract the year from a date column.
Parameters:
-
column
(ColumnOrName
) –The column to extract the year from.
Returns:
-
Column
–A Column object with the year extracted.
Raises:
-
TypeError
–If column type is not a DateType or TimestampType.
Example
# dates: "2025-01-01", "2025-01-02", "2025-01-03"]
df.select(dt.year(col("date"))).to_pydict()
# Output: [{'year': 2025}, {'year': 2025}, {'year': 2025}]
Source code in src/fenic/api/functions/dt.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|