Table of Contents
Getting started with Python to_timestamp using pandas
Pandas is an open-source library in Python. It provides ready-to-use high-performance data structures and data analysis tools. Pandas module runs on top of NumPy and it is popularly used for data science and data analytics. Apart from data manipulations, it can also be used to visualize the data using various plots.
Pandas module has many useful methods and one of them is to_timestamp() method. This method returns the Timestamp representation of the Period at the target frequency at the specified end of the period. A timestamp is a sequence of characters or encoded information identifying when a certain event occurred,
The simple syntax of the python to_timestamp method is as follows:
Now let us jump into the practical part and understand how the to_timestam() method works in Python.
Example-1: Python to_timestamp() method with default parameter values
Before going to the implementation part, make sure that you have installed the pandas' module on your system. You can use the pip command to install the module. Once the installation is complete, import the module.
# importing pandas module import pandas as pd
Let us first create a Period and then will use the to_timestamp() method to convert the period to a time stamp.
# Creating a time period timePeriod = pd.Period(freq ='S', year = 2002, month = 3, day = 24, hour = 2, minute = 1, second = 33) # Print the Period object print(timePeriod)
This is now a Period, you can also verify by printing the type of the variable as shown below:
# printing the type of the variable print(type(timePeriod))
Periods can be used to check if a specific event occurs within a certain period. Basically, a Period represents an interval while a Timestamp represents a point in time. Now, let us convert the above Period into a time stamp.
# converting period to timestamp timeStamp =timePeriod.to_timestamp() # printing print(timeStamp)
Let us also check the type to verify that it is now a time stamp.
# printing the type of the variable print(type(timeStamp))
As you can see, we have converted a period into a time stamp using python. So far we have converted a Period into a time stamp using the default parameters of to_timestamp() method. Let us now look at its parameters as well.
Example-2: Python to_timestamp() with different parameter values
The to_timestamp() method takes an optional parameter known as frequency. It specifies the timestamp in the specified frequency. For example, "M" represents monthly frequency, and "T" represents minute frequency. Let us take an example and understand how the parameter in to_timestamp works. We will again create a Period and will convert the Period into a times stamp by specifying the frequency.
# Creating a time period timePeriod = pd.Period(freq='S',year = 2002, month = 3, day = 24, hour = 2, minute = 1, second = 33) # T shows minutely frequency timeStamp =timePeriod.to_timestamp(freq ="T") # print print(timeStamp)
As you can see, the seconds have been assigned 00 because we specified frequency as minutes. If we will assign the frequency monthly, then hours, minutes, and seconds will be zeros.
# Creating a time period timePeriod = pd.Period(freq='S',year = 2002, month = 3, day = 24, hour = 2, minute = 1, second = 33) # T shows minutely frequency timeStamp =timePeriod.to_timestamp(freq ="M") # print print(timeStamp)
As you can see, hours, minutes and seconds have been assigned to zeros.
Example-3: Timestamp to DateTime
DateTime in Python is the combination between dates and times. Let us first create a timestamp and then we will convert it into Python Datetime.
# importing datetime module from datetime import datetime # creating a random number to have time stamp timeStamp = 1446733073 # converting timestamp to datetime dt= datetime.fromtimestamp(timestamp) # printing print(dt) print(type(dt))
As you can see, we have now created a Python DateTime from a time stamp.
Converting the date column into a Python timestamp
Now, we will jump into real-life examples and convert the column from a data frame into a Python timestamp. We will use a time series dataset about Electric Production from Kaggle which you can download from here. Let us first import the dataset and then we will print a few columns.
# importing dataset data = pd.read_csv("Electric_Production.csv") # printing print(data.head())
As you can see, the dataset has two columns. we will only deal with the DATE column and will convert it into python to_timestamp.
# creating new row and converting the date into time stamp data['new'] = data['DATE'].to_timestamp
We have created a new row and added the timestamp there. Let us print the dataset to confirm it.
# printing print(data.head())
As you can see, we were able to create a new column that contains the time stamp.
pandas is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series. It contains many useful methods and one of them is to_timestamp. In this short article, we discussed how we can use to_timstamp method by solving various examples. Moreover, we also learned how we can convert a timestamp into Python DateTime.