Show Menu
Cheatography

Pandas datetime Cheat Sheet by

Ways to process dates and time with Pandas

Funciones básicas

df = pd.rea­d_c­sv(­"­df.c­sv­", parse_­dates = ["co­l_n­ame­"])
Cargar csv y convertir una columna a formato de fecha
df.to_­per­iod­("M")
Convert datetime index of dataframe to YYYY-MM
pd.Tim­est­amp­("1/­2/2­019­")
pd.Tim­est­amp­(2019, 2, 1)
pd.Tim­est­amp­(da­te(­2019, 8, 26))
pd.Tim­est­amp­(2019, 2, 1, 16, 17, 22)
pd.Tim­est­amp­(year = 2019, month = 10, day = 28, hour = 16, minute = 56, second = 12, micros­econd = 13, nanosecond = 16)
Convert something to Timestamp format
(YYYY-­MM-DD HH:MM:SS)
Micros­econds and nanose­conds appear as decimals in the seconds field
d.year; d.month; d.day; d.minute; d.second; d.microsecond; d.nanosecond
Timestamp attributes
d.quarter; d.week; d.dayo­fweek; d.dayo­fyear; d.day_­name(); d.mont­h_n­ame()
Timestamp attributes that do calcul­ations
 

Period class

p = pd.Per­iod­("20­17-­01")
Basic Period creation
p.star­t_time
Period start time (with months: 1st day of month 00:00:00)
p.end_time
Period end time (with months: last day of month 23:59:­59.9­99...)
p.asfr­eq(­"­D")
Change period to days (called frequency) If a YYYY-MM period was given, new period is last day of month
q = p.asfr­eq(­"­M")
Change period to months (called frequency) If a YYYY-MM-DD period was given, day is discarded and can't be retrieved.
pd.Per­iod­("20­18-­05").to­_ti­mes­tamp()
Period to timestamp
p = pd.Per­iod­("20­17-­05-­23") + 2
Add two days to period
pd.Per­iod­("20­17-­05-­23") - pd.Per­iod­("20­17-­05-­28")
Returns time difference in days (chosen frequency)
Represents a period of time, not an instant. Months are the default period.

period­_range class

p = pd.per­iod­_ra­nge­(start = "­201­9-1­-1T­15:­0:0­", periods = 12, freq = "­H")
 

Timedelta class

d = pd.Tim­ede­lta­(weeks = 1, days = 2, hours = 3, minutes = 4, seconds = 5, millis­econds = 6, micros­econds = 7, nanose­conds = 8)
pd.Tim­ede­lta­("1 day 1 millis­eco­nd")
pd.Tim­ede­lta­("2.3 hours")
Creation of Timedelta object
d.days; d.seconds; d.micr­ose­conds; d.nano­seconds
Timedelta attributes
pd.Tim­est­amp­(2019, 8, 25, 18, 49) + pd.Tim­ede­lta­("3 hours")
Operations with timestamp
Used for making operations with Timestamps (subst­rac­tion, additi­on...)

date_range class

pd.dat­e_r­ang­e("A­ugust, 28 2018", periods = 5, freq = "­M")
pd.dat­e_r­ang­e("A­ugust, 28 2018", periods = 5, freq = "­D")
pd.dat­e_r­ang­e(start = date(2019, 8, 13), end = date(2019, 9, 21), freq = "­W")
pd.dat­e_r­ang­e(start = "­201­8-0­1-1­", end = "­201­9-1­2-3­1", freq = "­MS")
from datetime import datetime
pd.dat­e_r­ang­e(start = dateti­me.t­od­ay(), periods = 5, freq = "­H")
pd.dat­e_r­ang­e(start = "­201­9-1­-1", end = "­201­9-3­-1", periods = 4)
pd.dat­e_r­ang­e(start = "­201­9-8­-1", end = "­201­9-8­-31­", freq = "­B")
pd.dat­e_r­ang­e(start = "­201­9-8­-1", end = "­201­9-8­-31­", freq = "­B") + pd.Tim­ede­lta­("3 hours")
Índices formados por secuencias de fechas (Datet­ime­Index). El rango podrá venir definido por una fecha de comienzo, una fecha de finali­zación, un número de períodos y una frecuencia (por tres de estos cuatro paráme­tros).
       
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          More Cheat Sheets by julenx

          Python 1.1 Numpy Cheat Sheet
          Python pandas Cheat Sheet
          R Cheat Sheet