Preview:
from datetime import date
from datetime import timedelta, timezone
from datetime import datetime  # yes, it's the same name


# "date" is a date class, same as "str"

# Create a date object
today = date(1992, 8, 24)

# same with datetime
dt = datetime(2017, 10, 1, 15, 23, 25)

# replace
dt = dt.replace(minute=0, second=0, microsecond=0)

# Subtract two dates
delta = d2 - d1
print(delta.days)29

# convert column to datetime
pd.to_datetime(df["col"])  # use errors='coerce' if NANs

# convert string to datetime
df['col1'].strftime('%B %d, %Y, %r')
df['col1'].strftime("%Y-%m-%d %H:%M:%S"))

df['col1'].strftime('d/%m/%Y')
# also possible
today.strftime('Year is %Y')

# get e.g. year from datetime column
df['col1'] = df['col1'].dt.strftime('%Y')

# use strptime for the other way around (datetime to string)

# extract date from datetime64 object and keep datetime64 format
df['date'] = df['datetime'].dt.normalize()

# set datetime index

# slice datetime index
slice = df['2021-01-01':'2022-01-01']

# print as isoformat
dt.isoformat()

# parse datetimes
dt = datetime.strptime("12/30/2017 15:19:13", "%m/%d/%Y %H:%M:%S")

# create timedelta
delta = timedelta(days=1, seconds=1)
today + delta = new time

#resample on months and plot
df.resample('M', on="col1").size().plot()
plt.show()

#weekday name
dt.day_name()
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter