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 pddf = 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)
month, day, year, and hour columns.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._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 pddf = 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)
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._datetime_ indexNow, let’s see how we can assign datetime as an index to our data frame:
import pandas as pddf = 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)
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.