Pandas Tutorial

Creating Objects

Viewing Data

Selection

Manipulating Data

Grouping Data

Merging, Joining and Concatenating

Working with Date and Time

Working With Text Data

Working with CSV and Excel files

Operations

Visualization

Applications and Projects

Convert timestamp to ISO Format in Pandas

Converting a timestamp to ISO 8601 format is a common task when dealing with datetime data, especially if you need to ensure consistent and standardized datetime formats. Here's a tutorial to help you convert timestamps to ISO 8601 format using pandas.

Convert Timestamp to ISO 8601 Format in Pandas

1. Setup:

Make sure you have pandas installed:

pip install pandas

2. Import Necessary Libraries:

import pandas as pd

3. Create a Sample Timestamp:

You can create a sample timestamp using pd.Timestamp:

timestamp = pd.Timestamp('2023-08-31 12:45:30')
print(timestamp)

4. Convert Timestamp to ISO Format:

Pandas Timestamp objects have a method called isoformat() that converts them to ISO 8601 format:

iso_format = timestamp.isoformat()
print(iso_format)

This will give you an output like '2023-08-31T12:45:30', which is in the ISO 8601 format.

5. Convert a Series of Timestamps to ISO Format:

If you have a Series of Timestamps and you want to convert all of them to ISO format, you can apply the isoformat() method to the entire Series:

timestamps_series = pd.Series([pd.Timestamp('2023-08-31 12:45:30'), pd.Timestamp('2023-09-01 14:15:45')])
iso_series = timestamps_series.apply(lambda x: x.isoformat())
print(iso_series)

6. Working with ISO 8601 Format with Time Zones:

ISO 8601 format can also include timezone information:

timestamp_with_timezone = pd.Timestamp('2023-08-31 12:45:30', tz='US/Eastern')
iso_format_with_timezone = timestamp_with_timezone.isoformat()
print(iso_format_with_timezone)

This will give you an output like '2023-08-31T12:45:30-04:00', which includes the offset for the US Eastern timezone.

7. Summary:

The ISO 8601 datetime format is a popular choice for ensuring consistent datetime representation across different systems. With pandas, converting to this format is straightforward using the isoformat() method. Whether you're working with individual timestamps or a Series of them, pandas provides an easy way to get them in ISO 8601 format.

  1. Convert Pandas timestamp to ISO datetime:

    • Description: Use the .isoformat() method to convert a Pandas timestamp to ISO datetime format.
    • Code:
      import pandas as pd
      
      # Sample timestamp
      timestamp = pd.Timestamp('2022-01-01 12:00:00')
      
      # Convert timestamp to ISO datetime
      iso_datetime = timestamp.isoformat()
      
  2. ISO format conversion in Pandas DataFrame:

    • Description: Use the pd.to_datetime() function to convert a column in a Pandas DataFrame to ISO datetime format.
    • Code:
      import pandas as pd
      
      # Sample DataFrame with a datetime column
      df = pd.DataFrame({'datetime': ['2022-01-01 12:00:00', '2022-01-02 15:30:00']})
      
      # Convert column to ISO datetime format
      df['iso_datetime'] = pd.to_datetime(df['datetime']).dt.isoformat()
      
  3. Using to_iso8601 for timestamp conversion in Pandas:

    • Description: Use the .dt.to_iso8601() method to convert a timestamp to ISO 8601 format in Pandas.
    • Code:
      import pandas as pd
      
      # Sample timestamp
      timestamp = pd.Timestamp('2022-01-01 12:00:00')
      
      # Convert timestamp to ISO 8601 format
      iso8601_format = timestamp.dt.to_iso8601()
      
  4. Convert Unix timestamp to ISO format in Pandas:

    • Description: Use the pd.to_datetime() function to convert a Unix timestamp to ISO datetime format in Pandas.
    • Code:
      import pandas as pd
      
      # Sample Unix timestamp
      unix_timestamp = 1641000000  # Example timestamp for 2022-01-01 00:00:00
      
      # Convert Unix timestamp to ISO format
      iso_datetime = pd.to_datetime(unix_timestamp, unit='s').isoformat()
      
  5. Datetime to ISO string in Pandas:

    • Description: Use the .dt.strftime('%Y-%m-%dT%H:%M:%S') method to convert a datetime column to ISO datetime format in Pandas.
    • Code:
      import pandas as pd
      
      # Sample DataFrame with a datetime column
      df = pd.DataFrame({'datetime': ['2022-01-01 12:00:00', '2022-01-02 15:30:00']})
      
      # Convert datetime column to ISO format
      df['iso_datetime'] = pd.to_datetime(df['datetime']).dt.strftime('%Y-%m-%dT%H:%M:%S')
      
  6. ISO 8601 timestamp formatting in Pandas:

    • Description: Use the .dt.strftime('%Y-%m-%dT%H:%M:%S') method to format a datetime column to ISO 8601 in Pandas.
    • Code:
      import pandas as pd
      
      # Sample DataFrame with a datetime column
      df = pd.DataFrame({'datetime': ['2022-01-01 12:00:00', '2022-01-02 15:30:00']})
      
      # Format datetime column to ISO 8601
      df['iso8601_datetime'] = pd.to_datetime(df['datetime']).dt.strftime('%Y-%m-%dT%H:%M:%S')
      
  7. Pandas to_datetime ISO format examples:

    • Description: Use the pd.to_datetime() function with the format parameter to convert a column to ISO datetime format in Pandas.
    • Code:
      import pandas as pd
      
      # Sample DataFrame with a datetime column
      df = pd.DataFrame({'datetime': ['2022-01-01 12:00:00', '2022-01-02 15:30:00']})
      
      # Convert datetime column to ISO format using to_datetime
      df['iso_datetime'] = pd.to_datetime(df['datetime'], format='%Y-%m-%dT%H:%M:%S').dt.isoformat()
      
  8. ISO timestamp conversion with Pandas Series:

    • Description: Use the .apply() method to convert a Pandas Series of timestamps to ISO datetime format.
    • Code:
      import pandas as pd
      
      # Sample Series with timestamps
      timestamps = pd.Series([pd.Timestamp('2022-01-01 12:00:00'), pd.Timestamp('2022-01-02 15:30:00')])
      
      # Convert Series to ISO datetime format
      iso_datetime_series = timestamps.apply(lambda x: x.isoformat())
      
  9. String to ISO datetime in Pandas DataFrame:

    • Description: Use the pd.to_datetime() function to convert a string column to ISO datetime format in a Pandas DataFrame.
    • Code:
      import pandas as pd
      
      # Sample DataFrame with a string column
      df = pd.DataFrame({'date_string': ['2022-01-01', '2022-01-02']})
      
      # Convert string column to ISO datetime format
      df['iso_datetime'] = pd.to_datetime(df['date_string']).dt.isoformat()