-
Notifications
You must be signed in to change notification settings - Fork 177
Open
Description
Thank you for creating and sharing tabulate.
I have a feature request that I think would be useful.
Add an option to review the first couple rows of data to determine if any columns contain Unix/Linux Epoch timestamp values (either in seconds or milliseconds) and convert then automatically convert those columns into Date/Time values.
I am providing an example function that just detects whether a column is likely an epoch timestamp but it doesn't actually change the timestamp to a datetime value, but that is pretty easy to add.
You could also enhance the code by allowing the user to set the following:
- The size of the window to allow (min_dt & max_dt). I just used +/- 10 years from current but this could be passed in.
- Format of the datetime data.
`
def is_likely_epoch(n, unit="s"):
if not isinstance(n, (int, float)):
return False
# Convert milliseconds to seconds if needed
if unit == "ms":
n = n / 1000.0
try:
now = datetime.datetime.now(datetime.timezone.utc)
# Define dynamic bounds: now ± 10 years
min_dt = now - datetime.timedelta(days=365 * 10)
max_dt = now + datetime.timedelta(days=365 * 10)
MIN_EPOCH_S = min_dt.timestamp()
MAX_EPOCH_S = max_dt.timestamp()
# Check if value falls within bounds
if not (MIN_EPOCH_S <= n <= MAX_EPOCH_S):
return False
# Attempt conversion to datetime
datetime.datetime.fromtimestamp(n, tz=datetime.timezone.utc)
return True
except (ValueError, OSError, OverflowError):
return False
`
Thank you for considering this request.
Metadata
Metadata
Assignees
Labels
No labels