How to Change Pandas Dataframe Datetime to Time
Last Updated :
07 Feb, 2024
The DatetimeIndex contains datetime64[ns] data type, which represents timestamps with nanosecond precision. In many cases, we may just want to extract the time component from a Pandas Datetime column or index.
Let's discuss easy ways to convert the Datetime to Time data while preserving all the time information using pandas
Converting Datetime to Time in Pandas
Creating Sample Datetime Data
Let's start by creating a Pandas DataFrame with a Datetime index and column: This DataFrame contains 5 rows with hourly datetime data.
Python
import pandas as pd
import numpy as np
# Generate a DatetimeIndex with hourly frequency starting from '2023-02-01'
dates = pd.date_range('2023-02-01', periods=5, freq='H')
df = pd.DataFrame()
df['Datetime'] = dates
print(df)
Output:
Datetime
0 2023-02-01 00:00:00
1 2023-02-01 01:00:00
2 2023-02-01 02:00:00
3 2023-02-01 03:00:00
4 2023-02-01 04:00:00
Let's Extract Time from Datetime using following ways:
1. Using dt.time
Pandas provides the dt.time attribute to extract time from a DatetimeIndex or Series. This extracts the time component into a new column without losing any information.
Python
df['Time'] = df['Datetime'].dt.time
print(df)
Output:
Datetime Time
0 2023-02-01 00:00:00 00:00:00
1 2023-02-01 01:00:00 01:00:00
2 2023-02-01 02:00:00 02:00:00
3 2023-02-01 03:00:00 03:00:00
4 2023-02-01 04:00:00 04:00:00
2. Using DatetimeIndex
We can also extract time from the DatetimeIndex.
Python
df.index = dates
df.index = df.index.time
print(df)
Output:
Datetime Time
00:00:00 2023-02-01 00:00:00 00:00:00
01:00:00 2023-02-01 01:00:00 01:00:00
02:00:00 2023-02-01 02:00:00 02:00:00
03:00:00 2023-02-01 03:00:00 03:00:00
04:00:00 2023-02-01 04:00:00 04:00:00
3. Timezone Handling
An important consideration with datetime data is timezones. Pandas stores the timezone information and handles conversion properly when extracting the time component.
Let's set a timezone to the datetime data
Python
df.index = dates
df.index = df.index.tz_localize('US/Pacific')
print(df.index)
Output:
DatetimeIndex(['2023-02-01 00:00:00-08:00', '2023-02-01 01:00:00-08:00',
'2023-02-01 02:00:00-08:00', '2023-02-01 03:00:00-08:00',
'2023-02-01 04:00:00-08:00'],
dtype='datetime64[ns, US/Pacific]', freq=None)
Conclusion
The ability to switch between Datetime and Time makes it very convenient to work with temporal data in Pandas.
Similar Reads
Change String To Date In Pandas Dataframe Working with date and time data in a Pandas DataFrame is common, but sometimes dates are stored as strings and need to be converted into proper date formats for analysis and visualization. In this article, we will explore multiple methods to convert string data to date format in a Pandas DataFrame.U
5 min read
How to Convert Datetime to Date in Pandas ? DateTime is a collection of dates and times in the format of "yyyy-mm-dd HH:MM:SS" where yyyy-mm-dd is referred to as the date and HH:MM:SS is referred to as Time. In this article, we are going to discuss converting DateTime to date in pandas. For that, we will extract the only date from DateTime us
4 min read
How to Convert Integer to Datetime in Pandas DataFrame? Let's discuss how to convert an Integer to Datetime in it. Now to convert Integers to Datetime in Pandas DataFrame. Syntax of  pd.to_datetimedf['DataFrame Column'] = pd.to_datetime(df['DataFrame Column'], format=specify your format)Create the DataFrame to Convert Integer to Datetime in Pandas Check
2 min read
How to Group Pandas DataFrame By Date and Time ? In this article, we will discuss how to group by a dataframe on the basis of date and time in Pandas. We will see the way to group a timeseries dataframe by Year, Month, days, etc. Additionally, we'll also see the way to groupby time objects like minutes. Pandas GroupBy allows us to specify a groupb
3 min read
How to convert datetime to date in Python In this article, we are going to see how to convert DateTime to date in Python. For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt.date f
3 min read