Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython | Pandas Timestamp.now

Python | Pandas Timestamp.now

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]).

Syntax :Timestamp.now()

Parameters : None

Return : Timestamp

Example
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)


Output :

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

Now we will use the Timestamp.now() function to find the current time in the local timezone.

Python3




# return the current time
ts.now()


Output :

Timestamp('2023-07-21 10:07:23.622458')

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.  

Create Tempstamp Data

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

Create Timestamp Indices

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

RELATED ARTICLES

Most Popular

Recent Comments