> Getting the current date and time > Arithmetic
# Get the current date
# Get the current date and time
# Create two datetimes
dt.date.today()
dt.datetime.now()
now = dt.datetime.now()
Working with Dates
then = pd.Timestamp('2021-09-15 10:03:30')
and Times in Python
> Reading date, datetime, and time columns in a CSV file # Get time elapsed as timedelta object
now - then
# Specify datetime column
# Get time elapsed in seconds
Learn Python Basics online at www.DataCamp.com pd.read_csv("filename.csv", parse_dates = ["col1", "col2"])
(now - then).total_seconds()
# Specify datetime column
# Adding a day to a datetime
pd.read_csv("filename.csv", parse_dates = {"col1": ["year", "month", "day"]})
dt.datetime(2022,8,5,11,13,50) + dt.timedelta(days=1)
Key
> Key definitions > Time Zones
When working with dates and times, you will encounter technical terms and
> Parsing dates, datetimes, and times # Get current time zone
jargon such as the following: tm.localtime().tm_zone
# Parse dates in ISO format
pd.to_datetime(iso)
# Get a list of all time zones
Date: Handles dates without time pytz.all_timezones
POSIXct: Handles date & time in calendar time # Parse dates in US format
POSIXlt: Handles date & time in local time pd.to_datetime(us, dayfirst=False)
# Parse strings to datetimes
Hms: Parses periods with hour, minute, and secon dttm = pd.to_datetime(iso)
Timestamp: Represents a single pandas date & tim # Parse dates in NON US format
pd.to_datetime(non_us, dayfirst=True)
# Get datetime with timezone using location
Interval: Defines an open or closed range between dates and time
dttm.dt.tz_localize('CET', ambiguous='infer')
Time delta: Computes time difference between different datetimes
# Parse dates, guessing a single format
pd.to_datetime(iso, infer_datetime_format=True)
# Get datetime with timezone using UTC offset
dttm.dt.tz_localize('+0100')
The
> The ISO8601 datetime format # Parse dates in single, specified format
pd.to_datetime(iso, format="%Y-%m-%d %H:%M:%S")
# Convert datetime from one timezone to another
dttm.dt.tz_localize('+0100').tz_convert('US/Central')
The ISO 8601 datetime format specifies datetimes from the largest to the # Parse dates in single, specified format
smallest unit of time (YYYY-MM-DD HH:MM:SS TZ). Some of the advantages of pd.to_datetime(us, format="%m/%d/%Y %H:%M:%S")
ISO 8601 are
# Make dates from components
> Time Intervals
pd.to_datetime(parts)
It avoids ambiguities between MM/DD/YYYY and DD/MM/YYYY formats # Create interval datetimes
The 4-digit year representation mitigates overflow problems after the year 2099 start_1 = pd.Timestamp('2021-10-21 03:02:10')
Using numeric month values (08 not AUG) makes it language independent, so
dates make sense throughout the world
> Extracting components finish_1 = pd.Timestamp('2022-09-15 10:03:30')
start_2 = pd.Timestamp('2022-08-21 03:02:10')
Python is optimized for this format since it makes comparison and sorting easier. finish_2 = pd.Timestamp('2022-12-15 10:03:30')
# Parse strings to datetimes
dttm = pd.to_datetime(iso)
# Specify the interval between two datetimes
> Packages used in this cheat sheet
pd.Interval(start_1, finish_1, closed='right')
# Get year from datetime pandas series
dttm.dt.year
# Get the length of an interval
pd.Interval(start_1, finish_1, closed='right').length
Load the packages and dataset used in this cheatsheet.
# Get day of the year from datetime pandas series
dttm.dt.day_of_year
# Determine if two intervals are intersecting
import datetime as dt
pd.Interval(start_1, finish_1,
import time as tm
# Get month name from datetime pandas series
closed='right').overlaps(pd.Interval(start_2, finish_2, closed='right'))
import pytz
dttm.dt.month_name()
import pandas as pd
In this cheat sheet, we will be using 3 pandas series — iso, us, non_us,
# Get day name from datetime pandas series
dttm.dt.day_name()
> Time Deltas
and 1 pandas DataFrame parts
# Get datetime.datetime format from datetime pandas series
# Define a timedelta in days
dttm.dt.to_pydatetime()
pd.Timedelta(7, "d")
iso us non_us # Convert timedelta to seconds
1969-07-20 20:17:40 07/20/1969 20:17:40 20/07/1969 20:17:40
> Rounding dates pd.Timedelta(7, "d").total_seconds()
1969-11-19 06:54:35 11/19/1969 06:54:35 19/11/1969 06:54:35
1971-02-05 09:18:11 02/05/1971 09:18:11 05/02/1971 09:18:11 # Rounding dates to nearest time unit
dttm.dt.round('1min')
parts
# Flooring dates to nearest time unit
Learn Data Skills Online at
year month day dttm.dt.floor('1min')
www.DataCamp.com
1969 7 20
# Ceiling dates to nearest time unit
1969 11 19 dttm.dt.ceil('1min')
1971 2 5