Skip to content

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 @typeguard.typechecked decorator.

ColumnDoesNotExistError

If the column does not exist within dataframe.columns.

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
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession
>>> from toolbox_pyspark.datetime import rename_datetime_column
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = spark.createDataFrame(
...     pd.DataFrame(
...         {
...             "a": [1, 2, 3, 4],
...             "b": ["a", "b", "c", "d"],
...             "c_date": pd.date_range(start="2022-01-01", periods=4, freq="h"),
...             "d_date": pd.date_range(start="2022-02-01", periods=4, freq="h"),
...         }
...     )
... )
>>>
>>> # Check
>>> df.show()
Terminal
+---+---+---------------------+---------------------+
| 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
>>> rename_datetime_column(df, "c_date").show()
Terminal
+---+---+---------------------+---------------------+
| 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
>>> rename_datetime_column(df, "fff")
Terminal
ColumnDoesNotExistError: Column "fff" does not exist in "dataframe".
Try one of: ["a", "b", "c_date", "d_date"].

Conclusion: Column does not exist.

See Also
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
@typechecked
def rename_datetime_column(
    dataframe: psDataFrame,
    column: str,
) -> psDataFrame:
    """
    !!! note "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.

    Params:
        dataframe (psDataFrame):
            The DataFrame to update.
        column (str):
            The column to check.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        ColumnDoesNotExistError:
            If the `#!py column` does not exist within `#!py dataframe.columns`.

    Returns:
        (psDataFrame):
            The updated Data Frame.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession
        >>> from toolbox_pyspark.datetime import rename_datetime_column
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = spark.createDataFrame(
        ...     pd.DataFrame(
        ...         {
        ...             "a": [1, 2, 3, 4],
        ...             "b": ["a", "b", "c", "d"],
        ...             "c_date": pd.date_range(start="2022-01-01", periods=4, freq="h"),
        ...             "d_date": pd.date_range(start="2022-02-01", periods=4, freq="h"),
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: Update column"}
        >>> rename_datetime_column(df, "c_date").show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully renamed column."
        </div>

        ```{.py .python linenums="1" title="Example 2: Missing column"}
        >>> rename_datetime_column(df, "fff")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        ColumnDoesNotExistError: Column "fff" does not exist in "dataframe".
        Try one of: ["a", "b", "c_date", "d_date"].
        ```
        !!! failure "Conclusion: Column does not exist."
        </div>

    ??? tip "See Also"
        - [`assert_column_exists()`][toolbox_pyspark.checks.assert_column_exists]
        - [`rename_datetime_columns()`][toolbox_pyspark.datetime.rename_datetime_columns]
    """
    assert_column_exists(dataframe=dataframe, column=column, match_case=True)
    if f"{column}TIME" not in dataframe.columns:
        return dataframe.withColumnRenamed(column, f"{column}TIME")
    else:
        return dataframe

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 None or "all", then the function will automatically determine which columns to update based on the following logic:

  1. Loop through each column on dataframe to fetch the name and dtype using the method: dataframe.dtypes.
    1. If the column name ends with "date"
    2. AND the column type is "timestamp"
    3. AND there is NOT already a column existing in the dataframe.columns with the name: f"{column}TIME"
    4. THEN rename the column to have the name: f"{column}TIME"
  2. Next column.

Default: None.

None

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

ColumnDoesNotExistError

If any of the columns do not exist within dataframe.columns.

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
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession
>>> from toolbox_pyspark.datetime import rename_datetime_column
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = spark.createDataFrame(
...     pd.DataFrame(
...         {
...             "a": [1, 2, 3, 4],
...             "b": ["a", "b", "c", "d"],
...             "c_date": pd.date_range(start="2022-01-01", periods=4, freq="h"),
...             "d_date": pd.date_range(start="2022-02-01", periods=4, freq="h"),
...         }
...     )
... )
>>>
>>> # Check
>>> df.show()
Terminal
+---+---+---------------------+---------------------+
| 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
>>> rename_datetime_column(df, ["c_date"]).show()
Terminal
+---+---+---------------------+---------------------+
| 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
>>> rename_datetime_column(df, "c_date").show()
Terminal
+---+---+---------------------+---------------------+
| 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
>>> rename_datetime_column(df).show()
Terminal
+---+---+---------------------+---------------------+
| 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
>>> rename_datetime_column(df, "all").show()
Terminal
+---+---+---------------------+---------------------+
| 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
>>> rename_datetime_columns(df, ["fff", "ggg"])
Terminal
Attribute Error: Columns ["fff", "ggg"] do not exist in "dataframe".
Try one of: ["a", "b", "c_dateTIME", "d_dateTIME"].

