2.2.4.1 Time and Date Data

Dealing with time and date data can be tricky. String-formatted data is hard to compare and represent for modelling.

print("02/03/1900" > "01/01/2020")
True

We use the datetime.datetime object in examples below. However, you can also use datetime.date and datetime.time as appropriate.

datetime

We need to represent date data in a format that will allow us to compare items and perform operations such as addition and subtraction.

Python’s standard library includes the datetime module. This allows us to represent dates and times as structured objects.

import datetime

# create a datetime object with value set to now 
now = datetime.datetime.now()
print(now)
2023-02-14 23:04:13.724714

This object has structure. We can, for example, extract the year property from this object.

print(now.year)
2023

We can also compare this datetime to others, as well as perform date arithmetic.

past = datetime.datetime.fromisoformat("2020-12-22") 
is_gt_now = past > now
print(f"d gt now: {is_gt_now}")

# subtract past from now
difference = now - past 
print(f"now - d: {difference}. Type: {type(difference)}")
d gt now: False
now - d: 784 days, 23:04:13.724714. Type: <class 'datetime.timedelta'>

We can get a numeric, POSIX timestamp, representation of these dates with datetime.datetime.timestamp().

print(f"now timestamp: {now.timestamp()}")
print(f"past timestamp: {past.timestamp()}")
now timestamp: 1676415853.724714
past timestamp: 1608595200.0

Note some UTC vs local time complications detailed here.

Converting From/To String

For converting from a string, we can use the datetime.datetime.strptime(date_string, format) function. Format codes are detailed here.

dt = datetime.datetime.strptime("30/03/99 16:30", "%d/%m/%y %H:%M")
print(f"{dt}. Type: {type(dt)}")
1999-03-30 16:30:00. Type: <class 'datetime.datetime'>

And to convert a date to string we can use datetime.datetime.strftime(format).

s = now.strftime("%d/%m/%y %H:%M") 
print(f"{s}. Type: {type(s)}")
14/02/23 23:04. Type: <class 'str'>

Pandas Datetime Accessor (Series.dt)

Pandas provides an accessor object for datetime-like properties of Series values. See here.

E.g. (taken almost directly from Pandas docs, linked above)

import pandas as pd

seconds_series = pd.Series(pd.date_range("2000-01-01", periods=3, freq="s"))
display(seconds_series)

# access seconds property of values in series
display(seconds_series.dt.second)
0   2000-01-01 00:00:00
1   2000-01-01 00:00:01
2   2000-01-01 00:00:02
dtype: datetime64[ns]
0    0
1    1
2    2
dtype: int64