Datetime Processing in Python

common datetime data structure:

int/float : a) timestamp, seconds since JAN 01 1970. (UTC), short for ts
      b) ordinal, days since JAN 01 0. short for to
datetime.datetime : short for t, datetime
string : printable datetime string(usually contains year,month,...seconds)

base imports

import time
from datetime import datetime, timedelta
import matplotlib.dates as dates

import pytz

from dateutil import rrule
from dateutil.parser import parse

common convert functions

# gen sample t
t = datetime.now() # prec:1e-6s
t = datetime.today()# same as above
t1 = datetime(2022,6,10,2,22,2) # from struct function
t2 = t + timedelta(seconds=-11)
ts = time.time() #prec:1e-7s
# ts >> to
to = ts.ordinal(t) #int
to = dates.date2num(t) #float
# datetime >> string
tstr = datetime.strftime(t, "%Y%m%d%H%M%S%f")
# string >> datetime, this is a class function
t = datetime.strptime(str, "%Y%m%d%H%M%S%f")
# datetime <> ts
ts = t.timestamp()
t = datetime.fromtimestamp(ts)

set timezone

for tz in pytz.all_timezones:
    print(tz)
# generally use "Etc/GMT +n"即可
tstr = "2018-04-20 10:10:05"
t = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S") 
ta = pytz.timezone("Etc/GMT+8").localize(t)
tb = ta.astimezone(pytz.timezone("Etc/GMT+8"))
#t.timestamp()==ta.==tb. this is a bug
t.strftime()==ta.strftime()!=tb.strftime()

gen time series

# using pandas to gen sample t series
time_index = pd.date_range('06/06/2017', periods=100000, freq='30S')
# using rrule 
dtstr = "2022-06-10"
m1 = rrule.rrule(dtstart=parse(dtstr + " 09:31:00"), until=parse(dtstr + " 11:30:00"), freq=rrule.MINUTELY, interval=1, cache=True)
mms = list(m1)

set pandas datetime index

## converts:

# timestring Series >> DateTime Series
tseries = pd.to_datetime(df["logtime"], format="%Y%m%d%H%M%S%f",unit='s',errors="coerce"))
# DateTime Series >> DateTimeIndex Series
index = pd.DatetimeIndex(tseries)

# set index
dataframe = pd.DataFrame(index=time_index) #set when Generated
df.set_index(index, inplace=True)
df.index.set_names(["dt"],inplace=True)

TODO

set matplotlib index

10
Subscribe to my newsletter

Read articles from Faustellar Zhang directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Faustellar Zhang
Faustellar Zhang