Conclusion: Columns do not exist.

See Also
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
@typechecked
def rename_datetime_columns(
    dataframe: psDataFrame,
    columns: Optional[Union[str_collection, str]] = None,
) -> psDataFrame:
    """
    !!! note "Summary"
        Fix the column names for the date-time columns.

    ???+ abstract "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.

    Params:
        dataframe (psDataFrame):
            The DataFrame to update.
        columns (Optional[Union[str_collection, str]], None):
            An optional list of columns to update.
            If this is not provided, or is the value `#!py None` or `#!py "all"`, then the function will automatically determine which columns to update based on the following logic:

            1. Loop through each column on `dataframe` to fetch the name and dtype using the method: `dataframe.dtypes`.
                1. If the column name ends with `#!py "date"`
                2. **AND** the column type is `#!py "timestamp"`
                3. **AND** there is **NOT** already a column existing in the `dataframe.columns` with the name: `f"{column}TIME"`
                4. **THEN** rename the column to have the name: `f"{column}TIME"`
            2. Next column.

            Default: `None`.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        ColumnDoesNotExistError:
            If any of the `#!py columns` do not exist within `#!py dataframe.columns`.

    Returns:
        (psDataFrame):
            The updated DataFrame.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession
        >>> from toolbox_pyspark.datetime import rename_datetime_column
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = spark.createDataFrame(
        ...     pd.DataFrame(
        ...         {
        ...             "a": [1, 2, 3, 4],
        ...             "b": ["a", "b", "c", "d"],
        ...             "c_date": pd.date_range(start="2022-01-01", periods=4, freq="h"),
        ...             "d_date": pd.date_range(start="2022-02-01", periods=4, freq="h"),
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: One column"}
        >>> rename_datetime_column(df, ["c_date"]).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully renamed column."
        </div>

        ```{.py .python linenums="1" title="Example 2: One column `str`"}
        >>> rename_datetime_column(df, "c_date").show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully renamed column."
        </div>

        ```{.py .python linenums="1" title="Example 3: All columns"}
        >>> rename_datetime_column(df).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully renamed columns."
        </div>

        ```{.py .python linenums="1" title="Example 4: All columns using 'all'"}
        >>> rename_datetime_column(df, "all").show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully renamed columns."
        </div>

        ```{.py .python linenums="1" title="Example 5: Missing column"}
        >>> rename_datetime_columns(df, ["fff", "ggg"])
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        Attribute Error: Columns ["fff", "ggg"] do not exist in "dataframe".
        Try one of: ["a", "b", "c_dateTIME", "d_dateTIME"].
        ```
        !!! failure "Conclusion: Columns do not exist."
        </div>

    ??? tip "See Also"
        - [`assert_columns_exists()`][toolbox_pyspark.checks.assert_columns_exists]
        - [`rename_datetime_column()`][toolbox_pyspark.datetime.rename_datetime_column]
    """
    if columns is None or columns == "all":
        datetime_cols: str_list = [
            col
            for col in get_columns(dataframe, "all_datetime")
            if col.lower().endswith("date")
        ]
        columns = [
            col
            for col in datetime_cols
            if col.lower().endswith("date") and f"{col}TIME" not in dataframe.columns
        ]
    elif is_type(columns, str):
        columns = [columns]
    assert_columns_exists(dataframe, columns, True)
    for column in columns:
        dataframe = rename_datetime_column(dataframe, column)
    return dataframe

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 dataframe.columns, and must be type typestamp.

required
from_timezone str

The timezone which will be converted from. Must be a valid TimeZoneID, for more info, see: TimeZoneID.
If not given, will default the from_timezone to be UTC.
Default: None.

None
column_with_target_timezone str

The column containing the target timezone value. By default will be the column "timezone_location".
Defaults to "timezone_location".upper().

