Want to parse a Python string into a datetime? Use strptime, passing (a) the string and (b) the date format specification: Both return datetime.datetime(2026, 4, 1, 0, 0)
Want to format a Python datetime as a string? Use strftime:
Want a Python datetime with a timezone? Use the “zoneinfo” module: Then pass it to the “tzinfo” keyword arg:
Want a Python datetime? Just invoke datetime.datetime: Omitted time units are 0:
Want to compare two Python datetimes? Just subtract, getting a timedelta:
Need the current date and time in Python?
Generate a Python Pandas DatetimeIndex of datetime values, specifying the frequency, by passing freq and a code: This returns a 184-element DatetimeIndex with evenly-spaced datetime values in that period.
Generate a Python Pandas DatetimeIndex of datetime values with pd.date_range: This returns a 10-element series with evenly-spaced datetime values in that period.
Have a Python Pandas series with datetime values, and want all those until now? Compare with pd.Timestamp.now(): This returns the rows from df where the “when” column is before now.
Want to check whether a Python Pandas series contains another string? Use .str.contains: This returns a boolean series, whose index matches that of df. Keep only those rows containing ‘a’: