🔍 Code Extractor

function parse_datetime_selection

Maturity: 40

Converts string or datetime-like selection specifications into parsed datetime objects, handling single values, slices, and collections.

File:
/tf/active/vicechatdev/patches/util.py
Lines:
2094 - 2107
Complexity:
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

  • datetime
  • cftime

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function parse_datetime_v1 61.8% similar

    Converts various date representations (string, integer, pandas Timestamp) into a numpy datetime64 object using pandas datetime parsing capabilities.

    From: /tf/active/vicechatdev/patches/util.py
  • function parse_datetime 57.8% similar

    Parses a datetime string in YYYY-MM-DD HH:MM:SS format into a Python datetime object, returning None if parsing fails.

    From: /tf/active/vicechatdev/CDocs/utils/__init__.py
  • function format_datetime_v1 53.2% similar

    Converts an ISO format datetime string into a human-readable UTC datetime string formatted as 'YYYY-MM-DD HH:MM:SS UTC'.

    From: /tf/active/vicechatdev/SPFCsync/dry_run_test.py
  • function isdatetime 52.3% similar

    Determines whether a given value (array or scalar) is a recognized datetime type, checking both NumPy datetime64 arrays and Python datetime objects.

    From: /tf/active/vicechatdev/patches/util.py
  • function dt_to_int 49.1% similar

    Converts various datetime types (pandas, numpy, cftime, Python datetime) to an integer timestamp with a specified time unit.

    From: /tf/active/vicechatdev/patches/util.py
← Back to Browse