upper()

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

ColumnDoesNotExistError

If "column" or column_with_target_timezone does not exist within dataframe.columns.

ValueError

If the from_timezone or column_with_target_timezone is not a valid timezone.

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
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession
>>> from toolbox_pyspark.datetime import add_local_datetime_column
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = spark.createDataFrame(
...     pd.DataFrame(
...         {
...             "a": [1, 2, 3, 4],
...             "b": ["a", "b", "c", "d"],
...             "c": pd.date_range(start="2022-01-01", periods=4, freq="D"),
...             "d": pd.date_range(start="2022-02-01", periods=4, freq="D"),
...             "e": pd.date_range(start="2022-03-01", periods=4, freq="D"),
...             "target": ["Asia/Singapore"] * 4,
...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
...         }
...     )
... )
>>>
>>> # Check
>>> df.show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
| 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
>>> add_local_datetime_column(df, "c").show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+
| 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
>>> add_local_datetime_column(
...     dataframe=df,
...     column="c",
...     from_timezone="Australia/Sydney",
...     column_with_target_timezone="target",
... ).show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 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
>>> add_local_datetime_column(df, "invalid_column")
Terminal
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
>>> add_local_datetime_column(df, "c", from_timezone="Invalid/Timezone")
Terminal
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 in column 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 the from_timezone value to UTC, then a second conversion from UTC to whichever timezone is defined in the column column_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 the dataframe. Therefore, we need to call .withColumn() first to add {column}_UTC, then we need to call .withColumn() a second time to add {column}_LOCAL.
See Also
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
@typechecked
def add_local_datetime_column(
    dataframe: psDataFrame,
    column: str,
    from_timezone: Optional[str] = None,
    column_with_target_timezone: str = "timezone_location".upper(),
) -> psDataFrame:
    """
    !!! note "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.

    Params:
        dataframe (psDataFrame):
            The DataFrame to be fixed
        column (str):
            The name of the column to do the conversion for. Must exist in `#!py dataframe.columns`, and must be type `typestamp`.
        from_timezone (str, optional):
            The timezone which will be converted from. Must be a valid TimeZoneID, for more info, see: [TimeZoneID](https://docs.oracle.com/middleware/12211/wcs/tag-ref/MISC/TimeZones.html).<br>
            If not given, will default the `from_timezone` to be UTC.<br>
            Default: `#!py None`.
        column_with_target_timezone (str, optional):
            The column containing the target timezone value. By default will be the column `#!py "timezone_location"`.<br>
            Defaults to `#!py "timezone_location".upper()`.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        ColumnDoesNotExistError:
            If `#!py "column"` or `column_with_target_timezone` does not exist within `#!py dataframe.columns`.
        ValueError:
            If the `from_timezone` or `column_with_target_timezone` is not a valid timezone.

    Returns:
        (psDataFrame):
            The updated DataFrame.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession
        >>> from toolbox_pyspark.datetime import add_local_datetime_column
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = spark.createDataFrame(
        ...     pd.DataFrame(
        ...         {
        ...             "a": [1, 2, 3, 4],
        ...             "b": ["a", "b", "c", "d"],
        ...             "c": pd.date_range(start="2022-01-01", periods=4, freq="D"),
        ...             "d": pd.date_range(start="2022-02-01", periods=4, freq="D"),
        ...             "e": pd.date_range(start="2022-03-01", periods=4, freq="D"),
        ...             "target": ["Asia/Singapore"] * 4,
        ...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: Converting from UTC time"}
        >>> add_local_datetime_column(df, "c").show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully converted from UTC."
        </div>

        ```{.py .python linenums="1" title="Example 2: Converting from specific timezone, with custom column containing target timezone"}
        >>> add_local_datetime_column(
        ...     dataframe=df,
        ...     column="c",
        ...     from_timezone="Australia/Sydney",
        ...     column_with_target_timezone="target",
        ... ).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully converted timezone."
        </div>

        ```{.py .python linenums="1" title="Example 3: Invalid column name"}
        >>> add_local_datetime_column(df, "invalid_column")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
        Try one of: ["a", "b", "c", "d", "e", "target", "TIMEZONE_LOCATION"].
        ```
        !!! failure "Conclusion: Column does not exist."
        </div>

        ```{.py .python linenums="1" title="Example 4: Invalid timezone"}
        >>> add_local_datetime_column(df, "c", from_timezone="Invalid/Timezone")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        ValueError: The timezone "Invalid/Timezone" is not a valid timezone.
        ```
        !!! failure "Conclusion: Invalid timezone."
        </div>

    ??? info "Notes"
        - If `#!py from_timezone is None`, then it is assumed that the datetime data in `column` is _already_ in UTC timezone.<br>
        - If `#!py from_timezone is not None`, then a new column will be added with the syntax `#!py {column}_UTC`, then another column added with `#!py {column}_LOCAL`. This is necessary because PySpark cannot convert immediately from one timezone to another; it must first require a conversion from the `from_timezone` value _to_ UTC, then a second conversion _from_ UTC to whichever timezone is defined in the column `column_with_target_timezone`.<br>
        - The reason why this function uses multiple [`.withColumn()`][withColumn] methods, instead of a single [`.withColumns()`][withColumns] expression is because to add the `#!py {column}_LOCAL` column, it is first necessary for the `#!py {column}_UTC` column to exist on the `dataframe`. Therefore, we need to call [`.withColumn()`][withColumn] first to add `#!py {column}_UTC`, then we need to call [`.withColumn()`][withColumn] a second time to add `#!py {column}_LOCAL`.
        [withColumn]: https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.withColumn.html
        [withColumns]: https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.withColumns.html

    ??? tip "See Also"
        - [`assert_columns_exists()`][toolbox_pyspark.checks.assert_columns_exists]
        - [`add_local_datetime_columns()`][toolbox_pyspark.datetime.add_local_datetime_columns]
    """
    assert_columns_exists(dataframe=dataframe, columns=[column, column_with_target_timezone])
    require_utc: bool = f"{column}_UTC" not in dataframe.columns
    require_local: bool = f"{column}_LOCAL" not in dataframe.columns
    if from_timezone is not None:
        if require_utc:
            dataframe = dataframe.withColumn(
                f"{column}_UTC",
                F.to_utc_timestamp(
                    F.col(column).cast("timestamp"),
                    from_timezone.title(),
                ),
            )
        if require_local:
            dataframe = dataframe.withColumn(
                f"{column}_LOCAL",
                F.from_utc_timestamp(
                    F.col(f"{column}_UTC").cast("timestamp"),
                    F.col(column_with_target_timezone),
                ),
            )
    else:
        if require_local:
            dataframe = dataframe.withColumn(
                f"{column}_LOCAL",
                F.from_utc_timestamp(
                    F.col(column).cast("timestamp"),
                    F.col(column_with_target_timezone),
                ),
            )
    return dataframe

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 date.
Defaults to None.

