HKUST2023 Python HSC Lecture2
HKUST2023 Python HSC Lecture2
FINANCE IN PYTHON
Lecture II – File I/O and Time Series Analysis in
Python
MAFS 5360 – Summer, 2023
1
Copyright © by Dr. Hongsong Chou, 2019 - 2023. No part of this material may be: (i) copied, photocopied, or duplicated in any form, by any means, or (ii)
redistributed without prior expressed consent from the author. The views expressed here are those of the author himself and himself only.
AGENDA
• Datetime in Python
2
(TENTATIVE) OUTLINE OF THE COURSE
• Lecture 1 (June 20/27): Basic Python programming – Data Structure
and object-oriented programming
• Lecture 2 (June 29/July 6): Utilities in Python programming – File I/O
and financial time series handling
• Lecture 3 (July 8/13): Discussions on student-led projects, and
project kick-offs;
• Lecture 4 (July 15/20): Machine learning through Python
programming – packages and best practices
• Lecture 5 (July 22/27): Parallel computing and performance
optimization in Python programming
• Student-led presentations - Team A (August 3)
• Student-led presentations - Team B (August 5)
4
DATE/TIME
• Rather powerful implementation of date/time in Python (personal
view: a lot better than Java on this regard);
• Mostly in the datetime package:
import datetime
thisDay = datetime(2019, 6, 27)
5
CONVERT TO STRING FORMATS
• datetime.strftime()
Directive Meaning Example
%a Abbreviated weekday name. Sun, Mon, ...
%A Full weekday name. Sunday, Monday, ...
%w Weekday as a decimal number. 0, 1, ..., 6
%d Day of the month as a zero-padded decimal. 01, 02, ..., 31
%-d Day of the month as a decimal number. 1, 2, ..., 30
%b Abbreviated month name. Jan, Feb, ..., Dec
%B Full month name. January, February, ...
%m Month as a zero-padded decimal number. 01, 02, ..., 12
%-m Month as a decimal number. 1, 2, ..., 12
%y Year without century as a zero-padded decimal number. 00, 01, ..., 99
%-y Year without century as a decimal number. 0, 1, ..., 99
%Y Year with century as a decimal number. 2013, 2019 etc.
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, ..., 23
%-H Hour (24-hour clock) as a decimal number. 0, 1, ..., 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, ..., 12
%-I Hour (12-hour clock) as a decimal number. 1, 2, ... 12
%p Locale’s AM or PM. AM, PM
%M Minute as a zero-padded decimal number. 00, 01, ..., 59
%-M Minute as a decimal number. 0, 1, ..., 59
%S Second as a zero-padded decimal number. 00, 01, ..., 59
%-S Second as a decimal number. 0, 1, ..., 59
%f Microsecond as a decimal number, zero-padded on the left. 000000 - 999999
%z UTC offset in the form +HHMM or -HHMM.
%Z Time zone name.
%j Day of the year as a zero-padded decimal number. 001, 002, ..., 366
%-j Day of the year as a decimal number. 1, 2, ..., 366
%U
Week number of the year (Sunday as the first day of the week). All days in a new year
00, 01, ..., 53
preceding the first Sunday are considered to be in week 0.
Week number of the year (Monday as the first day of the week). All days in a new year
6
%W 00, 01, ..., 53
preceding the first Monday are considered to be in week 0.
%c Locale’s appropriate date and time representation. Mon Sep 30 07:06:05 2013
%x Locale’s appropriate date representation. 9/30/2013
%X Locale’s appropriate time representation. 7:06:05
%% A literal '%' character. %
AGENDA
• Datetime in Python
7
FILE OBJECTS IN PYTHON
• FILE is an object exposing a file-oriented API (with methods such
as read() or write()) to an underlying resource;
• Three categories of file objects: raw binary files, buffered binary files
and text files;
• open() returns a file object, and is most commonly used with two
arguments: open(filename, mode); modes can be:
- ‘r’: read only;
- ‘w’: write only;
- ‘r+’: both reading and writing;
- ‘b’: binary mode;
- ‘a’: appending mode;
- default: ‘r’;
• Attributes of file objects:
- file.closed, file.mode, file.name;
• Methods of file objects:
8
- read(size), readline(), write(stream),
seek(), tell(), close().
PATTERN MATCHING
• Python – like many other modern languages – provide string
manipulation tools:
- search, match, sub, split;
• Simple matching case:
- “foo” will match “foo” and only that string;
- “[abc]” matches ‘a’, ‘b’ or ‘c’;
- “[^abc]” matches anything but ‘a’, ‘b’ or ‘c’;
- “.” matches any single character;
- “(abc)+” mathes ‘abc’, ‘abcabc’,…
- “x|y” matches ‘x’ or ‘y’;
- “ab*” matches ‘a’, ‘ab’, or ‘a’ followed by any
number of ‘b’s;
• More at https://docs.python.org/3/library/re.html or search
“regular expression” on Google;
• See attached jupyter notebook: FileIO_Python.ipynb.
9
FILE DIRECTORY NAVIGATION
• The os package provides a portable way of using operating system
dependent functionality;
• Details can be found at: https://docs.python.org/3/library/os.html
• Here are a few methods that we will use in this lecture:
- os.getcwd();
- os.chdir(path);
- os.mkdir(path);
- os.mkdirs(path);
- os.rename(src, dest);
- os.replace(src, dest);
- os.remove(path);
- os.listdir(path);
- os.listdirs(path);
- os.stat(path);
- os.stat_result;
10
- os.stat_result.st_size.
AGENDA
• Datetime in Python
11
TIME SERIES ANALYSIS TUTORIAL
• (see attached jupyter notebook for this tutorial:
TimeSeriesAnalysisInPython.ipynb)
12
THAT’S ALL FOR THIS LECTURE.
THANK YOU!
13