Reprezentarea timpului cu datetime

Python intermediar pentru finanțe

Kennedy Behrman

Data Engineer, Author, Founder

Datetime-uri

Grafic cu prețul acțiunilor în creștere

Python intermediar pentru finanțe

Datetime-uri

Grafic cu deficitul comercial al Cubei

Python intermediar pentru finanțe

Datetime-uri

from datetime import datetime
black_monday = datetime(1987, 10, 19)
print(black_monday)
datetime.datetime(1987, 10, 19, 0, 0)
Python intermediar pentru finanțe

Datetime curent

datetime.now()
datetime.datetime(2019, 11, 6, 3, 48, 30, 886713)
Python intermediar pentru finanțe

Datetime din șir de caractere

black_monday_str = "Monday, October 19, 1987. 9:30 am"
format_str = "%A, %B %d, %Y. %I:%M %p"
datetime.datetime.strptime(black_monday_str, format_str)
datetime.datetime(1987, 10, 19, 9, 30)
Python intermediar pentru finanțe

Datetime din șir de caractere

An

  • %y Fără secol (01, 02, ..., 98, 99)
  • %Y Cu secol (0001, 0002, ..., 1998, 1999, ..., 9999)

Lună

  • %b Nume abreviate (Jan, Feb, ..., Nov, Dec)
  • %B Nume complete (January, February, ... November, December)
  • %m Ca numere (01, 02, ..., 11, 12)

Zi din lună

  • %d (01, 02, ..., 30, 31)
Python intermediar pentru finanțe

Datetime din șir de caractere

Zi din săptămână

  • %a Nume abreviat (Sun, ... Sat)
  • %A Nume complet (Sunday, ... Saturday)
  • %w Număr (0, ..., 6)

Oră

  • %H Format 24h (00, 01, ... 23)
  • %I Format 12h (01, 02, ... 12)
  • %M (01, 02, ..., 59)
Python intermediar pentru finanțe

Datetime din șir de caractere

Secunde

  • %S (00, 01, ... 59)

Microsecunde

  • %f (000000, 000001, ... 999999)

AM/PM

  • %p (AM, PM)
Python intermediar pentru finanțe

Datetime din șir de caractere

%m Luni

%M Minute

Python intermediar pentru finanțe

Datetime din șir de caractere

"1837-05-10"

%Y

%m

%d

"%Y-%m-%d"
Python intermediar pentru finanțe

Datetime din șir de caractere

"Friday, 17 May 01"

%A

%d

%B

%y

"%A, %d %B %y"
Python intermediar pentru finanțe

Șir de caractere din datetime

dt.strftime(format_string)
Python intermediar pentru finanțe

Șir de caractere din datetime

great_depression_crash = datetime.datetime(1929, 10, 29)
great_depression_crash
datetime.datetime(1929, 10, 29, 0, 0)
great_depression_crash.strftime("%a, %b %d, %Y")
'Tue, Oct 29, 1929'
Python intermediar pentru finanțe

Să exersăm!

Python intermediar pentru finanțe

Preparing Video For Download...