None
from_timezone Optional[str]

The timezone which will be converted from. If not given, will default the from timezone to be UTC.
Defaults to None.

None
column_with_target_timezone str

The column containing the target timezone value. By default will be the column "timezone_location".
Defaults to "timezone_location".upper().

upper()

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

ColumnDoesNotExistError

If any of the columns do not exist within dataframe.columns.

ValueError

If the from_timezone or column_with_target_timezone is not a valid timezone.

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
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession
>>> from toolbox_pyspark.datetime import add_local_datetime_columns
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = spark.createDataFrame(
...     pd.DataFrame(
...         {
...             "a": [1, 2, 3, 4],
...             "b": ["a", "b", "c", "d"],
...             "c": pd.date_range(start="2022-01-01", periods=4, freq="D"),
...             "d_datetime": pd.date_range(start="2022-02-01", periods=4, freq="D"),
...             "e_datetime": pd.date_range(start="2022-03-01", periods=4, freq="D"),
...             "target": ["Asia/Singapore"] * 4,
...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
...         }
...     )
... )
>>>
>>> # Check
>>> df.show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+
| 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
>>> add_local_datetime_columns(df).show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 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
>>> add_local_datetime_columns(df, ["c", "d_datetime"]).show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 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
>>> add_local_datetime_columns(
...     dataframe=df,
...     columns=["c", "d_datetime"],
...     from_timezone="Australia/Sydney",
...     column_with_target_timezone="target",
... ).show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
| 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
>>> add_local_datetime_columns(
...     dataframe=df,
...     columns="c",
...     from_timezone="Australia/Sydney",
...     column_with_target_timezone="target",
... ).show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
| 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
>>> add_local_datetime_columns(
...     dataframe=df,
...     columns="all",
...     from_timezone="Australia/Sydney",
...     column_with_target_timezone="target",
... ).show()
Terminal
+---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
| 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
>>> add_local_datetime_columns(df, "invalid_column")
Terminal
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
>>> add_local_datetime_columns(df, "c", from_timezone="Invalid/Timezone")
Terminal
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
@typechecked
def 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:
    """
    !!! note "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.

    ???+ abstract "Details"
        Under the hood, this function will call [`add_local_datetime_column()`][toolbox_pyspark.datetime.add_local_datetime_column] for each `column` in `columns`.

    Params:
        dataframe (psDataFrame):
            The DataFrame to update.
        columns (Optional[Union[str, str_collection]], optional):
            The columns to check. If not provided, it will use all of the columns which contains the text `date`.<br>
            Defaults to `#!py None`.
        from_timezone (Optional[str], optional):
            The timezone which will be converted from. If not given, will default the from timezone to be UTC.<br>
            Defaults to `#!py None`.
        column_with_target_timezone (str, optional):
            The column containing the target timezone value. By default will be the column `#!py "timezone_location"`.<br>
            Defaults to `#!py "timezone_location".upper()`.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        ColumnDoesNotExistError:
            If any of the `#!py columns` do not exist within `#!py dataframe.columns`.
        ValueError:
            If the `from_timezone` or `column_with_target_timezone` is not a valid timezone.

    Returns:
        (psDataFrame):
            The updated DataFrame.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession
        >>> from toolbox_pyspark.datetime import add_local_datetime_columns
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = spark.createDataFrame(
        ...     pd.DataFrame(
        ...         {
        ...             "a": [1, 2, 3, 4],
        ...             "b": ["a", "b", "c", "d"],
        ...             "c": pd.date_range(start="2022-01-01", periods=4, freq="D"),
        ...             "d_datetime": pd.date_range(start="2022-02-01", periods=4, freq="D"),
        ...             "e_datetime": pd.date_range(start="2022-03-01", periods=4, freq="D"),
        ...             "target": ["Asia/Singapore"] * 4,
        ...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: Default config"}
        >>> add_local_datetime_columns(df).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully converted columns to local timezone."
        </div>

        ```{.py .python linenums="1" title="Example 2: Semi-custom config"}
        >>> add_local_datetime_columns(df, ["c", "d_datetime"]).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully converted columns to local timezone."
        </div>

        ```{.py .python linenums="1" title="Example 3: Full-custom config"}
        >>> add_local_datetime_columns(
        ...     dataframe=df,
        ...     columns=["c", "d_datetime"],
        ...     from_timezone="Australia/Sydney",
        ...     column_with_target_timezone="target",
        ... ).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully converted columns to local time zone, from other custom time zone."
        </div>

        ```{.py .python linenums="1" title="Example 4: Single column"}
        >>> add_local_datetime_columns(
        ...     dataframe=df,
        ...     columns="c",
        ...     from_timezone="Australia/Sydney",
        ...     column_with_target_timezone="target",
        ... ).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully converted single column from other time zone to local time zone."
        </div>

        ```{.py .python linenums="1" title="Example 5: All columns"}
        >>> add_local_datetime_columns(
        ...     dataframe=df,
        ...     columns="all",
        ...     from_timezone="Australia/Sydney",
        ...     column_with_target_timezone="target",
        ... ).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+----------------+-------------------+---------------------+---------------------+---------------------+---------------------+
        ```
        !!! success "Conclusion: Successfully converted all date time columns from other time zone to local time zone."
        </div>

        ```{.py .python linenums="1" title="Example 6: Invalid column name"}
        >>> add_local_datetime_columns(df, "invalid_column")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
        Try one of: ["a", "b", "c", "d_datetime", "e_datetime", "target", "TIMEZONE_LOCATION"].
        ```
        !!! failure "Conclusion: Column does not exist."
        </div>

        ```{.py .python linenums="1" title="Example 7: Invalid timezone"}
        >>> add_local_datetime_columns(df, "c", from_timezone="Invalid/Timezone")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        ValueError: The timezone "Invalid/Timezone" is not a valid timezone.
        ```
        !!! failure "Conclusion: Invalid timezone."
        </div>

    ??? tip "See Also"
        - [`add_local_datetime_column()`][toolbox_pyspark.datetime.add_local_datetime_column]
    """
    if columns is None or columns in ["all"]:
        columns = [col for col in dataframe.columns if col.lower().endswith("datetime")]
    elif is_type(columns, str):
        columns = [columns]
    assert_columns_exists(dataframe, list(columns) + [column_with_target_timezone])
    for column in columns:
        dataframe = add_local_datetime_column(
            dataframe=dataframe,
            column=column,
            from_timezone=from_timezone,
            column_with_target_timezone=column_with_target_timezone,
        )
    return dataframe

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 @typeguard.typechecked decorator.

