pydata

Keep Looking, Don't Settle

python numpy pandas matplotlib date time change

A lot of the time it is necessary to process date and time data in python and there are a lot of packeges in python can deal with date and time, like time, datetime, or matplotlib.dates and so on. Here is the summary for overview.

  • Date and time representation
  • string format of date/time
  • python datetime object
  • unix time
  • matplotlib date representaion
  • numpy datetime
  • pandas datetime

  • Date conversion

  • conversion between string and datetime
  • conversion between unix time and datetime
  • conversion between struct_time and datetime
  • conversion between matplotlib time and datetime

  • Reference

1. different date / time format

1.1 string time

string is like which is of the formar of ISO. Like x = '2016-03-20 15:29:28' or x = '20Mar2016 15:29:28' or x = '2016/03/20 15:29:28'.

The standard library time.strftime(fmt) in python will output this format.

import time
print time.strftime('%Y-%m-%d %H:%M:%S')
print time.strftime('%d%b%Y %H:%M:%S')
print time.strftime('%Y/%m/%d')
2016-03-29 16:37:43
29Mar2016 16:37:43
2016/03/29

1.2 python datetime object

this is from python datetime standard module with datetime class.

import datetime 
datetime.datetime.now()
datetime.datetime(2016, 3, 29, 16, 34, 1, 798000)

1.3 unix time

unix time starts from the begining of 1970 and counts the elapsed seconds.

time.time()
1459287621.612

1.4 matplotlib date

matplotlib.pyplot has the function plt.plot_date() to draw time series data like stock price. matplotlib.dates has function num2date to convert datetime objects to matplotlib time format.

import matplotlib.dates as mdates
mdates.date2num(datetime.datetime.now())
736052.6975765047

1.5 numpy datetime

numpy accepts date or datetime in string format in ISO 8601.

import numpy as np
print np.datetime64('2005-02-25')
print np.array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64')
print np.array(['2007-07-13', '2006-01-13', '2010-08-13'], dtype='datetime64[D]')
2005-02-25
['2007-07-13' '2006-01-13' '2010-08-13']
['2007-07-13' '2006-01-13' '2010-08-13']

2. date / time conversion

2.1.1 string time and datetime

datetime.datetime.strptime('2016-03-29 16:37:43', '%Y-%m-%d %H:%M:%S')
datetime.datetime(2016, 3, 29, 16, 37, 43)

2.1.2 string can also be converted to struct_time

time.strptime('2016-03-29 16:37:43', '%Y-%m-%d %H:%M:%S')
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=29, tm_hour=16, tm_min=37, tm_sec=43, tm_wday=1, tm_yday=89, tm_isdst=-1)

2.2 conversion between unix time and datetime

datetime.datetime.fromtimestamp(1459287621.612)
datetime.datetime(2016, 3, 29, 16, 40, 21, 612000)

2.3.1 conversion from struct_time to datetime

x = time.localtime()
print x
datetime.datetime(*x[:6])
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=29, tm_hour=17, tm_min=32, tm_sec=30, tm_wday=1, tm_yday=89, tm_isdst=1)

datetime.datetime(2016, 3, 29, 17, 32, 30)

2.3.2 conversion from datetime to struct_time with timetuple()

t = datetime.datetime.now()
print t
print t.timetuple()
2016-03-29 17:32:56.001000
time.struct_time(tm_year=2016, tm_mon=3, tm_mday=29, tm_hour=17, tm_min=32, tm_sec=56, tm_wday=1, tm_yday=89, tm_isdst=-1)

2.4.1 conversion from matplotdate time to datetime with mdates.num2date() function

mdates.num2date(736052.6975765047)
datetime.datetime(2016, 3, 29, 16, 44, 30, 610003, tzinfo=<matplotlib.dates._UTC object at 0x0000000004996BE0>)

2.4.2 conversion from datetime to matplotlib time

mdates.date2num(datetime.datetime.now())
736052.7348223958