Skip to content

Commit

Permalink
fix(python): load_as_version with datetime object with no timezone sp…
Browse files Browse the repository at this point in the history
…ecified (delta-io#2429)

# Description
Upon attempting to retrieve the version with a datetime object, the
`load_as_version` method throws a `ValueError: Failed to parse datetime
string: premature end of input`.

Datetime objects without a specified timezone will be treated as UTC
datetimes.
  • Loading branch information
t1g0rz authored Apr 21, 2024
1 parent ebbdd69 commit 5f137ca
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions python/deltalake/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import operator
import warnings
from dataclasses import dataclass
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from enum import Enum
from functools import reduce
from pathlib import Path
Expand Down Expand Up @@ -607,6 +607,7 @@ def load_as_version(self, version: Union[int, str, datetime]) -> None:
"""
Load/time travel a DeltaTable to a specified version number, or a timestamp version of the table. If a
string is passed then the argument should be an RFC 3339 and ISO 8601 date and time string format.
If a datetime object without a timezone is passed, the UTC timezone will be assumed.
Args:
version: the identifier of the version of the DeltaTable to load
Expand All @@ -620,7 +621,8 @@ def load_as_version(self, version: Union[int, str, datetime]) -> None:
**Use a datetime object**
```
dt.load_as_version(datetime(2023,1,1))
dt.load_as_version(datetime(2023, 1, 1))
dt.load_as_version(datetime(2023, 1, 1, tzinfo=timezone.utc))
```
**Use a datetime in string format**
Expand All @@ -633,6 +635,8 @@ def load_as_version(self, version: Union[int, str, datetime]) -> None:
if isinstance(version, int):
self._table.load_version(version)
elif isinstance(version, datetime):
if version.tzinfo is None:
version = version.astimezone(timezone.utc)
self._table.load_with_datetime(version.isoformat())
elif isinstance(version, str):
self._table.load_with_datetime(version)
Expand Down

0 comments on commit 5f137ca

Please sign in to comment.