pandas-timeseries-1


To understand how Pandas works with time series, we need to understand the datetime module of Python. The main object types of this module are below

  • date - stores year, month, day using Gregorian calendar
  • time - stores hours, minutes, seconds, microseconds
  • datetime - both date and time
  • timedelta - difference between two datetime values - represented as days, seconds, microseconds

datetime module


In [1]:
from datetime import datetime

In [2]:
now = datetime.now()
now


Out[2]:
datetime.datetime(2020, 3, 12, 15, 51, 21, 230960)

In [5]:
(now.year, now.month, now.day, now.hour, now.minute)


Out[5]:
(2020, 3, 12, 15, 51)

Time delta

You can get the time difference between two dates as timedelta objects


In [6]:
delta = datetime(2020,3,12) - datetime(2020,9,25)
delta


Out[6]:
datetime.timedelta(days=-197)

In [8]:
(delta.days, delta.seconds)


Out[8]:
(-197, 0)

Shifting time with timedelta


In [9]:
from datetime import timedelta

In [12]:
datetime.now() + timedelta(days=25)


Out[12]:
datetime.datetime(2020, 4, 6, 15, 57, 52, 925891)

In [13]:
datetime.now() - timedelta(days=20, hours=5)


Out[13]:
datetime.datetime(2020, 2, 21, 10, 58, 23, 219047)

Working with time stamps


In [16]:
stamp = datetime.now()
str(stamp)


Out[16]:
'2020-03-12 16:07:14.063049'

Printing time stamps


In [24]:
stamp.strftime('%Y-%m-%d-%H-%M-%S')


Out[24]:
'2020-03-12-16-07-14'

The table below gives most popular format strings to use with strftime.

  • %Y - 4 digit year
  • %y - 2 digit year
  • %m - 2 digit month [02,10]
  • %d - 2 digit day [01,22]
  • %H - hours in 24 hour clock, 2 digits
  • %I - hours in 12 hour clock, 2 digits
  • %M - minutes, 2 digits
  • %S - seconds, 2 digits
  • %w - weekday as integer [0 (sun) - 6 (sat)]
  • %U - week number, 2 digits, from 00-53. First Sunday of the year is the start of the week (1) and days before belong to week 0
  • %W - week number where Monday is the start of the week
  • %z - UTC time zone offset as +HHMM or -HHMM, empty is time zone is unknown (naive)
  • %F - shortcut for %Y-%m-%d (2020-03-12)
  • %D - shortcut for %m/%d/%y (03/12/20)

If you notice, year, month, date are lower while hour, min, sec are upper cases.


In [28]:
datetime.now().strftime('Current hour is %I and week number is %U')


Out[28]:
'Current hour is 04 and week number is 10'

In [29]:
datetime.now().strftime('%z')


Out[29]:
''

In [30]:
datetime.now().strftime('%D')


Out[30]:
'03/12/20'

Reading timestamps into datetime objects using strptime()


In [33]:
stamp_str = '03/12/20'
datetime.strptime(stamp_str, '%m/%d/%y')


Out[33]:
datetime.datetime(2020, 3, 12, 0, 0)

Parsing with dateutil package

dateutil is a 3rd party package and allows you to parse common date formats without explicitly stating the format.


In [34]:
from dateutil.parser import parse

In [35]:
parse('03/12/20')


Out[35]:
datetime.datetime(2020, 3, 12, 0, 0)

In [ ]: