Package datetime

  • This package has several modules which can be imported to analyze time data.

  • datetime module
import datetime as dt
date = dt.datetime(2022,1,25,15,53,43) # datetime object allows specification of datetime in the following order - Year, month, Date, Hours, Minutes, seconds
print(date)
print(dt.datetime.today())

will print out:

2022-01-25 15:53:43
2022-01-25 16:32:27.002518

  • Datetime can be parsed from a string by using .strptime() method
dt_string = "2022-01-22 1:00:01"
dt_format = "%Y-%m-%d %H:%M:%S"
print(dt.datetime.strptime(dt_string,dt_format))

Prints out the date: 2022-01-22 01:00:01. From a datetime object, extracting specific information is possible using print(date.strftime("The person was seen on %Y/%m/%d at %H hours, %M minutes on the street of D.C.")) to print “The person was seen on 2022/01/25 at 15 hours, 53 minutes on the street of D.C.”

Note: %Y = year, %m = month, %d = day, %H = hours, %M = minutes, %S = seconds


  • timedelta module
import datetime as dt
time_diff = dt.timedelta(hours = 10)
date_new = date + time_diff
print(date_new)

Prints out 2022-01-26 01:53:43, which is 10 hours ahead of the original date.

- Using `total_seconds()` method on a timedelta object give total seconds elapsed between the two datetime objects compared in the timedelta object. For example, `time_diff.total_seconds()` will give 36000 seconds (10 hours * 60 * 60)

  • timezone module
import datetime as dt
ET = dt.timezone(dt.timedelta(hours=-5)) # Eastern standard time, timezone object
#adding timezone to date info
dt_with_tz = date.astimezone(ET)
print(dt_with_tz)

prints out: 2022-01-25 15:53:43-05:00. Now, timezone can be replaced using .replace(tzinfo="new_time_zone") method applied on the datetime object. UTC timezone can be accessed using dt.timezone.utc attribute.

  • Timezone database in the package dateutil and module tz
from dateutil import tz
timezone_object_et = tz.gettz('America/New York')

which creates a timezone object for Eastern time for ‘America/New York’. Then this timezone object can be applied to a datetime object using tzinfo = "timezone_object_et" attribute.

- Timezone set using the `tz` module will account for Daylight time changes

Handling datetime with pandas

  • Pandas can import datetime using parse_dates = ["start time","end time"] in the pd.read_csv() function where pandas tries to be smart to parse the format
  • To specify the format, each column can be read first as string and then converted to datetime object using pd.to_datetime(dataframe["start time"],format = "%Y-%m-%d, %H:%M:%S") function
  • All other functions can be performed in pandas using the same methods as used in timedelta, datetime, and timezone object, but pandas require the use of .dt. before a method (You can think of this as if pandas imports the datetime package as dt).
  • Pandas also offers the ability to resample based on Month (M), Date(D) etc. on a particular columns, perform analysis and plot the data, all in one line. For example, dataframe.resample("M",on="start time").size().plot(ylim=[0 15]) will pick months from the start time column, get the size (in this case total number of values for each month on the “start time” column), and plot it as a line plot!
  • It is also possible to use groupby function
  • Use .dt.tz_localize("America/New_York") to set timezone, and .dt.tz_convert("Europe/London") to convert the time zone.