DateTime
toolbox_pyspark.datetime
🔗
Summary
The datetime
module is used for fixing column names that contain datetime data, adding conversions to local datetimes, and for splitting a column in to their date and time components.
rename_datetime_column
🔗
rename_datetime_column(
dataframe: psDataFrame, column: str
) -> psDataFrame
Summary
For a given column in a Data Frame, if there is not another column existing that has TIME
appended to the end, then re-name the column to append TIME
to it.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The DataFrame to update. |
required |
column
|
str
|
The column to check. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated Data Frame. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
+---+---+---------------------+---------------------+
| a | b | c_date | d_date |
+---+---+---------------------+---------------------+
| 0 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 |
| 1 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 |
| 2 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 |
| 3 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 |
+---+---+---------------------+---------------------+
Example 1: Update column | |
---|---|
1 |
|
+---+---+---------------------+---------------------+
| a | b | c_dateTIME | d_date |
+---+---+---------------------+---------------------+
| 0 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 |
| 1 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 |
| 2 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 |
| 3 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 |
+---+---+---------------------+---------------------+
Conclusion: Successfully renamed column.
Example 2: Missing column | |
---|---|
1 |
|
ColumnDoesNotExistError: Column "fff" does not exist in "dataframe".
Try one of: ["a", "b", "c_date", "d_date"].
Conclusion: Column does not exist.
Source code in src/toolbox_pyspark/datetime.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
|
rename_datetime_columns
🔗
rename_datetime_columns(
dataframe: psDataFrame,
columns: Optional[Union[str_collection, str]] = None,
) -> psDataFrame
Summary
Fix the column names for the date-time columns.
Details
This is necessary because in NGW, there are some columns which have datetime
data types, but which have the name only containing date
.
So, this function will fix that.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The DataFrame to update. |
required |
columns
|
(Optional[Union[str_collection, str]], None)
|
An optional list of columns to update.
If this is not provided, or is the value
Default: |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If any of the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated DataFrame. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
+---+---+---------------------+---------------------+
| a | b | c_date | d_date |
+---+---+---------------------+---------------------+
| 0 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 |
| 1 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 |
| 2 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 |
| 3 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 |
+---+---+---------------------+---------------------+
Example 1: One column | |
---|---|
1 |
|
+---+---+---------------------+---------------------+
| a | b | c_dateTIME | d_date |
+---+---+---------------------+---------------------+
| 0 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 |
| 1 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 |
| 2 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 |
| 3 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 |
+---+---+---------------------+---------------------+
Conclusion: Successfully renamed column.
Example 2: One column `str` | |
---|---|
1 |
|
+---+---+---------------------+---------------------+
| a | b | c_dateTIME | d_date |
+---+---+---------------------+---------------------+
| 0 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 |
| 1 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 |
| 2 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 |
| 3 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 |
+---+---+---------------------+---------------------+
Conclusion: Successfully renamed column.
Example 3: All columns | |
---|---|
1 |
|
+---+---+---------------------+---------------------+
| a | b | c_dateTIME | d_dateTIME |
+---+---+---------------------+---------------------+
| 0 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 |
| 1 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 |
| 2 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 |
| 3 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 |
+---+---+---------------------+---------------------+
Conclusion: Successfully renamed columns.
Example 4: All columns using 'all' | |
---|---|
1 |
|
+---+---+---------------------+---------------------+
| a | b | c_dateTIME | d_dateTIME |
+---+---+---------------------+---------------------+
| 0 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 |
| 1 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 |
| 2 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 |
| 3 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 |
+---+---+---------------------+---------------------+
Conclusion: Successfully renamed columns.
Example 5: Missing column | |
---|---|
1 |
|
Attribute Error: Columns ["fff", "ggg"] do not exist in "dataframe".
Try one of: ["a", "b", "c_dateTIME", "d_dateTIME"].
Conclusion: Columns do not exist.
Source code in src/toolbox_pyspark/datetime.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 228 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 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 329 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 357 358 359 360 361 362 |
|
add_local_datetime_column
🔗
add_local_datetime_column(
dataframe: psDataFrame,
column: str,
from_timezone: Optional[str] = None,
column_with_target_timezone: str = "timezone_location".upper(),
) -> psDataFrame
Summary
For the given column
, add a new column with the suffix _LOCAL
which is a conversion of the datetime values from column
to the desired timezone.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The DataFrame to be fixed |
required |
column
|
str
|
The name of the column to do the conversion for. Must exist in |
required |
from_timezone
|
str
|
The timezone which will be converted from. Must be a valid TimeZoneID, for more info, see: TimeZoneID. |
None
|
column_with_target_timezone
|
str
|
The column containing the target timezone value. By default will be the column |
upper()
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If |
ValueError
|
If the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated DataFrame. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
| a | b | c | d | e | target | TIMEZONE_LOCATION |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
Example 1: Converting from UTC time | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+
| a | b | c | d | e | target | TIMEZONE_LOCATION | c_LOCAL |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-01 08:00:00 |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-02 08:00:00 |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-03 08:00:00 |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-04 08:00:00 |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+
Conclusion: Successfully converted from UTC.
Example 2: Converting from specific timezone, with custom column containing target timezone | |
---|---|
1 2 3 4 5 6 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| a | b | c | d | e | target | TIMEZONE_LOCATION | c_UTC | c_LOCAL |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth | 2021-12-31 13:00:00 | 2021-12-31 21:00:00 |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-01 13:00:00 | 2022-01-01 21:00:00 |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-02 13:00:00 | 2022-01-02 21:00:00 |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-03 13:00:00 | 2022-01-03 21:00:00 |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
Conclusion: Successfully converted timezone.
Example 3: Invalid column name | |
---|---|
1 |
|
ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
Try one of: ["a", "b", "c", "d", "e", "target", "TIMEZONE_LOCATION"].
Conclusion: Column does not exist.
Example 4: Invalid timezone | |
---|---|
1 |
|
ValueError: The timezone "Invalid/Timezone" is not a valid timezone.
Conclusion: Invalid timezone.
Notes
- If
from_timezone is None
, then it is assumed that the datetime data incolumn
is already in UTC timezone. - If
from_timezone is not None
, then a new column will be added with the syntax{column}_UTC
, then another column added with{column}_LOCAL
. This is necessary because PySpark cannot convert immediately from one timezone to another; it must first require a conversion from thefrom_timezone
value to UTC, then a second conversion from UTC to whichever timezone is defined in the columncolumn_with_target_timezone
. - The reason why this function uses multiple
.withColumn()
methods, instead of a single.withColumns()
expression is because to add the{column}_LOCAL
column, it is first necessary for the{column}_UTC
column to exist on thedataframe
. Therefore, we need to call.withColumn()
first to add{column}_UTC
, then we need to call.withColumn()
a second time to add{column}_LOCAL
.
Source code in src/toolbox_pyspark/datetime.py
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 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 418 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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 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 501 502 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 |
|
add_local_datetime_columns
🔗
add_local_datetime_columns(
dataframe: psDataFrame,
columns: Optional[Union[str, str_collection]] = None,
from_timezone: Optional[str] = None,
column_with_target_timezone: str = "timezone_location".upper(),
) -> psDataFrame
Summary
For each of the data
or datetime
columns in dataframe
, add a new column which is converting it to the timezone of the local datetime.
Details
Under the hood, this function will call add_local_datetime_column()
for each column
in columns
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The DataFrame to update. |
required |
columns
|
Optional[Union[str, str_collection]]
|
The columns to check. If not provided, it will use all of the columns which contains the text |
None
|
from_timezone
|
Optional[str]
|
The timezone which will be converted from. If not given, will default the from timezone to be UTC. |
None
|
column_with_target_timezone
|
str
|
The column containing the target timezone value. By default will be the column |
upper()
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If any of the |
ValueError
|
If the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated DataFrame. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
| a | b | c | d_datetime | e_datetime | target | TIMEZONE_LOCATION |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
Example 1: Default config | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| a | b | c | d_datetime | e_datetime | target | TIMEZONE_LOCATION | d_datetime_LOCAL | e_datetime_LOCAL |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth | 2022-02-01 08:00:00 | 2022-03-01 08:00:00 |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth | 2022-02-02 08:00:00 | 2022-03-02 08:00:00 |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth | 2022-02-03 08:00:00 | 2022-03-03 08:00:00 |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth | 2022-02-04 08:00:00 | 2022-03-04 08:00:00 |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
Conclusion: Successfully converted columns to local timezone.
Example 2: Semi-custom config | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| a | b | c | d_datetime | e_datetime | target | TIMEZONE_LOCATION | c_LOCAL | d_datetime_LOCAL |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-01 08:00:00 | 2022-02-01 08:00:00 |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-02 08:00:00 | 2022-02-02 08:00:00 |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-03 08:00:00 | 2022-02-03 08:00:00 |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-04 08:00:00 | 2022-02-04 08:00:00 |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
Conclusion: Successfully converted columns to local timezone.
Example 3: Full-custom config | |
---|---|
1 2 3 4 5 6 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
| a | b | c | d_datetime | e_datetime | target | TIMEZONE_LOCATION | c_UTC | c_LOCAL | d_datetime_UTC | d_datetime_LOCAL |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth | 2021-12-31 13:00:00 | 2021-12-31 21:00:00 | 2022-01-31 13:00:00 | 2022-02-01 08:00:00 |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-01 13:00:00 | 2022-01-01 21:00:00 | 2022-02-01 13:00:00 | 2022-02-02 08:00:00 |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-02 13:00:00 | 2022-01-02 21:00:00 | 2022-02-02 13:00:00 | 2022-02-03 08:00:00 |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-03 13:00:00 | 2022-01-03 21:00:00 | 2022-02-03 13:00:00 | 2022-02-04 08:00:00 |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
Conclusion: Successfully converted columns to local time zone, from other custom time zone.
Example 4: Single column | |
---|---|
1 2 3 4 5 6 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| a | b | c | d_datetime | e_datetime | target | TIMEZONE_LOCATION | c_UTC | c_LOCAL |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth | 2021-12-31 13:00:00 | 2021-12-31 21:00:00 |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-01 13:00:00 | 2022-01-01 21:00:00 |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-02 13:00:00 | 2022-01-02 21:00:00 |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-03 13:00:00 | 2022-01-03 21:00:00 |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
Conclusion: Successfully converted single column from other time zone to local time zone.
Example 5: All columns | |
---|---|
1 2 3 4 5 6 |
|
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
| a | b | c | d_datetime | e_datetime | target | TIMEZONE_LOCATION | d_datetime_UTC | d_datetime_LOCAL | e_datetime_UTC | e_datetime_LOCAL |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Asia/Singapore | Australia/Perth | 2022-01-31 13:00:00 | 2022-02-01 08:00:00 | 2022-02-28 13:00:00 | 2022-03-01 08:00:00 |
| 2 | b | 2022-01-02 00:00:00 | 2022-02-02 00:00:00 | 2022-03-02 00:00:00 | Asia/Singapore | Australia/Perth | 2022-02-01 13:00:00 | 2022-02-02 08:00:00 | 2022-03-01 13:00:00 | 2022-03-02 08:00:00 |
| 3 | c | 2022-01-03 00:00:00 | 2022-02-03 00:00:00 | 2022-03-03 00:00:00 | Asia/Singapore | Australia/Perth | 2022-02-02 13:00:00 | 2022-02-03 08:00:00 | 2022-03-02 13:00:00 | 2022-03-03 08:00:00 |
| 4 | d | 2022-01-04 00:00:00 | 2022-02-04 00:00:00 | 2022-03-04 00:00:00 | Asia/Singapore | Australia/Perth | 2022-02-03 13:00:00 | 2022-02-04 08:00:00 | 2022-03-03 13:00:00 | 2022-03-04 08:00:00 |
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
Conclusion: Successfully converted all date time columns from other time zone to local time zone.
Example 6: Invalid column name | |
---|---|
1 |
|
ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
Try one of: ["a", "b", "c", "d_datetime", "e_datetime", "target", "TIMEZONE_LOCATION"].
Conclusion: Column does not exist.
Example 7: Invalid timezone | |
---|---|
1 |
|
ValueError: The timezone "Invalid/Timezone" is not a valid timezone.
Conclusion: Invalid timezone.
See Also
Source code in src/toolbox_pyspark/datetime.py
551 552 553 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 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 |
|
split_datetime_column
🔗
split_datetime_column(
dataframe: psDataFrame, column: str
) -> psDataFrame
Summary
Take the column column
, which should be a timestamp
type, and split it in to it's respective date
and time
components.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The DataFrame to update. |
required |
column
|
str
|
The column to split. |
required |
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If the |
TypeError
|
If the |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated DataFrame. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
+---+---+---------------------+---------------------+---------------------+-------------------+
| a | b | c_datetime | d_datetime | e_datetime | TIMEZONE_LOCATION |
+---+---+---------------------+---------------------+---------------------+-------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Australia/Perth |
| 2 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 | 2022-03-01 01:00:00 | Australia/Perth |
| 3 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 | 2022-03-01 02:00:00 | Australia/Perth |
| 4 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 | 2022-03-01 03:00:00 | Australia/Perth |
+---+---+---------------------+---------------------+---------------------+-------------------+
Example 1: Default config | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
| a | b | c_datetime | d_datetime | e_datetime | TIMEZONE_LOCATION | C_DATE | C_TIME |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Australia/Perth | 2022-01-01 | 00:00:00 |
| 2 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 | 2022-03-01 01:00:00 | Australia/Perth | 2022-01-01 | 01:00:00 |
| 3 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 | 2022-03-01 02:00:00 | Australia/Perth | 2022-01-01 | 02:00:00 |
| 4 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 | 2022-03-01 03:00:00 | Australia/Perth | 2022-01-01 | 03:00:00 |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
Conclusion: Successfully split the column in to it's Date and Time constituents.
Example 2: Invalid column name | |
---|---|
1 |
|
ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
Try one of: ["a", "b", "c_datetime", "d_datetime", "e_datetime", "TIMEZONE_LOCATION"].
Conclusion: Column does not exist.
Example 2: Invalid column name | |
---|---|
1 |
|
TypeError: Column must be type 'timestamp' or 'datetime'.
Current type: [('b', 'string')]
Conclusion: Column is not the correct type for splitting.
See Also
Source code in src/toolbox_pyspark/datetime.py
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 |
|
split_datetime_columns
🔗
split_datetime_columns(
dataframe: psDataFrame,
columns: Optional[Union[str, str_collection]] = None,
) -> psDataFrame
Summary
For all the columns listed in columns
, split them each in to their respective date
and time
components.
Details
The reason why this function is structured this way, and not re-calling split_datetime_column()
in each iteration of columns
is due to pyspark
RDD complexity. More specifically, if it were to call split_datetime_column()
each time, the RDD would get incredibly and unnecessarily complicated. However, by doing it this way, using the .withColumns()
method, it will project the SQL expression once down to the underlying dataframe; not multiple times. Therefore, in this way, the underlying SQL execution plan is now much less complicated; albeit that the coding DRY principle is not strictly being followed here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataframe
|
DataFrame
|
The DataFrame to update. |
required |
columns
|
Optional[Union[str, str_collection]]
|
The list of columns to update. If not given, it will generate the list of columns from the |
None
|
Raises:
Type | Description |
---|---|
TypeError
|
If any of the inputs parsed to the parameters of this function are not the correct type. Uses the |
ColumnDoesNotExistError
|
If any of the |
TypeError
|
If any of the columns in |
Returns:
Type | Description |
---|---|
DataFrame
|
The updated DataFrame. |
Examples
Set up | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
+---+---+---------------------+---------------------+---------------------+-------------------+
| a | b | c_datetime | d_datetime | e_datetime | TIMEZONE_LOCATION |
+---+---+---------------------+---------------------+---------------------+-------------------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Australia/Perth |
| 2 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 | 2022-03-01 01:00:00 | Australia/Perth |
| 3 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 | 2022-03-01 02:00:00 | Australia/Perth |
| 4 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 | 2022-03-01 03:00:00 | Australia/Perth |
+---+---+---------------------+---------------------+---------------------+-------------------+
Example 1: Default config | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
| a | b | c_datetime | d_datetime | e_datetime | TIMEZONE_LOCATION | C_DATE | C_TIME | D_DATE | D_TIME | E_DATE | E_TIME |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Australia/Perth | 2022-01-01 | 00:00:00 | 2022-02-01 | 00:00:00 | 2022-03-01 | 00:00:00 |
| 2 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 | 2022-03-01 01:00:00 | Australia/Perth | 2022-01-01 | 01:00:00 | 2022-02-01 | 01:00:00 | 2022-03-01 | 01:00:00 |
| 3 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 | 2022-03-01 02:00:00 | Australia/Perth | 2022-01-01 | 02:00:00 | 2022-02-01 | 02:00:00 | 2022-03-01 | 02:00:00 |
| 4 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 | 2022-03-01 03:00:00 | Australia/Perth | 2022-01-01 | 03:00:00 | 2022-02-01 | 03:00:00 | 2022-03-01 | 03:00:00 |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
Conclusion: Successfully split all DateTime columns in to their Date and Time constituents.
Example 2: Custom config | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+
| a | b | c_datetime | d_datetime | e_datetime | TIMEZONE_LOCATION | C_DATE | C_TIME | D_DATE | D_TIME |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Australia/Perth | 2022-01-01 | 00:00:00 | 2022-02-01 | 00:00:00 |
| 2 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 | 2022-03-01 01:00:00 | Australia/Perth | 2022-01-01 | 01:00:00 | 2022-02-01 | 01:00:00 |
| 3 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 | 2022-03-01 02:00:00 | Australia/Perth | 2022-01-01 | 02:00:00 | 2022-02-01 | 02:00:00 |
| 4 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 | 2022-03-01 03:00:00 | Australia/Perth | 2022-01-01 | 03:00:00 | 2022-02-01 | 03:00:00 |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+
Conclusion: Successfully split two columns into their Date and Time constituents.
Example 3: All columns | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
| a | b | c_datetime | d_datetime | e_datetime | TIMEZONE_LOCATION | C_DATE | C_TIME | D_DATE | D_TIME | E_DATE | E_TIME |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Australia/Perth | 2022-01-01 | 00:00:00 | 2022-02-01 | 00:00:00 | 2022-03-01 | 00:00:00 |
| 2 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 | 2022-03-01 01:00:00 | Australia/Perth | 2022-01-01 | 01:00:00 | 2022-02-01 | 01:00:00 | 2022-03-01 | 01:00:00 |
| 3 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 | 2022-03-01 02:00:00 | Australia/Perth | 2022-01-01 | 02:00:00 | 2022-02-01 | 02:00:00 | 2022-03-01 | 02:00:00 |
| 4 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 | 2022-03-01 03:00:00 | Australia/Perth | 2022-01-01 | 03:00:00 | 2022-02-01 | 03:00:00 | 2022-03-01 | 03:00:00 |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
Conclusion: Successfully split all DateTime columns in to their Date and Time constituents.
Example 4: Single column | |
---|---|
1 |
|
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
| a | b | c_datetime | d_datetime | e_datetime | TIMEZONE_LOCATION | C_DATE | C_TIME |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
| 1 | a | 2022-01-01 00:00:00 | 2022-02-01 00:00:00 | 2022-03-01 00:00:00 | Australia/Perth | 2022-01-01 | 00:00:00 |
| 2 | b | 2022-01-01 01:00:00 | 2022-02-01 01:00:00 | 2022-03-01 01:00:00 | Australia/Perth | 2022-01-01 | 01:00:00 |
| 3 | c | 2022-01-01 02:00:00 | 2022-02-01 02:00:00 | 2022-03-01 02:00:00 | Australia/Perth | 2022-01-01 | 02:00:00 |
| 4 | d | 2022-01-01 03:00:00 | 2022-02-01 03:00:00 | 2022-03-01 03:00:00 | Australia/Perth | 2022-01-01 | 03:00:00 |
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
Conclusion: Successfully split a single column in to it's Date and Time constituents.
Example 5: Invalid column name | |
---|---|
1 |
|
ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
Try one of: ["a", "b", "c_datetime", "d_datetime", "e_datetime", "TIMEZONE_LOCATION"].
Conclusion: Column does not exist.
Example 6: Invalid column type | |
---|---|
1 |
|
TypeError: Column must be type 'timestamp' or 'datetime'.
Current type: [('b', 'string')]
Conclusion: Column is not the correct type for splitting.
See Also
Source code in src/toolbox_pyspark/datetime.py
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 |
|