How to return the ISO 8610 format for a timestamp object

Overview

The format of ISO 8610 format is given as YYY-MM-DD HH:MM:SS.mmmmmmnnn. To convert a Timestamp object—an equivalent of Python's datetime object— to the ISO 8610 format in pandas, we use the isoformat() function.

Syntax

Timestamp.isoformat()
Syntax for the isoformat() function

Parameters

This function takes the following optional parameter values:

  • sep: This is a string to be used in the date and time fields.
  • timespec: This takes any of the following values—"seconds", "milliseconds", "microseconds", "nanoseconds", "minutes", "hours", and "auto". They specify which of the terms of the time listed to be included in the output. By default, it takes the "auto" value.

Return value

This function returns a string representing the ISO format for the input Timestamp object.

Example

# A code to illustrate the .is_year_end attribute in Pandas
# importing the pandas library
import pandas as pd
# creating a Timestamp object
a = pd.Timestamp(2022, 4, 29)
# obtaining the date time in ISO 8610 format
my_date = a.isoformat("*")
print(my_date)

Explanation

  • Line 3: We import the pandas library.
  • Line 6: We create a Timestamp object, a.
  • Line 9: We use the isoformat() function to return the ISO 8610 format of the input Timestamp object. We assign the result to a variable, my_date.
  • Line 11: We print the value of the variable, my_date.