How to create a DateTime column in pandas

In this shot, we will explain how to create a datetime column in a data frame using the data that is already present in the data frame.

We will create a data frame that will contain month, day, year, and hour columns, and then we will create a single column that will represent a datetime object.

Let’s see the code now:

import pandas as pd
df = pd.DataFrame([[12, 25, 2020, 10], [1, 15, 2021, 11]],
columns = ['month', 'day', 'year', 'hour'])
print("Dataframe: \n", df)
df["Date-Time"] = pd.to_datetime(df)
print("Dataframe after creating Datetime:\n", df)

Explanation:

  • In line 1, we import the required package.
  • In line 3, we create a data frame that contains the month, day, year, and hour columns.
  • In line 5, we print the data frame that we created in line 3.
  • In line 7, we use the to_datetime() function, which takes your entire data frame and creates a datetime object, to create a new column, Date-Time, in our data frame and save the new values.
  • Finally, in line 9, we print the new data frame. Here, we can see that we have created a new _datetime_ column in the data frame.

In the above example, we used the complete data frame to create the datetime object. Now, let’s see how we can use a subset of data frame columns to create a datetime object:

import pandas as pd
df = pd.DataFrame([[12, 25, 2017, 10],[1, 15, 2018, 11]],
columns = ['month', 'day', 'year', 'hour'])
print("Dataframe:", df)
df["Date-Time-Subset"] = pd.to_datetime(df[['month', 'day', 'year']])
print("Dataframe after creating Datetime from a subset of columns:\n", df)

Explanation:

  • This code is almost the same as the previous example – the only difference is in line 7, where we pass a subset of columns in the to_datetime() function. This creates a _datetime_ column that will only contain the date, as we have not used the hour column to create a date-time object.

How to create a _datetime_ index

Now, let’s see how we can assign datetime as an index to our data frame:

import pandas as pd
df = pd.DataFrame([[12, 25, 2017, 10], [1, 15, 2018, 11]],
columns = ['month', 'day', 'year', 'hour'])
print("Dataframe:\n", df)
df.index = pd.to_datetime(df[['month', 'day', 'year']])
print("Dataframe after creating Datetime as index:\n", df)

Explanation:

  • The code is almost the same – the only difference is in line 7, where we set the datetime values as an index to the data frame.

As you can see, we can create the datetime data values very easily, and even assign them as an index for fast lookup within the data frame.

Free Resources