Python Pandas - Construct a naive UTC datetime from a POSIX timestamp

PythonPandasServer Side ProgrammingProgramming

To construct a naive UTC datetime from a POSIX timestamp, use the timestamp.utcfromtimestamp() method. Pass the POSIX as an argument.

At first, import the required libraries −

import pandas as pd

Create a timestamp

timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')

Constructing a naive UTC datetime from a POSIX timestamp. POSIX is passed as an argument

timestamp.utcfromtimestamp(1631717502)

Example

Following is the code

import pandas as pd

# creating a timestamp
timestamp = pd.Timestamp('2021-09-14T15:12:34.261811624')

# display the Timestamp
print("Timestamp...\n", timestamp)

# constructing a naive UTC datetime from a POSIX timestamp
# POSIX is passed as an argument
print("\nConstruct UTC Datetime...\n", timestamp.utcfromtimestamp(1631717502))

Output

This will produce the following code

Timestamp...
 2021-09-14 15:12:34.261811624

Construct UTC Datetime...
 2021-09-15 14:51:42
raja
Updated on 13-Oct-2021 07:19:50

Advertisements