@@ -310,3 +310,81 @@ def optional_datetime_to_unix_nanoseconds(
310310 if value is None:
311311 return None
312312 return datetime_to_unix_nanoseconds(value)
313+
314+
315+ def convert_to_date(value: str) -> date | None:
316+ """
317+ Convert the given `value` to a date (or None).
318+
319+ Parameters
320+ ----------
321+ value : str
322+ The date string value to convert.
323+
324+ Returns
325+ -------
326+ datetime.date or `None`
327+ The corresponding `date` object if the conversion succeeds,
328+ or `None` if the input value cannot be converted.
329+
330+ """
331+ # Calling `.date()` on a NaT value will retain the NaT value
332+ timestamp = pd.to_datetime(value, utc=True, errors="coerce")
333+ return timestamp.date() if pd.notna(timestamp) else None
334+
335+
336+ def convert_to_datetime(value: str) -> pd.Timestamp | None:
337+ """
338+ Convert the given `value` to a pandas Timestamp (or None).
339+
340+ Parameters
341+ ----------
342+ value : str
343+ The datetime string value to convert.
344+
345+ Returns
346+ -------
347+ pandas.Timestamp or None
348+ The corresponding `Timestamp` object if the conversion succeeds,
349+ or `None` if the input value cannot be converted.
350+
351+ """
352+ return pd.to_datetime(value, utc=True, errors="coerce")
353+
354+
355+ def convert_date_columns(df: pd.DataFrame, columns: list[str]) -> None:
356+ """
357+ Convert the specified columns in a DataFrame to date objects.
358+
359+ The function modifies the input DataFrame in place.
360+
361+ Parameters
362+ ----------
363+ df : pandas.DataFrame
364+ The pandas DataFrame to modify.
365+ columns : List[str]
366+ The column names to convert.
367+
368+ """
369+ for column in columns:
370+ if column not in df:
371+ continue
372+ df[column] = df[column].apply(convert_to_date)
373+
374+
375+ def convert_datetime_columns(df: pd.DataFrame, columns: list[str]) -> None:
376+ """
377+ Convert the specified columns in a DataFrame to pandas Timestamp objects.
378+
379+ Parameters
380+ ----------
381+ df : pandas.DataFrame
382+ The pandas DataFrame to modify.
383+ columns : List[str]
384+ The column names to convert.
385+
386+ """
387+ for column in columns:
388+ if column not in df:
389+ continue
390+ df[column] = df[column].apply(convert_to_datetime)
0 commit comments