pandas.Timestamp.combine in Python

In this tutorial, you will learn how to combine date and time using the timestamp.combine() method in Python.

For this, let us import the Pandas package first using the command:

import pandas as pd

We will call pd.timestamp and it takes several parameters and some of them are year, month, day, hour, second, minute, etc. In this example, we will use all of these parameters and will store them in a variable called “timestamp_obj” which is nothing but a timestamp object.

timestamp_obj = pd.Timestamp(year = 2019, month = 11, day = 21, hour = 10, second = 49, minute = 22)

Now we will use .date() function to return the date and we will store it in a variable. You can definitely choose the variable name yourself.

date = timestamp_obj.date()
print(date)

Now if you print this, you will get the following output:

2019-11-21

In the same way, you can .time() function on the timestamp object above and you will be able to return time. And just like the above, we will store it in a variable, and the code for the same is:

time = timestamp_obj.time()
print(time)

Output for the above code will be:

10:22:49

You can finally use timestamp.combine() to combine the date and time. It obviously takes date and time as an argument.

result = pd.Timestamp.combine(date, time)
print(result)

The output for the above code will be:

2019-11-21 10:22:49

Leave a Reply

Your email address will not be published. Required fields are marked *