ColumnDoesNotExistError

If the column does not exist within dataframe.columns.

TypeError

If the column is not type timestamp or datetime.

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
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession
>>> from toolbox_pyspark.datetime import split_datetime_column
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = spark.createDataFrame(
...     pd.DataFrame(
...         {
...             "a": [1, 2, 3, 4],
...             "b": ["a", "b", "c", "d"],
...             "c_datetime": pd.date_range(start="2022-01-01", periods=4, freq="h"),
...             "d_datetime": pd.date_range(start="2022-02-01", periods=4, freq="h"),
...             "e_datetime": pd.date_range(start="2022-03-01", periods=4, freq="h"),
...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
...         }
...     )
... )
>>>
>>> # Check
>>> df.show()
Terminal
+---+---+---------------------+---------------------+---------------------+-------------------+
| 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
>>> split_datetime_column(df, "c_datetime").show()
Terminal
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
| 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
>>> split_datetime_column(df, "invalid_column")
Terminal
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
>>> split_datetime_column(df, "b")
Terminal
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
@typechecked
def split_datetime_column(
    dataframe: psDataFrame,
    column: str,
) -> psDataFrame:
    """
    !!! note "Summary"
        Take the column `column`, which should be a `timestamp` type, and split it in to it's respective `date` and `time` components.

    Params:
        dataframe (psDataFrame):
            The DataFrame to update.
        column (str):
            The column to split.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        ColumnDoesNotExistError:
            If the `#!py column` does not exist within `#!py dataframe.columns`.
        TypeError:
            If the `column` is not type `timestamp` or `datetime`.

    Returns:
        (psDataFrame):
            The updated DataFrame.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession
        >>> from toolbox_pyspark.datetime import split_datetime_column
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = spark.createDataFrame(
        ...     pd.DataFrame(
        ...         {
        ...             "a": [1, 2, 3, 4],
        ...             "b": ["a", "b", "c", "d"],
        ...             "c_datetime": pd.date_range(start="2022-01-01", periods=4, freq="h"),
        ...             "d_datetime": pd.date_range(start="2022-02-01", periods=4, freq="h"),
        ...             "e_datetime": pd.date_range(start="2022-03-01", periods=4, freq="h"),
        ...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+-------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+-------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: Default config"}
        >>> split_datetime_column(df, "c_datetime").show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
        ```
        !!! success "Conclusion: Successfully split the column in to it's Date and Time constituents."
        </div>

        ```{.py .python linenums="1" title="Example 2: Invalid column name"}
        >>> split_datetime_column(df, "invalid_column")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
        Try one of: ["a", "b", "c_datetime", "d_datetime", "e_datetime", "TIMEZONE_LOCATION"].
        ```
        !!! failure "Conclusion: Column does not exist."
        </div>

        ```{.py .python linenums="1" title="Example 2: Invalid column name"}
        >>> split_datetime_column(df, "b")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        TypeError: Column must be type 'timestamp' or 'datetime'.
        Current type: [('b', 'string')]
        ```
        !!! failure "Conclusion: Column is not the correct type for splitting."
        </div>

    ??? tip "See Also"
        - [`split_datetime_columns()`][toolbox_pyspark.datetime.split_datetime_columns]
    """
    assert_column_exists(dataframe, column)
    datetime_cols: str_list = get_columns(dataframe, "all_datetime")
    if not is_in(column, datetime_cols):
        raise TypeError(
            "Column must be type 'timestamp' or 'datetime'.\n"
            f"Current type: {[(col,typ) for col,typ in dataframe.dtypes if col == column]}"
        )
    col_date_name: str = column.upper().replace("DATETIME", "DATE")
    col_time_name: str = column.upper().replace("DATETIME", "TIME")
    col_date_value: Column = F.date_format(column, "yyyy-MM-dd").cast("string").cast("date")
    col_time_value: Column = F.date_format(column, "HH:mm:ss").cast("string")
    return dataframe.withColumns(
        {
            col_date_name: col_date_value,
            col_time_name: col_time_value,
        }
    )

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 dataframe.columns which contain the text datetime.
Defaults to None.

