function parse_datetime_selection
Converts string or datetime-like selection specifications into parsed datetime objects, handling single values, slices, and collections.
/tf/active/vicechatdev/patches/util.py
2094 - 2107
simple
Purpose
This function normalizes datetime selection specifications by parsing string representations and datetime-like objects into standardized datetime objects. It handles three types of inputs: single values (strings or datetime objects), slices (for range selections), and collections (lists or sets). This is useful for data filtering and selection operations where datetime specifications may come in various formats.
Source Code
def parse_datetime_selection(sel):
"""
Parses string selection specs as datetimes.
"""
if isinstance(sel, str) or isdatetime(sel):
sel = parse_datetime(sel)
if isinstance(sel, slice):
if isinstance(sel.start, str) or isdatetime(sel.start):
sel = slice(parse_datetime(sel.start), sel.stop)
if isinstance(sel.stop, str) or isdatetime(sel.stop):
sel = slice(sel.start, parse_datetime(sel.stop))
if isinstance(sel, (set, list)):
sel = [parse_datetime(v) if isinstance(v, str) else v for v in sel]
return sel
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
sel |
- | - | positional_or_keyword |
Parameter Details
sel: A selection specification that can be: (1) a string representing a datetime, (2) a datetime-like object, (3) a slice object with string or datetime start/stop values, or (4) a list/set of strings or datetime objects. The function will parse any string or datetime-like components into proper datetime objects.
Return Value
Returns the selection specification with all string and datetime-like values converted to parsed datetime objects. The return type matches the input structure: single datetime object for single values, slice object with parsed datetime boundaries for slices, or list of parsed datetime objects for collections.
Dependencies
datetimecftime
Required Imports
import datetime as dt
Conditional/Optional Imports
These imports are only needed under specific conditions:
import cftime
Condition: Required if the parse_datetime function or isdatetime function uses cftime for handling non-standard calendars or climate/forecast time conventions
Required (conditional)Usage Example
# Assuming parse_datetime and isdatetime functions are available
# Example 1: Parse a single string datetime
result = parse_datetime_selection('2023-01-15')
# Example 2: Parse a slice with string boundaries
result = parse_datetime_selection(slice('2023-01-01', '2023-12-31'))
# Example 3: Parse a list of datetime strings
result = parse_datetime_selection(['2023-01-01', '2023-06-15', '2023-12-31'])
# Example 4: Mixed types in a list
from datetime import datetime
result = parse_datetime_selection(['2023-01-01', datetime(2023, 6, 15)])
Best Practices
- Ensure parse_datetime() and isdatetime() helper functions are available in the module scope before calling this function
- The function preserves the input structure (single value, slice, or collection) while only converting the datetime components
- When using slices, both start and stop values are independently checked and parsed if needed
- The function handles None values in slices gracefully (e.g., slice(None, '2023-12-31'))
- For collections, the function returns a list even if the input was a set, which may affect ordering
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function parse_datetime_v1 61.8% similar
-
function parse_datetime 57.8% similar
-
function format_datetime_v1 53.2% similar
-
function isdatetime 52.3% similar
-
function dt_to_int 49.1% similar