Chapter4-Handling Dates and Times
Chapter4-Handling Dates and Times
Again a DateTime
Journey
D ATA T Y P E S F O R D ATA S C I E N C E I N P Y T H O N
Jason Myers
Instructor
From string to datetime
The datetime module is part of the Python standard library
06/11/2016
2016-06-11 00:00:00
date_dt.strftime('%m/%d/%Y')
'06/11/2016'
date_dt.isoformat()
'2016-06-11T00:00:00'
Jason Myers
Instructor
Datetime Components
day , month , year , hour , minute , second , and more are
available from a datetime instance
daily_violations = defaultdict(int)
for violation in parking_violations:
violation_date = datetime.strptime(violation[4],
'%m/%d/%Y')
daily_violations[violation_date.day] += 1
2017-05-05 12:30:00.740415
2017-05-05 17:30:05.467221
2016-07-12 04:39:00-04:00
print(la_dt)
2016-07-12 01:39:00-07:00
Jason Myers
Instructor
Incrementing through time
timedelta is used to represent an amount of change in time
2016-07-12 04:39:00
2016-04-13 04:39:00
print(record_dt + flashback)
2016-10-10 04:39:00
datetime.timedelta
print(time_diff)
0:00:04
Jason Myers
Instructor
Parsing time with pendulum
.parse() will a empt to convert a string to a pendulum
datetime object without the need of the format string
import pendulum
print(occured_dt)
'2016-06-11T14:38:00-04:00'
print(violation_dts)
[<Pendulum [2016-06-11T14:38:00-04:00]>,
<Pendulum [2016-04-25T14:09:00-04:00]>,
<Pendulum [2016-04-23T07:49:00-04:00]>,
<Pendulum [2016-04-26T07:09:00-04:00]>,
<Pendulum [2016-01-04T09:52:00-05:00]>]
2016-06-12T03:38:00+09:00
2016-04-26T03:09:00+09:00
2016-04-23T20:49:00+09:00
2016-04-26T20:09:00+09:00
2016-01-04T23:52:00+09:00
print(pendulum.now('Asia/Tokyo'))
<Pendulum [2017-05-06T08:20:40.104160+09:00]>
print(diff.in_words())
print(diff.in_hours())
71