None

Raises:

Type Description
TypeError

If any of the inputs parsed to the parameters of this function are not the correct type. Uses the @typeguard.typechecked decorator.

ColumnDoesNotExistError

If any of the columns do not exist within dataframe.columns.

TypeError

If any of the columns in columns are not type timestamp or datetime.

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
>>> # Imports
>>> import pandas as pd
>>> from pyspark.sql import SparkSession
>>> from toolbox_pyspark.datetime import split_datetime_columns
>>>
>>> # Instantiate Spark
>>> spark = SparkSession.builder.getOrCreate()
>>>
>>> # Create data
>>> df = spark.createDataFrame(
...     pd.DataFrame(
...         {
...             "a": [1, 2, 3, 4],
...             "b": ["a", "b", "c", "d"],
...             "c_datetime": pd.date_range(start="2022-01-01", periods=4, freq="h"),
...             "d_datetime": pd.date_range(start="2022-02-01", periods=4, freq="h"),
...             "e_datetime": pd.date_range(start="2022-03-01", periods=4, freq="h"),
...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
...         }
...     )
... )
>>>
>>> # Check
>>> df.show()
Terminal
+---+---+---------------------+---------------------+---------------------+-------------------+
| 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
>>> split_datetime_columns(df).show()
Terminal
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
| 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
>>> split_datetime_columns(df, ["c_datetime", "d_datetime"]).show()
Terminal
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+
| 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
>>> split_datetime_columns(df, "all").show()
Terminal
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
| 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
>>> split_datetime_columns(df, "c_datetime").show()
Terminal
+---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
| 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
>>> split_datetime_columns(df, "invalid_column")
Terminal
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
>>> split_datetime_columns(df, "b")
Terminal
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
@typechecked
def split_datetime_columns(
    dataframe: psDataFrame,
    columns: Optional[Union[str, str_collection]] = None,
) -> psDataFrame:
    """
    !!! note "Summary"
        For all the columns listed in `columns`, split them each in to their respective `date` and `time` components.

    ???+ abstract "Details"
        The reason why this function is structured this way, and not re-calling [`split_datetime_column()`][toolbox_pyspark.datetime.split_datetime_column] in each iteration of `columns` is due to `#!py pyspark` RDD complexity. More specifically, if it _were_ to call [`split_datetime_column()`][toolbox_pyspark.datetime.split_datetime_column] each time, the RDD would get incredibly and unnecessarily complicated. However, by doing it this way, using the [`.withColumns()`](https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.withColumns.html) 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.

    Params:
        dataframe (psDataFrame):
            The DataFrame to update.
        columns (Optional[Union[str, str_collection]], optional):
            The list of columns to update. If not given, it will generate the list of columns from the `#!py dataframe.columns` which contain the text `datetime`.<br>
            Defaults to `#!py None`.

    Raises:
        TypeError:
            If any of the inputs parsed to the parameters of this function are not the correct type. Uses the [`@typeguard.typechecked`](https://typeguard.readthedocs.io/en/stable/api.html#typeguard.typechecked) decorator.
        ColumnDoesNotExistError:
            If any of the `#!py columns` do not exist within `#!py dataframe.columns`.
        TypeError:
            If any of the columns in `columns` are not type `timestamp` or `datetime`.

    Returns:
        (psDataFrame):
            The updated DataFrame.

    ???+ example "Examples"

        ```{.py .python linenums="1" title="Set up"}
        >>> # Imports
        >>> import pandas as pd
        >>> from pyspark.sql import SparkSession
        >>> from toolbox_pyspark.datetime import split_datetime_columns
        >>>
        >>> # Instantiate Spark
        >>> spark = SparkSession.builder.getOrCreate()
        >>>
        >>> # Create data
        >>> df = spark.createDataFrame(
        ...     pd.DataFrame(
        ...         {
        ...             "a": [1, 2, 3, 4],
        ...             "b": ["a", "b", "c", "d"],
        ...             "c_datetime": pd.date_range(start="2022-01-01", periods=4, freq="h"),
        ...             "d_datetime": pd.date_range(start="2022-02-01", periods=4, freq="h"),
        ...             "e_datetime": pd.date_range(start="2022-03-01", periods=4, freq="h"),
        ...             "TIMEZONE_LOCATION": ["Australia/Perth"] * 4,
        ...         }
        ...     )
        ... )
        >>>
        >>> # Check
        >>> df.show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+-------------------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+-------------------+
        ```
        </div>

        ```{.py .python linenums="1" title="Example 1: Default config"}
        >>> split_datetime_columns(df).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
        ```
        !!! success "Conclusion: Successfully split all DateTime columns in to their Date and Time constituents."
        </div>

        ```{.py .python linenums="1" title="Example 2: Custom config"}
        >>> split_datetime_columns(df, ["c_datetime", "d_datetime"]).show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+
        ```
        !!! success "Conclusion: Successfully split two columns into their Date and Time constituents."
        </div>

        ```{.py .python linenums="1" title="Example 3: All columns"}
        >>> split_datetime_columns(df, "all").show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+------------+----------+------------+----------+
        ```
        !!! success "Conclusion: Successfully split all DateTime columns in to their Date and Time constituents."
        </div>

        ```{.py .python linenums="1" title="Example 4: Single column"}
        >>> split_datetime_columns(df, "c_datetime").show()
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
        | 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 |
        +---+---+---------------------+---------------------+---------------------+-------------------+------------+----------+
        ```
        !!! success "Conclusion: Successfully split a single column in to it's Date and Time constituents."
        </div>

        ```{.py .python linenums="1" title="Example 5: Invalid column name"}
        >>> split_datetime_columns(df, "invalid_column")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        ColumnDoesNotExistError: Column "invalid_column" does not exist in "dataframe".
        Try one of: ["a", "b", "c_datetime", "d_datetime", "e_datetime", "TIMEZONE_LOCATION"].
        ```
        !!! failure "Conclusion: Column does not exist."
        </div>

        ```{.py .python linenums="1" title="Example 6: Invalid column type"}
        >>> split_datetime_columns(df, "b")
        ```
        <div class="result" markdown>
        ```{.txt .text title="Terminal"}
        TypeError: Column must be type 'timestamp' or 'datetime'.
        Current type: [('b', 'string')]
        ```
        !!! failure "Conclusion: Column is not the correct type for splitting."
        </div>

    ??? tip "See Also"
        - [`split_datetime_column()`][toolbox_pyspark.datetime.split_datetime_column]
    """
    if columns is None or columns in ["all"]:
        columns = [col for col in dataframe.columns if "datetime" in col.lower()]
    elif is_type(columns, str):
        columns = [columns]
    assert_columns_exists(dataframe, columns)
    datetime_cols: str_list = get_columns(dataframe, "all_datetime")
    if not is_all_in(columns, datetime_cols):
        raise TypeError(
            "Columns to split must be type 'timestamp' or 'datetime'.\n"
            f"Current types: {[(col,typ) for col,typ in dataframe.dtypes if col in columns]}"
        )
    cols_exprs: dict[str, Column] = {}
    for column in columns:
        col_date_name: str = column.upper().replace("DATETIME", "DATE")
        col_time_name: str = column.upper().replace("DATETIME", "TIME")
        col_date_value: Column = (
            F.date_format(column, "yyyy-MM-dd").cast("string").cast("date")
        )
        col_time_value: Column = F.date_format(column, "HH:mm:ss").cast("string")
        cols_exprs[col_date_name] = col_date_value
        cols_exprs[col_time_name] = col_time_value
    return dataframe.withColumns(cols_exprs)