Open In App

Python | Pandas Timestamp.now

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Pandas Timestamp.now() function returns the current time in the local timezone. It is Equivalent to datetime. now([tz]).

Pandas Timestamp.now Syntax

Syntax :Timestamp.now()

Parameters : None

Return : A Timestamp object representing the current time in the specified timezone.

Timestamp.now in Pandas Examples

The Timestamp.now() method in Pandas is used to create a Timestamp object representing the current time. A Timestamp object is a data type in Pandas that represents a specific point in time. It includes the date, time, and timezone information. Here we will see different examples on how to use this method:

Get Current Timestamp with Pandas

The code shows how to use Pandas Timestamp.now() to get the current date and time.

Python3




import pandas as pd
 
# Capture the current timestamp
current_time = pd.Timestamp.now()
 
print("Current Timestamp:", current_time)


Output:

Current Timestamp: 2023-10-12 07:24:35.042577

Get Current Time with Timestamp Objects in Pandas

Use Timestamp.now() the function to return the current time in the local timezone.

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 current time
ts.now()


Output :

2011-11-21 10:00:49-06:00
Timestamp('2023-10-12 07:25:32.822525')

As we can see in the output, the Timestamp.now() function has returned the current time in the local timezone. It auto-detects the local timezone.  

Generating Timestamps and Creating Time-Series Data using Pandas

Timestamped data associates each record with a specific timestamp. This is common when recording temperature, stock prices, or any other measurement over time. The pd.Timestamp.now() function creates timestamped data.

Python3




import pandas as pd
 
# Generate timestamps for the last 5 days
timestamps = pd.date_range(end=pd.Timestamp.now(), periods=5, freq="D")
 
# Create a DataFrame with timestamped data
data = {
    "timestamp": timestamps,
    "temperature": [21.2, 23.2, 27.2, 29.2, 31.2]
}
df = pd.DataFrame(data)
 
print(df)


Output:

                                            timestamp  temperature 
0          2023-08-20 04:40:14.707909         21.2 
1            2023-08-21 04:40:14.707909         23.2 
2           2023-08-22 04:40:14.707909         27.2 
3           2023-08-23 04:40:14.707909         29.2 
4           2023-08-24 04:40:14.707909         31.2

Creating a Time-Series DataFrame with Timestamped Indices using Pandas

When working with time-series data, it’s crucial to use timestamped indices for easy time-based operations and calculations.

Python3




import pandas as pd
 
# Generate timestamps for the last 7 days
timestamps = pd.date_range(end=pd.Timestamp.now(), periods=7, freq="D")
 
# Create a DataFrame with timestamped indices
data = {
    "temperature": [23.3, 34.5, 22.1, 22, 31.3, 33.4, 43.2],
    "humidity": [43, 58, 54, 34, 47, 56, 40]
}
df = pd.DataFrame(data, index=timestamps)
 
print(df)


Output:

                                                     temperature  humidity
2023-08-18 04:44:10.054030              23.3        43 
2023-08-19 04:44:10.054030              34.5        58 
2023-08-20 04:44:10.054030              22.1        54 
2023-08-21 04:44:10.054030              22.0        34 
2023-08-22 04:44:10.054030             31.3        47 
2023-08-23 04:44:10.054030            33.4        56 
2023-08-24 04:44:10.054030            43.2        40


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads