×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python - Get total number of hours from a Pandas Timedelta?

Learn, how can we get total number of hours from a Pandas Timedelta?
Submitted by Pranit Sharma, on August 18, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Timedelta() is the method used for representing differences in time, which is expressed in different units, like hours, minutes, and seconds.

The Timedelta() method takes a string inside it as a parameter in which we simply define the time in human language and it converts that string to measure time differences.

Getting total number of hours from a Pandas Timedelta

To get the total number of hours from a pandas timedelta, we will first define a string for the time difference and then we will count the total number of hours by dividing the total time difference by the time of 1 hour.

Let us understand with the help of an example,

Python program to get total number of hours from a Pandas Timedelta

# Importing pandas package
import pandas as pd

# Importing numpy package
import numpy as np

# Creating dictionary
d = {'Time':[pd.Timedelta('1 days 2 hours')]}

# Creating DataFrame
df = pd.DataFrame(d)

# Display Original DataFrames
print("Created DataFrame:\n",df,"\n")

# Getting total hours
result = df['Time']/pd.Timedelta('1 hour')

# Display the data type of this column
print("Data type:\n",result)

Output

The output of the above program is:

Example: Total number of hours from a Pandas Timedelta

Python Pandas Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.