Open In App

Python | Pandas Timestamp.timestamp

Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

Pandas Timestamp.timestamp() function returns the time expressed as the number of seconds that have passed since January 1, 1970. That zero moment is known as the epoch.

Pandas Timestamp.timestamp Syntax

Syntax : Timestamp.timestamp()

Parameters : None

Return : number of seconds since zero moment

Timestamp.timestamp in Pandas Examples

The Timestamp.timestamp() method in Pandas is used to convert an Timestamp object to a Unix timestamp. A Unix timestamp is a numerical representation of a specific point in time. Here we will see different examples of how to use it:

Pandas Datetime to Unix Timestamp using Pandas

This example demonstrates the conversion between a Timestamp object and its corresponding Unix time representation using the pandas library. To get the Unix time representation, we will use Timestamp.timestamp() method to a Timestamp object.

Python3




import pandas as pd
 
# Create a Timestamp object
timestamp = pd.Timestamp("2022-10-12 12:30:45")
 
# Convert to Unix time
unix_time = timestamp.timestamp()
 
print("Unix Time:", unix_time)


Output:

Unix Time: 1665577845.0

Creating Timestamp Objects with Pandas

Use Timestamp.timestamp() function to return the number of seconds that have passed since the zero moment for the given Timestamp object.

Python3




# importing pandas as pd
import pandas as pd
 
# Create the Timestamp object
ts = pd.Timestamp(year = 2011,  month = 11, day = 21,
                  hour = 10, second = 49, tz = 'US/Central')
 
# Print the Timestamp object
print(ts)
# return the number of seconds
ts.timestamp()


Output:

2011-11-21 10:00:49-06:00
1321891249.0

As we can see in the output, the Timestamp.timestamp() function has returned a float value indicating this many seconds has passed since the epoch for the given Timestamp object.  

Converting Timestamp with Timezone to Unix Time using Pandas

Convert a timestamp with a different time zone to Unix timestamp. This example demonstrates the conversion between a Timestamp object with a specified timezone and its corresponding Unix time representation using the Pandas library

Python3




# importing pandas as pd
import pandas as pd
 
timestamp_with_tz = pd.Timestamp("2023-01-15 10:30:00", tz="Asia/Tokyo")
unix_time = timestamp_with_tz.timestamp()
 
print(unix_time)


Output:

 1673746200.0

Creating Timestamp Object in Specific Timezone

Use Timestamp.timestamp() function to return the number of seconds that has passed since the zero moment for the given Timestamp object.

Python3




# importing pandas as pd
import pandas as pd
 
# Create the Timestamp object
ts = pd.Timestamp(year = 2009, month = 5, day = 31,
                  hour = 4, second = 49, tz = 'Europe/Berlin')
 
# Print the Timestamp object
print(ts)


Output:

2009-05-31 04:00:49+02:00
1243735249.0

As we can see in the output, the Timestamp.timestamp() function has returned a float value indicating this many seconds has passed since the epoch for the given Timestamp object.



Last Updated : 04 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads